Link:
http://poj.org/problem?id=3164
Topic:
Command Network
Time limit:1000ms |
|
Memory limit:131072k |
Total submissions:8922 |
|
accepted:2609 |
Description After a long lasting the war on words, a war in arms finally breaks out between Littleken ' s and Knuthocean ' s kingdoms. A sudden and violent assault by Knuthocean's force have 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, and the most urgent point was to enable Littenken ' s commands t o Reach every disconnected node in the destroyed network and decides on a plan to build a unidirectional communication net Work. The nodes is distributed on a plane. If Littleken ' s commands is to being able to being delivered directly from a node-to-another node B, a wire would has to be BU Ilt along the straight line segment connecting the nodes. Since It's in wartime, not between all pairs of the nodes can wires be built. Snoopy wants the plan to require, the shortest total, length of wires so, the construction can is done very soon. Input The input contains several test cases. Each test case is starts with a line containing the integer n (n ≤100) and the number of nodes in the destroy Ed 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 TH E nodes. Then follow M lines each containing II integers i and J between 1 and N(inclusive) Meaning a wire can is built between node I and node J for unidirectional command delivery from the F Ormer to the latter. Littleken ' s headquarter is all 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 the digits past the decimal p Oint. In the cases this 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.19poor Snoopy
Source POJ monthly--2006.12.31, Galaxy |
#include <iostream>#include<cstdio>#include<sstream>#include<cstring>#include<map>#include<Set>#include<vector>#include<stack>#include<queue>#include<algorithm>#include<cmath>#defineRap (i, A, n) for (int i=a; i<=n; i++)#defineMOD 2018#defineLL Long Long#defineULL unsigned long Long#definePair Pair<int, int>#defineMem (A, B) memset (A, B, sizeof (a))#define_ Ios_base::sync_with_stdio (0), Cin.tie (0)//freopen ("1.txt", "R", stdin);using namespacestd;Const intMAXN =10010, INF =0x7fffffff;intN, M;intVIS[MAXN], INC[MAXN], PRE[MAXN];Doublew[ the][ the];structedge{intx, y;} EDGE[MAXN];voidDfsintu) {Vis[u]=1; for(intI=1; i<=n; i++) if(!vis[i] && W[u][i] <INF) Dfs (i);}DoubleDirmst (intu) { DoubleAns =0; //= = Step 1: Determine whether a minimum tree diagram can be formed, direct DFS traversal (that is, checking if one is able to connect)dfs (U); for(intI=1; i<=n; i++) if(!Vis[i])return-1; //= = If you can form a minimal tree diagram, continueMem (Vis,0); while(true) { //= = 1. Find the smallest precursor edge for(intI=1; i<=n; i++)if(I! = u &&!)Inc[i]) {W[i][i]= INF; Pre[i] =i; for(intj=1; j<=n; J + +)if(!inc[j] && W[j][i] <W[pre[i]][i]) pre[i]=J; } //= = 2. Determine if there is a ring inti; for(i=1; i<=n; i++)if(I! = u &&!)Inc[i]) { intj = i, cnt =0; while(J! = u && pre[j]! = i && cnt <= n) j = Pre[j], + +CNT; if(j = = u | | cnt > N)Continue; Break; } //= = did not find the ring, get the answer if(I >N) { for(intI=1; i<=n; i++)if(I! = u &&!inc[i]) ans + =W[pre[i]][i]; returnans; } //= = Ring, then shrink the ring intj =i; Mem (Vis,0); Do{ans+ = W[pre[j]][j], j = pre[j], vis[j] = inc[j] =true; } while(J! =i); Inc[i]=false;//the ring shrinks into point I, the point I still exists for(intk=1; k<=n; k++)if(Vis[k]) {//points in the ring for(intj=1; j<=n; J + +)if(!vis[j]) {//points not in the ring if(W[i][j] > W[k][j]) w[i][j] = W[k][j];//update the out edge of the ring if(W[j][k] < INF && W[j][k]-w[pre[k]][k] < W[j][i])//update a swapped in EdgeW[j][i] = w[j][k]-W[pre[k]][k]; } } } returnans;}voidinit () {mem (Vis,0); Mem (inc,0); Rap (i,0, N) Rap (j, I, N) w[i][j]= W[j][i] =INF;}intMain () { while(~SCANF ("%d%d", &n, &m)) {init (); Rap (i,1, N) {scanf ("%d%d", &edge[i].x, &edge[i].y); } rap (I,1, M) { intA, B; scanf ("%d%d", &a, &b); Doublec = sqrt ((Double) (edge[a].x-edge[b].x) * (edge[a].x-edge[b].x) + (Double) (EDGE[A].Y-EDGE[B].Y) * (EDGE[A].Y-edge[b].y)); if(W[a][b] >c) W[a][b]=C; } DoubleAns = dirmst (1); if(Ans <0) puts ("Poor Snoopy"); Elseprintf"%.2f\n", ans); } return 0;}
Command Network openj_bailian-3436 (minimum to Spanning tree template problem)