最小树形图poj3164 Command Network

来源:互联网 发布:淘宝差评师怎么找 编辑:程序博客网 时间:2024/06/16 00:32
Command Network
Time Limit: 1000MS Memory Limit: 131072KTotal Submissions: 18628 Accepted: 5340

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 60 64 60 07 201 21 32 33 43 13 24 30 01 00 11 21 34 12 3

Sample Output

31.19

poor snoopy

#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>#include <iostream>using namespace std;const int M=10005,N=105,inf=1e9;//最小树形图:选择一些边,使得根节点能够到达图中所有的节点,并使得选出的边的边权和最小 (有向图,所有不可用最小生成树) struct Edge{    int u, v;double w;} edge[M];int n, m;int id[N],vis[N],pre[N];double in[N];double Directed_MST(int root) {//如果ret<0,则不存在最小树形图     double ret = 0;    while(true) {        for(int i=0;i<n;i++)            in[i]=inf;        for(int i=0;i<m;i++){    //找最小入边            int u = edge[i].u;            int v = edge[i].v;            if(edge[i].w < in[v] && u != v) {                in[v] = edge[i].w;                pre[v] = u;            }        }        for(int i=0;i<n;i++) { //如果存在除root以外的孤立点,则不存在最小树形图            if(i == root)  continue;            if(in[i] == inf)            {            return -1;}        }        int cnt = 0;  //顶点从0开始编号        memset(vis,-1,sizeof(vis));        memset(id,-1,sizeof(id));        in[root] = 0;        for(int i=0;i<n;i++) {    //找环            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] = cnt;                id[v] = cnt++;            }        }        if(cnt == 0) break;        for(int i=0;i<n;i++)            if(id[i] == -1)                id[i] = cnt++;    //重新标号        for(int i=0;i<m;i++){    //更新其他点到环的距离            int v =edge[i].v;            edge[i].u = id[edge[i].u];            edge[i].v = id[edge[i].v];            if(edge[i].u != edge[i].v)                edge[i].w -= in[v];        }        n = cnt;        root = id[root];    }    return ret;}double x[105],y[105];inline double dist(int i,int j){return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));}int main(){while(~scanf("%d%d",&n,&m)){for(int i=0;i<n;++i)scanf("%lf%lf",x+i,y+i);for(int i=0;i<m;++i){int u,v;scanf("%d%d",&u,&v);u--,v--;edge[i].u=u,edge[i].v=v,edge[i].w=dist(u,v);}double ans=Directed_MST(0);if(ans>=0)printf("%.2f\n",ans);else puts("poor snoopy");}return 0;} 


原创粉丝点击