您的位置:首页 > 其它

poj3164 Command Network 最小树形图-朱刘算法

2014-07-20 12:50 459 查看
Language:
Default

Command Network

Time Limit: 1000MSMemory Limit: 131072K
Total Submissions: 12439Accepted: 3596
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

POJ Monthly--2006.12.31, galaxy

思路:裸的固定根的最小树形图,详见代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN=10000+100;
const int inf=0x3fffffff;
int n,m;
double in[MAXN];
int pre[MAXN],vis[MAXN],id[MAXN];
struct node
{
    double x,y;
}p[MAXN];
struct Edge
{
    int u,v;
    double w;
}edge[MAXN];
double get_dis(node a,node b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double Direct_MST(int rt){
    double ans=0;
    while(1){
        for(int i=1;i<=n;i++)
            in[i]=inf;
        for(int i=0;i<m;i++){
            int u=edge[i].u,v=edge[i].v;
            if(edge[i].w<in[v] && u!=v){
                pre[v]=u; in[v]=edge[i].w;
            }
        }
        for(int i=1;i<=n;i++){
            if(i == rt) continue;
            if(in[i] == inf ) return -1;
        }
        int cnt=1; in[rt]=0;
        memset(vis,-1,sizeof(vis));
        memset(id,-1,sizeof(id));
        for(int i=1;i<=n;i++){
            ans+=in[i];
            int v=i;
            while(v!=rt && vis[v]!=i && id[v]== -1)
                vis[v]=i,v=pre[v];
            if(v!=rt && id[v]== -1){
                for(int u=pre[v];u!=v;u=pre[u])
                    id[u]=cnt;
                id[v]=cnt++;
            }
        }
        if(cnt == 1) break;
        for(int i=1;i<=n;i++) if(id[i]== -1)
            id[i]=cnt++;
        for(int i=0;i<m;i++){
            int u=edge[i].u,v=edge[i].v;
            edge[i].u=id[u]; edge[i].v=id[v];
            if(id[u]!=id[v]) edge[i].w-=in[v];
        }
        n=cnt-1; rt=id[rt];
    }
    return ans;
}
int main()
{
    //freopen("text.txt","r",stdin);
    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=0;i<m;i++){
            scanf("%d%d",&edge[i].u,&edge[i].v);
            if(edge[i].u!=edge[i].v)
                edge[i].w=get_dis(p[edge[i].u],p[edge[i].v]);
            else
                edge[i].w=inf;
        }
        double ans=Direct_MST(1);
        if(ans==-1)
            printf("poor snoopy\n");
        else
            printf("%.2lf\n",ans);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: