Command Network
Time Limit: 1000MS |
|
Memory Limit: 131072K |
Total Submissions: 14782 |
|
Accepted: 4249 |
Description
After a long lasting war in words, a war on 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 Former 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
Test instructions: give you the coordinates of the n point and the M-bar have a forward edge, ask you to point 1 as the root of the smallest tree diagram of the sum of the Benquan.
Entry question: Because of committing two points that part to the record ring for the loop inside, find a bug for a long time.
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm > #define MAXN 110#define maxm 20000+10#define INF 0x3f3f3f3fusing namespace std;struct edge{int from, to; double cost;}; Edge edge[maxm];int pre[maxn];int vis[maxn];int id[maxn];d ouble in[maxn];int N, m;struct node{double x, y;}; Node NUM[MAXN];d ouble Dis (Node A, Node B) {return sqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y));} void Getmap () {for (int i = 0; i < N; i++) scanf ("%lf%lf", &num[i].x, &NUM[I].Y); int A, B; for (int i = 0; i < M; i++) {scanf ("%d%d", &a, &b); a--, b--; Edge[i].from = A; Edge[i].to = b; Edge[i].cost = a==b? Inf:dis (Num[a], num[b]);//remove itself from the ring itself to infinity}}double zhuliu (int root, int n, int m, Edge *edge) {Double res = 0; int u, v; while (1) {for (int i = 0; i < n; i++) in[i] = INF; for (int i = 0; i < m; i++) { Edge E = Edge[i]; if (e.from! = e.to && e.cost < in[e.to]) {pre[e.to] = E.from; In[e.to] = E.cost; }} for (int i = 0; i < n; i++) if (i! = root && in[i] = = INF) return-1; int tn = 0; memset (ID,-1, sizeof (ID)); memset (Vis,-1, sizeof (VIS)); In[root] = 0; for (int i = 0; i < n; i++) {res + = In[i]; v = i; while (vis[v] = i && id[v] = = 1 && v! = root) {Vis[v] = i; v = pre[v]; if (v! = root && id[v] = =-1) {for (U = pre[v]; u = v; u = pre[u]) Id[u] = TN; ID[V] = tn++; }} if (tn = = 0) break; for (int i = 0; i < n; i++) if (id[i] = = 1) id[i] = tn++; for (int i = 0; I &lT M ) {v = edge[i].to; Edge[i].from = Id[edge[i].from]; Edge[i].to = id[edge[i].to]; if (edge[i].from! = edge[i].to) Edge[i++].cost-= In[v]; else swap (Edge[i], edge[--m]); } n = tn; root = Id[root]; } return res; int main () {while (scanf ("%d%d", &n, &m)! = EOF) {getmap (); Double ans = zhuliu (0, N, M, Edge); if (ans = =-1) printf ("Poor snoopy\n"); else printf ("%.2lf\n", ans); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 3164 Command Network "minimum tree Diagram" "Zhu Liu Algorithm Introduction"