POJ 3164 Command Network

Command Network
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 8780   Accepted: 2569

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 j between 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

Source

刘朱算法,求最小树形图
//这题差不多就是模板题了
//我被自环坑了一下午、不解释
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <cmath> #define N 102 #define M 999999999 using namespace std; double dis(double &x1,double &y1,double &x2,double &y2) { return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } double map[N][N]; int pre[N]; bool flag[N],visit[N]; int n,m; double sum; void zhuliu() { int i,j,k; for(i=1;i<=n;i++) map[i][i]=M,flag[i]=false;//开始没考虑自环,就没有map[i][i]=M,然后WA了N久 sum=0; pre[1]=1; while(true) { //求最短弧集合S for(i=2;i<=n;i++) { if(flag[i]) continue; pre[i]=i; for(j=1;j<=n;j++) if(!flag[j]&&map[j][i]<map[pre[i]][i]) pre[i]=j; if(pre[i]==i) //说明不连通 {sum=-1.0;return;} } //检查S for(i=2;i<=n;i++) { if(flag[i]) continue; memset(visit,0,sizeof(visit)); visit[1]=true; j=i; do { visit[j]=true; j=pre[j]; }while(!visit[j]); if(j==1) continue;//无环 //有环 i=j; do { sum+=map[pre[j]][j];//记录环的权值 j=pre[j]; }while(j!=i); j=i; //对与环上的点有关的边,修改边权 do { for(k=1;k<=n;k++) { if(flag[k]) continue; if(map[k][j]<M&&k!=pre[j]) map[k][j]-=map[pre[j]][j]; } j=pre[j]; }while(j!=i); //将环集中在i点 for(j=1;j<=n;j++) { if(j==i)continue; for(k=pre[i];k!=i;k=pre[k]) { if(map[k][j]<map[i][j]) map[i][j]=map[k][j]; if(map[j][k]<map[j][i]) map[j][i]=map[j][k]; } } for(j=pre[i];j!=i;j=pre[j]) flag[j]=true; break; } if(i==n+1) { for(i=2;i<=n;i++) if(!flag[i]) sum+=map[pre[i]][i]; break; } } return; } int main() { int i,j,k; double x[N][2]; while(scanf("%d%d",&n,&m)!=EOF) { for(i=1;i<=n;i++) for(j=1;j<=n;j++) map[i][j]=M; for(i=1;i<=n;i++) scanf("%lf %lf",&x[i][0],&x[i][1]); for(i=0;i<m;i++) { scanf("%d %d",&j,&k); map[j][k]=dis(x[j][0],x[j][1],x[k][0],x[k][1]); } zhuliu(); if(sum<0.0) printf("poor snoopy\n"); else printf("%.2lf\n",sum); } return 0; }

你可能感兴趣的:(command)