Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 10637 | Accepted: 3101 |
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
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<set> #include<queue> #include<string> #include<cmath> #include<fstream> #include<iomanip> #include<climits> #include<cfloat> using namespace std; #define MAX_INT 0x7fffffff #define MAX_LL 0x7fffffffffffffff #define ULL unsigned long long #define LL long long #define MAX(x,y) ((x) > (y) ? (x) : (y)) #define MIN(x,y) ((x) < (y) ? (x) : (y)) #define MAXN 111 #define MAXM 11111 #define INF DBL_MAX double w[MAXN][MAXN],ans; int n,m; int x[MAXN],y[MAXN]; int maxid,cid[MAXN]; int pre[MAXN]; double inv[MAXN]; bool vis[MAXN],removed[MAXN]; int dfs(int s){ int ans=1; vis[s]=true; for(int i=1; i<=n; i++) if(!vis[i] && w[s][i]<INF) ans+=dfs(i); return ans; } void update(int s){ inv[s]=INF; for(int i=1; i<=n; i++) if(w[i][s]<inv[s] && !removed[i]){ inv[s]=w[i][s]; pre[s]=i; //这里不记得记录父亲节点,WA了N次 } } bool cycle(int s){ maxid++; int u=s; while(maxid!=cid[u]) { cid[u]=maxid; u=pre[u];} return u==s; } double dmst(int s){ int i,j; memset(vis, 0, sizeof(vis)); if(dfs(s)<n) return false; memset(removed, 0, sizeof(removed)); memset(cid, 0, sizeof(cid)); for(int i=1; i<=n; i++) update(i); pre[s]=s; inv[s]=0; ans=0; maxid=0; while(1){ bool havecycle=false; for(int i=1; i<=n; i++) if(!removed[i] && i!=s && cycle(i)){ havecycle=true; int u=i; do{ if(u!=i) removed[u]=true; ans+=inv[u]; for(int v=1; v<=n; v++) if(!removed[v] && cid[v]!=cid[i]){ if(w[v][u]<INF) w[v][i]=MIN(w[v][i],w[v][u]-inv[u]); w[i][v]=MIN(w[i][v],w[u][v]); if(pre[v]==u) pre[v]=i; } u=pre[u]; }while(u!=i); update(i); break; } if(!havecycle) break; } for(int i=1; i<=n; i++) if(!removed[i]) ans+=inv[i]; return true; } int main(){ //freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin); while(scanf(" %d %d",&n,&m)==2){ int i,j; for(i=1; i<=n; i++){ scanf(" %d %d",&x[i],&y[i]); } int u,v; double w_; for(i=1; i<=n; i++) for(j=1; j<=n; j++) w[i][j]=INF; for(i=0; i<m; i++){ scanf(" %d %d",&u,&v); w_=sqrt((x[u]-x[v])*(x[u]-x[v])+ (y[u]-y[v])*(y[u]-y[v])); if(u!=v) w[u][v]=MIN(w_, w[u][v]); //去重边和自环 } if(dmst(1)) printf("%.2f ",ans); else printf("poor snoopy "); } return 0; }