最小树形图(poj3164)

Command Network
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 12834   Accepted: 3718

Description

After a long lasting war on words, a war on arms finally breaks out between littleken’s and KnuthOcean’s kingdoms. A sudden and violent assault by KnuthOcean’s force has rendered a total failure of littleken’s command network. A provisional network must
be built immediately. littleken orders snoopy to take charge of the project.

With the situation studied to every detail, snoopy believes that the most urgent point is to enable littenken’s commands to reach every disconnected node in the destroyed network and decides on a plan to build a unidirectional communication network. The
nodes are distributed on a plane. If littleken’s commands are to be able to be delivered directly from a node A to another node B, a wire will have to be built along the straight line segment connecting the two nodes. Since it’s in wartime, not between all
pairs of nodes can wires be built. snoopy wants the plan to require the shortest total length of wires so that the construction can be done very soon.

Input

The input contains several test cases. Each test case starts with a line containing two integer N (N ≤ 100), the number of nodes in the destroyed network, and M (M ≤ 104), the number of pairs of nodes between
which a wire can be built. The next N lines each contain an ordered pair xi and yi, giving the Cartesian coordinates of the nodes. Then follow M lines each containing two integers i and jbetween
1 and N (inclusive) meaning a wire can be built between node i and node j for unidirectional command delivery from the former to the latter. littleken’s headquarter is always located at node 1. Process to end of file.

Output

For each test case, output exactly one line containing the shortest total length of wires to two digits past the decimal point. In the cases that such a network does not exist, just output ‘poor snoopy’.

Sample Input

4 6
0 6
4 6
0 0
7 20
1 2
1 3
2 3
3 4
3 1
3 2
4 3
0 0
1 0
0 1
1 2
1 3
4 1
2 3

Sample Output

31.19
poor snoopy
题意:给出n个网络节点的个数和其坐标,然后m条单向边,服务器的位置是1,问要是所有的基站都受到信号,最小的花费是多少
分析:最小树形图

程序:

#include"string.h"
#include"stdio.h"
#include"math.h"
#include"queue"
#define eps 1e-10
#define M 109
#define inf 100000000
using namespace std;
struct node
{
double x,y;
}p[M];
struct edge
{
int u,v;
double w;
}edge[M*M];
int pre[M],id[M],use[M];
double in[M];
double pow(double x)
{
return x*x;
}
double Len(node a,node b)
{
return sqrt(pow(a.x-b.x)+pow(a.y-b.y));
}
double mini_tree(int root,int n,int m)
{
double ans=0;
int i,u;
while(1)
{
for(i=1;i<=n;i++)
in[i]=inf;
for(i=1;i<=m;i++)
{
int u=edge[i].u;
int v=edge[i].v;
if(edge[i].w<in[v]&&u!=v)
{
in[v]=edge[i].w;
pre[v]=u;
}
}
for(i=1;i<=n;i++)
{
if(i==root)continue;
ans+=in[i];
if(fabs(in[i]-inf)<eps)
return -1;
}
memset(id,-1,sizeof(id));
memset(use,-1,sizeof(use));
int cnt=0;
for(i=1;i<=n;i++)
{
int v=i;
while(v!=root&&use[v]!=i&&id[v]==-1)
{
use[v]=i;
v=pre[v];
}
if(v!=root&&id[v]==-1)
{
++cnt;
id[v]=cnt;
for(u=pre[v];u!=v;u=pre[u])
id[u]=cnt;
}
}
if(cnt==0)
break;
for(i=1;i<=n;i++)
if(id[i]==-1)
id[i]=++cnt;
for(i=1;i<=m;i++)
{
int u=edge[i].u;
int v=edge[i].v;
edge[i].u=id[u];
edge[i].v=id[v];
if(edge[i].u!=edge[i].v)
edge[i].w-=in[v];
}
n=cnt;
root=id[root];
}
return ans;
}
int main()
{
int n,m,i;
while(scanf("%d%d",&n,&m)!=-1)
{
for(i=1;i<=n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
for(i=1;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
double L=Len(p[a],p[b]);
edge[i].u=a;
edge[i].v=b;
edge[i].w=L;
}
double ans=mini_tree(1,n,m);
if(ans<0)
printf("poor snoopy\n");
else
printf("%.2lf\n",ans);
}
return 0;
}

上一篇:5. SpringBoot —— Actuator简介


下一篇:在hexo静态博客中利用d3-cloud来展现标签云