Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 15882 | Accepted: 4568 |
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
题目大意:长时间战斗之后,战争切断了L和K王国的联系,Snoopy了解了具体情况之后,他决定先建立一个单向的传输网络。给出n个点的坐标,给出m个关系,表示a和b能够连接,连接需要花费的权值是两个点之间的距离,Snoopy为了连接所有的节点,还想用最小的花费来完成这个任务,如果这个任务能够完成,输出权值和,否则输出poor snoopy。
思路:有向图的最小生成树:最小树形图,用朱、刘算法来解决,这里对于朱、刘算法的个人详解:http://blog.csdn.net/mengxiang000000/article/details/51276668。
AC代码:
#include<stdio.h> #include<string.h> #include<math.h> using namespace std; #define INF 0x7fffffff double x[1000]; double y[1000]; double w[1000][1000]; int flag[1000];//判断是否在有向环里边的缩点 int vis[1000]; int pre[1000]; int n,m; void init() { memset(vis, 0, sizeof(vis)); memset(flag, 0, sizeof(flag)); for(int i=0; i<=n; i++) { w[i][i] = INF; for(int j=i+1; j<=n; j++) w[i][j]=w[j][i]=INF; } } double directed_mst(int u)//u表示根节点 { double ans=0; memset(vis, 0, sizeof(vis)); while(true) { //求最短弧集合E for(int i=1; i<=n; i++)if(i!=u&&!flag[i]) { w[i][i]=INF, pre[i] = i; for(int j=1; j<=n; j++)if(!flag[j] && w[j][i]<w[pre[i]][i]) { pre[i] = j; } if(pre[i]==i)return -1;//也可以用dfs预处理判断凸的连通 } //判断E是否有环 int i; for(i=1; i<=n; i++) { if(i!=u&&!flag[i]) { int j=i, cnt=0; while(j!=u && pre[j]!=i && cnt<=n) j=pre[j], ++cnt; if(j==u || cnt>n) continue; //最后能找到起点(根)或者是走过的点已经超过了n个,表示没有有向环 break; } } if(i>n) { for(int i=1; i<=n; i++)if(i!=u && !flag[i]) ans+=w[pre[i]][i]; return ans; } //有环,进行收缩,把整个环都收缩到一个点i上。 int j=i; memset(vis, 0, sizeof(vis)); do { ans += w[pre[j]][j], j=pre[j], vis[j]=flag[j]=true;//对环内的点标记,并且直接对环的权值进行加和记录,在最后找到最小树形图之后就不用展开收缩点了 } while(j!=i); flag[i] = false; // 环缩成了点i,点i仍然存在 //收缩点的同时,对边权值进行改变 for(int k=1; k<=n; ++k)if(vis[k]) // 在环中点点 { for(int j=1; j<=n; j++)if(!vis[j]) // 不在环中的点 { if(w[i][j] > w[k][j]) w[i][j] = w[k][j]; if(w[j][k]<INF && w[j][k]-w[pre[k]][k] < w[j][i]) w[j][i] = w[j][k] - w[pre[k]][k]; } } } return ans; } int main() { while(~scanf("%d%d",&n,&m)) { init(); for(int i=1; i<=n; i++) { scanf("%lf%lf",&x[i],&y[i]); } for(int i=0; i<m; i++) { int a,b; scanf("%d%d",&a,&b); w[a][b]=sqrt(pow(x[a]-x[b],2)+pow(y[a]-y[b],2)); } double ans = directed_mst(1); if(ans<0) printf("poor snoopy\n"); else printf("%.2f\n",ans); } }