Topic Links:
http://poj.org/problem?id=3164
#include <cmath>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;Const intMAXN = the;Const intN =10010;Const DoubleEXP = 1e-Ten;Const intINF =0x3f3f3f3f;structpoint{Doublex, y; DoubleLength (Point a) {returnsqrt ((a.x-x) * (a.x-x) + (a.y-y) * (a.y-y)); }};structedge{intu, v; DoubleW;}; Point POINT[MAXN]; Edge Edge[n];intN, M;DoubleDirected_mst (introot) { DoublePRE[MAXN], ans =0; intVIS[MAXN], ID[MAXN], PR[MAXN]; intu, v; while(true) { for(intI=1; i<=n; i++) Pre[i]=INF; for(intI=0; i<m; i++) {u=edge[i].u; V=edge[i].v; if(Edge[i].w<pre[v] && u!=v) {Pre[v]=EDGE[I].W; PR[V]=u; } } for(intI=1; i<=n; i++) { if(i = =root)Continue; if(Pre[i] = =INF)return-1; } memset (Vis,-1,sizeof(VIS)); memset (ID,-1,sizeof(ID)); intCru =1; Pre[root]=0; for(intI=1; i<=n; i++) {ans+=Pre[i]; V=i; while(Vis[v]!=i && id[v]==-1&& v!=root) {Vis[v]=i; V=Pr[v]; } if(V!=root && id[v]==-1) { for(u=pr[v]; u!=v; u=Pr[u]) Id[u]=Cru; Id[u]= Cru + +; } } if(Cru = =1) Break; for(intI=1; i<=n; i++) if(Id[i] = =-1) Id[i]= Cru + +; for(intI=0; i<m; i++) {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-=Pre[v]; } N= Cru-1; Root=Id[root]; //printf ("*****%.2f\n", ans); } returnans;}intMain () { while(SCANF ("%d%d", &n, &m)! =EOF) { for(intI=1; i<=n; i++) scanf ("%LF%LF", &point[i].x, &point[i].y); for(intI=0; i<m; i++) { intu, v; scanf ("%d%d", &u, &v); EDGE[I].U=u; EDGE[I].V=v; if(U = =v) EDGE[I].W=INF; ElseEDGE[I].W=point[u].length (Point[v]); } Doublenum = Directed_mst (1); if(num+1<Exp) printf ("Poor snoopy\n"); Elseprintf ("%.2f\n", num); } return 0;}
POJ 3164 Command Network