Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 14987 | Accepted: 4301 |
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
题目大意:给一张有向图,找出最小生成树。
题目分析:朱-刘算法。模板题。。。
代码如下:
# include<iostream> # include<cstdio> # include<queue> # include<cmath> # include<vector> # include<cstring> # include<algorithm> using namespace std; # define REP(i,s,n) for(int i=s;i<n;++i) const int N=105; const double inf=1e30; struct Edge { int fr,to; double w; }; Edge e[N*N]; double in[N]; int n,m,x[N],y[N],pre[N],vis[N],ID[N]; double dist(int id1,int id2) { return sqrt((x[id1]-x[id2])*(x[id1]-x[id2])*1.0+(y[id1]-y[id2])*(y[id1]-y[id2])*1.0); } double zhuLiu(int root,int nv,int ne) { double res=0; while(1) { REP(i,0,nv) in[i]=inf; REP(i,0,ne){ int u=e[i].fr; int v=e[i].to; if(u!=v&&e[i].w<in[v]) in[v]=e[i].w,pre[v]=u; } REP(i,0,nv) if(i!=root&&in[i]==inf) return -1.0; int nodeCnt=0; memset(vis,-1,sizeof(vis)); memset(ID,-1,sizeof(ID)); in[root]=0; REP(i,0,nv){ res+=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]=nodeCnt; ID[v]=nodeCnt++; } } if(nodeCnt==0) break; REP(i,0,nv) if(ID[i]==-1) ID[i]=nodeCnt++; REP(i,0,ne){ int v=e[i].to; e[i].fr=ID[e[i].fr]; e[i].to=ID[e[i].to]; if(e[i].fr!=e[i].to) e[i].w-=in[v]; } nv=nodeCnt; root=ID[root]; } return res; } int main() { int a,b; while(~scanf("%d%d",&n,&m)) { REP(i,0,n) scanf("%d%d",x+i,y+i); REP(i,0,m){ scanf("%d%d",&a,&b); e[i].fr=a-1,e[i].to=b-1; if(a==b) e[i].w=inf; else e[i].w=dist(a-1,b-1); } double ans=zhuLiu(0,n,m); if(ans==-1.0) printf("poor snoopy "); else printf("%.2f ",ans); } return 0; }