POJ3164 Command Network —— 最小树形图

题目链接:https://vjudge.net/problem/POJ-3164

 

Command Network
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 19079   Accepted: 5495

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

 

 

题解:

赤裸裸的最小树形图,即有向图的最小生成树。

 

代码如下:

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <algorithm>
  4 #include <cstring>
  5 #include <cmath>
  6 using namespace std;
  7 typedef long long LL;
  8 const double EPS = 1e-6;
  9 const int INF = 2e9;
 10 const LL LNF = 9e18;
 11 const int MOD = 1e9+7;
 12 const int MAXN = 1e2+10;
 13 
 14 struct Edge
 15 {
 16     int u, v;
 17     double w;
 18 }edge[10010];
 19 
 20 int x[MAXN], y[MAXN];
 21 int pre[MAXN], id[MAXN], vis[MAXN];
 22 double in[MAXN];
 23 
 24 double zhuliu(int root, int n, int m)
 25 {
 26     double res = 0;
 27     while(1)
 28     {
 29         for(int i = 0; i<n; i++)
 30             in[i] = INF+1;
 31         for(int i = 0; i<m; i++)
 32         if(edge[i].u!=edge[i].v && edge[i].w<in[edge[i].v])
 33         {
 34             pre[edge[i].v] = edge[i].u;
 35             in[edge[i].v] = edge[i].w;
 36         }
 37 
 38         for(int i = 0; i<n; i++)
 39             if(i!=root && in[i]>INF)
 40                 return -1;
 41 
 42         int tn = 0;
 43         memset(id, -1, sizeof(id));
 44         memset(vis, -1, sizeof(vis));
 45         in[root] = 0;
 46         for(int i = 0; i<n; i++)
 47         {
 48             res += in[i];
 49             int v = i;
 50             while(vis[v]!=i && id[v]==-1 && v!=root)
 51             {
 52                 vis[v] = i;
 53                 v = pre[v];
 54             }
 55             if(v!=root && id[v]==-1)
 56             {
 57                 for(int u = pre[v]; u!=v; u = pre[u])
 58                     id[u] = tn;
 59                 id[v] = tn++;
 60             }
 61         }
 62         if(tn==0) break;
 63         for(int i = 0; i<n; i++)
 64             if(id[i]==-1)
 65                 id[i] = tn++;
 66 
 67         for(int i = 0;  i<m; )
 68         {
 69             int v = edge[i].v;
 70             edge[i].u = id[edge[i].u];
 71             edge[i].v = id[edge[i].v];
 72             if(edge[i].u!=edge[i].v)
 73                 edge[i++].w -= in[v];
 74             else
 75                 swap(edge[i], edge[--m]);
 76         }
 77         n = tn;
 78         root = id[root];
 79     }
 80     return res;
 81 }
 82 
 83 int main()
 84 {
 85     int n, m;
 86     while(scanf("%d%d",&n,&m)!=EOF)
 87     {
 88         for(int i = 0; i<n; i++)
 89             scanf("%d%d", &x[i], &y[i]);
 90 
 91         for(int i = 0; i<m; i++)
 92         {
 93             int u, v;
 94             scanf("%d%d", &u, &v);
 95             edge[i].u = --u; edge[i].v = --v;
 96             edge[i].w = sqrt( 1.0*(x[u]-x[v])*(x[u]-x[v]) + 1.0*(y[u]-y[v])*(y[u]-y[v]) );
 97         }
 98 
 99         double ans = zhuliu(0, n, m);
100         if(ans<0) puts("poor snoopy");
101         else printf("%.2f\n", ans);
102     }
103 }
View Code

 

posted on 2017-10-31 13:55  h_z_cong  阅读(211)  评论(0编辑  收藏  举报

导航