Poj 3467 Command Network 最小树形图

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Tawn0000/article/details/82980486

                                                                                       Command Network

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

最小树形图,朱刘算法 O(VE) 主要是充分理解一下原理

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;
const int maxn = 1e2 + 10;
const int maxm = 1e4 + 10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
#define type double
type in[maxn];
int id[maxn],used[maxn],pre[maxn];

struct edge
{
  int u,v;
  type c;
  edge(int x = 0, int y = 0, type z = 0) : u(x),v(y),c(z) {}
}es[maxm];

type MTG(int root, int n, int m)
{
  type res = 0;
  while(1)
  {
    for(int i = 1; i <= n; i++)  in[i] = INF,id[i] = used[i] = -1;
    for(int i = 0; i < m; i++)
     {
       int u = es[i].u;
       int v = es[i].v;
       if(es[i].c < in[v] && u != v)  in[v] = es[i].c,pre[v] = u;
     }//找最小的入边
    for(int i = 1; i <= n; i++)
     {
       if(i == root) continue;
       res += in[i];
       if(fabs(in[i] - INF) < eps) return -1;
     }//加入所有的入边权到答案中
    int cnt = 0;
    for(int i = 1; i <= n; i++)//枚举每个点为起点进行找环
    {
      int v = i;
      while(v != root && used[v] != i && id[v] == -1)  used[v] = i,v = pre[v];
      if(v != root && id[v] == -1)//找到环的时候进行缩点编号
       {
         ++cnt;
         id[v] = cnt;
         for(int u = pre[v]; u != v; u = pre[u]) id[u] = cnt;
       }
    }
    if(cnt == 0) break;//无环则退出
    for(int i = 1; i <= n; i++)
       if(id[i] == -1)  id[i] = ++cnt;

    for(int i = 0; i < m; i++)//更新边
     {
       int u = es[i].u;
       int v = es[i].v;
       es[i].u = id[u];
       es[i].v = id[v];
       if(u != v)  es[i].c -= in[v]; //更新边权
     }
     n = cnt;//更新节点数目和根节点编号
     root = id[root];
     }
     return res;
  }
int n,m;
double x[maxn],y[maxn];

int main()
{
  while(~scanf("%d%d",&n,&m))
  {
    for(int i = 1; i <= n; i++)
      scanf("%lf%lf",&x[i],&y[i]);
    for(int i = 0; i < m; i++)
    {
      int u,v;
      double c;
      scanf("%d%d",&u,&v);
      if(u == v) {i--,m--;continue;}
      c = sqrt((x[u]-x[v])*(x[u]-x[v]) + (y[u]-y[v])*(y[u]-y[v]));
      es[i] = edge(u,v,c);
    }
    double ans = MTG(1,n,m);
    if(ans < 0) printf("poor snoopy\n");
    else printf("%.2f\n",ans);
  }
  return 0;
}

猜你喜欢

转载自blog.csdn.net/Tawn0000/article/details/82980486