Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 18372 | Accepted: 5262 |
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<stdio.h> #include<cstring> #include<cmath> #define maxn 105 #define inf 2000000000 #define clear(a) memset(a,-1,sizeof(a)) using namespace std; int n,m,root=1,which,cnt; int pre[maxn],vis[maxn],id[maxn]; double in[maxn]; struct point{ double x,y; }coordinate[maxn]; double calculate(point a,point b){ return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } struct edge{ int u,v; double w; }e[maxn*maxn]; inline void init(){ clear(vis),clear(id),cnt=1; } inline double Zhu_Liu_Algortithm(int root,int n,int m){ double ans=0; while(true){ for(int i=1;i<=n;i++) in[i]=inf; in[root]=0; for(int i=1;i<=m;i++){//统计入边 int u=e[i].u,v=e[i].v;//从u到v if(e[i].w<in[v]&&u!=v)//缩点后有可能u==v in[v]=e[i].w,pre[v]=u; } for(int i=1;i<=n;i++) { if(i==root) continue; if(in[i]==inf) return -1; } init(); //找环 for(int i=1;i<=n;i++){ ans+=in[i];//我所选择的边的权值 which=i; while(vis[which]!=i&&id[which]==-1&&which!=root){ vis[which]=i; which=pre[which]; } if(which!=root&&id[which]==-1){ for(int u=pre[which];u!=which;u=pre[u]) id[u]=cnt; id[which]=cnt++; } } if(cnt==1) break; //无环的时候就可以退出了,此时就是最小树形图 //建立新图 for(int i=1;i<=n;i++) if(id[i]==-1) id[i]=cnt++;//每个不在环上的点都是独立的新点 for(int i=1;i<=m;i++){ 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]; } n=cnt-1; root=id[root]; } return ans; } int main(){ while(scanf("%d%d",&n,&m)!=EOF){ for(int i=1;i<=n;i++) scanf("%lf%lf",&coordinate[i].x,&coordinate[i].y); for(int i=1;i<=m;i++){ scanf("%d%d",&e[i].u,&e[i].v); if(e[i].u!=e[i].v) e[i].w=calculate(coordinate[e[i].u],coordinate[e[i].v]); else e[i].w=inf; } double The_Final_Problem=Zhu_Liu_Algortithm(1,n,m); if(The_Final_Problem==-1) printf("poor snoopy\n"); else printf("%.2f\n",The_Final_Problem); } }