Command Network

Command Network

Time Limit: 1000MS
Memory Limit: 131072K

Total Submissions: 11970
Accepted: 3482

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
 
完全是照着别人的代码打出来的,第一道最小树形图
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <queue>
  6 #include <vector>
  7 #include <map>
  8 #include <cmath>
  9 using namespace std;
 10 typedef long long ll;
 11 const double INF=1<<30;
 12 const int N=105;
 13 const int M=10010;
 14 int vis[N],id[N];
 15 int n,m,ecnt,pre[N];
 16 double in[N];
 17 
 18 struct edge{
 19      int u,v;
 20      double w;
 21      edge( ) {}
 22      edge(int u,int v,double w):u(u),v(v),w(w) {}
 23 }e[M];
 24 
 25 struct node
 26 {
 27     double x,y;
 28 }point[N];
 29 
 30 double getdis(node a,node b)
 31 {
 32     double x=a.x-b.x;
 33     double y=a.y-b.y;
 34     return sqrt(x*x+y*y);
 35 }
 36 
 37 double MST(int root,int n,int m)
 38 {
 39     double res=0;
 40     while(1)
 41     {
 42         for(int i = 0; i < n; i++)
 43            in[i] = INF;
 44         for(int i = 0; i < m; i++)
 45         {
 46             int u = e[i].u;
 47             int v = e[i].v;
 48             if(e[i].w < in[v] && u != v)
 49               {pre[v] = u; in[v] = e[i].w;}
 50         }
 51         for(int i = 0; i < n; i++)
 52         {
 53             if(i == root) continue;
 54             if(in[i] == INF) return -1;//除了根以外有点没有入边,则根无法到达它
 55         }
 56         //2.找环
 57         int cnt = 0;
 58         memset(id, -1, sizeof(id));
 59         memset(vis, -1, sizeof(vis));
 60         in[root] = 0;
 61         for(int i = 0; i < n; i++) //标记每个环
 62         {
 63             res += in[i];
 64             int v = i;
 65             while(vis[v] != i && id[v] == -1 && v != root)  //每个点寻找其前序点,要么最终寻找至根部,要么找到一个环
 66             {
 67                 vis[v] = i;
 68                 v = pre[v];
 69             }
 70             if(v != root && id[v] == -1)//缩点
 71             {
 72                 for(int u = pre[v]; u != v; u = pre[u])
 73                     id[u] = cnt;
 74                 id[v] = cnt++;
 75             }
 76         }
 77         if(cnt == 0) break; //无环   则break
 78         for(int i = 0; i < n; i++)
 79             if(id[i] == -1) id[i] = cnt++;
 80         for(int i = 0; i < m; i++)
 81         {
 82             int u = e[i].u;
 83             int v = e[i].v;
 84             e[i].u = id[u];
 85             e[i].v = id[v];
 86             if(id[u] != id[v]) e[i].w -= in[v];
 87         }
 88         n = cnt;
 89         root = id[root];
 90     }
 91     return res;
 92 }
 93 
 94 void solve()
 95 {
 96     for(int i=0; i<n; i++) scanf("%lf%lf",&point[i].x,&point[i].y);
 97     ecnt=0;
 98     int u,v;
 99     for(int i=0; i<m; i++)
100     {
101         scanf("%d%d",&u,&v);
102         if(u==v) continue;
103         u--; v--;
104         double dis=getdis(point[u],point[v]);
105         e[ecnt++]=edge(u,v,dis);
106     }
107     double ans=MST(0,n,ecnt);
108     if(ans == -1) printf("poor snoopy\n");
109     else printf("%.2f\n", ans);
110 }
111 
112 int main()
113 {
114     while(scanf("%d%d",&n,&m)>0) solve();
115     return 0;
116 }
View Code

 

转载于:https://www.cnblogs.com/zyx1314/p/3719542.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值