POJ 3164 Command Network (最小树形图模板,3级)

Command Network
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 10662   Accepted: 3110

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<cstring>
#include<cstdio>
#include<cmath>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
typedef double type;
using namespace std;
const int nn=105;
const int mm=1e4+9;
const double oo=1e9;
class Point
{
  public:double u,v;
}p[nn];
class Edge
{
public:
  int u,v;
  type w;
}e[mm];
int pre[nn],id[nn],vis[nn],n,m;
type in[nn];
type dis(Point a,Point b)
{
  return sqrt((a.u-b.u)*(a.u-b.u)+(a.v-b.v)*(a.v-b.v) );
}
type Direct_MST(int root,int V,int E)
{
  type ret=0;
  while(1)
  {
    //找出每个的最小入边
    FOR(i,0,V-1)in[i]=oo;
    in[root]=0;
    FOR(i,0,E-1)
    {
      int u=e[i].u,v=e[i].v;
      if(e[i].w<in[v]&&u!=v){pre[v]=u;in[v]=e[i].w; }
    }
    FOR(i,0,V-1)
    {
      if(i==root)continue;
      if(in[i]==oo)return -1;//还有无入度点
    }
    ///find cicle
    clr(vis,-1);clr(id,-1);
    int bcc_no=0;
    FOR(i,0,V-1)
    {
      ret+=in[i];
      int v=i;
      while(vis[v]!=i&&id[v]==-1&&v!=root)
      { vis[v]=i;//每次的标号都不同,所以不需要初始化
        v=pre[v];
      }
      if(v!=root&&id[v]==-1)//缩点
      {
        for(int u=pre[v];u!=v;u=pre[u])
          id[u]=bcc_no;
        id[v]=bcc_no++;
      }
    }
    if(bcc_no==0)break;//no cicle
    FOR(i,0,V-1)
    if(id[i]==-1)
      id[i]=bcc_no++;
    //create new graph
    FOR(i,0,E-1)
    {
      int u=e[i].u,v=e[i].v;
      e[i].u=id[u];
      e[i].v=id[v];
      if(id[u]^id[v])e[i].w-=in[v];
    }
    V=bcc_no;
    root=id[root];
  }
  return ret;
}
int main()
{
  while(~scanf("%d%d",&n,&m))
  {
    FOR(i,0,n-1)scanf("%lf%lf",&p[i].u,&p[i].v);
    FOR(i,0,m-1)
    {
      scanf("%d%d",&e[i].u,&e[i].v);
      e[i].u--;e[i].v--;
      if(e[i].u!=e[i].v)e[i].w=dis(p[ e[i].u ],p[ e[i].v ]);
      else e[i].w=oo;//去自环
    }
    type ans=Direct_MST(0,n,m);
    if(ans==-1)printf("poor snoopy\n");
    else printf("%.2lf\n",ans);
    //cout<<ans<<endl;
  }
  return 0;
}


posted @ 2013-08-31 18:23  剑不飞  阅读(179)  评论(0编辑  收藏  举报