poj 3164 Command Network(最小樹形圖模板題)朱_ 劉演算法

life4711發表於2014-07-20

http://poj.org/problem?id=3164

Command Network
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 12454   Accepted: 3602

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
題目大意:給定n個點的座標和m組能直接相互到達的點對(有向),權值為兩點的距離,求最小樹形圖。1為根節點

思路:朱劉演算法模板題。

#include <cstdio>
#include <iostream>
#include <math.h>
#include <cstring>
using namespace std;
//======================================================================
//最小樹形圖(有向圖的最小生成樹)朱劉演算法模板
const int N=101,M=10001,inf=2147483647;
struct edge
{
    int u,v;
    double w;
} e[M];
struct node
{
    double x,y;
} p[N];
int n,m,id[N],pre[N],v[N];
double inw[N];
double ans;
void zhu_liu(int root)
{
    int s,t,idx=n;
    ans=0;
    while (idx)
    {
        for (int i=1; i<=n; ++i) inw[i]=inf,id[i]=-1,v[i]=-1;
        for (int i=1; i<=m; ++i)
        {
            s=e[i].u;
            t=e[i].v;
            if (e[i].w>inw[t] || s==t) continue;
            pre[t]=s;
            inw[t]=e[i].w;
        }
        inw[root]=0;
        pre[root]=root;
        for (int i=1; i<=n; ++i)
        {
            if (inw[i]==inf)
            {
                printf("poor snoopy\n");
                return;
            }
            ans+=inw[i];
        }
        idx=0;
        for (int i=1; i<=n; ++i)
            if (v[i]==-1)
            {
                t=i;
                while (v[t]==-1) v[t]=i,t=pre[t];
                if (v[t]!=i || t==root) continue;
                id[t]=++idx;
                for (s=pre[t]; s!=t; s=pre[s]) id[s]=idx;
            }
        if (idx==0) continue;
        for (int i=1; i<=n; ++i)
            if (id[i]==-1) id[i]=++idx;
        for (int i=1; i<=m; ++i)
        {
            e[i].w-=inw[e[i].v];
            e[i].u=id[e[i].u];
            e[i].v=id[e[i].v];
        }
        n=idx;
        root=id[root];
    }
    printf("%.2lf\n",ans);
}
//==============================================================
double longth(node a,node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for (int i=1; i<=n; ++i)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&e[i].u,&e[i].v);
            e[i].w=(longth(p[e[i].u],p[e[i].v]));
        }
        zhu_liu(1);
    }
    return 0;
}


相關文章