1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| #include <iostream> #include <algorithm> #include <math.h> #include <string.h> using namespace std; const int maxn = 205; const double inf = 1.0 * 0x3f3f3f3f; struct point { double x, y; }p[maxn]; struct node { int u, v; double w; }edge[maxn * maxn]; int pre[maxn], id[maxn], vis[maxn], n, m; double in[maxn]; double getdis(point a,point b) { return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y)); } double mst(int root,int nn,int mm) { double res = 0; while (1) { for (int i = 0; i < nn; i++) in[i] = inf; for (int i = 0; i < mm; i++) { int u = edge[i].u, v = edge[i].v; if (edge[i].w < in[v] && u != v) { pre[v] = u; in[v] = edge[i].w; } } for (int i = 0; i < nn; i++) { if (i == root) continue; if (in[i] == inf) return -1; } int cnt = 0; memset(id,-1,sizeof id); memset(vis,-1,sizeof vis); in[root] = 0; for (int i = 0; i < nn; i++) { res += in[i]; int v = i; while (vis[v]!=i && id[v]==-1 && v!=root) { vis[v] = i; v = pre[v]; } if (v != root && id[v] == -1) { for (int u = pre[v]; u != v; u = pre[u]) id[u] = cnt; id[v] = cnt++; } } if (cnt == 0) break; for (int i = 0; i < nn; i++) if (id[i] == -1) id[i] = cnt++; for (int i = 0; i < mm; i++) { int 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 -= in[v]; } nn = cnt; root = id[root]; } return res; } int main() {
while (scanf("%d%d",&n,&m) != EOF) { for (int i = 0; i < n; i++) scanf("%lf%lf",&p[i].x,&p[i].y); for (int i = 0; i < m; i++) { scanf("%d%d",&edge[i].u,&edge[i].v); edge[i].u--; edge[i].v--; if (edge[i].u != edge[i].v) edge[i].w = getdis(p[edge[i].u], p[edge[i].v]); else edge[i].w = inf; } double ans = mst(0,n,m); if (ans == -1) printf("poor snoopy\n"); else printf("%.2f\n",ans); } return 0; }
|