POJ 3164 Command Network 最小树形图
2016-03-06 23:07
363 查看
裸题
Total Submissions: 15695 Accepted: 4499
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.
#include <cstdio> #include <cstring> #include <cmath> #define ms(a,b) memset(a,b,sizeof(a)) #define FOR(i,j,k) for(int i=j;i<=k;++i) const int N = 105, M = 10001; int u[M], v[M], id , vis , fa , n, m; double x , y , w[M], in ; double edmonds(int rt, int n, int m) { int i, a, b, cnt; double ans = 0; while (1) { FOR(i,1,n) in[i] = 1e18; FOR(i,1,m) if (w[i] < in[v[i]] && u[i] != v[i]) in[v[i]] = w[i], fa[v[i]] = u[i]; FOR(i,1,n) if (i != rt && in[i] == 1e18) return -1; ms(id, 0); ms(vis, 0); in[rt] = 0; cnt = 0; FOR(i,1,n) { ans += in[i]; for (b = i; vis[b] != i && !id[b] && b != rt; b = fa[b]) vis[b] = i; if (b != rt && !id[b]) { ++cnt; for (a = fa[b]; a != b; a = fa[a]) id[a] = cnt; id[b] = cnt; } } if (!cnt) break; FOR(i,1,n) if (!id[i]) id[i] = ++cnt; FOR(i,1,m) { int x = v[i]; u[i] = id[u[i]]; v[i] = id[v[i]]; if (u[i] != v[i]) w[i] -= in[x]; } n = cnt; rt = id[rt]; } return ans; } int main() { while (scanf("%d%d", &n, &m) != EOF) { FOR(i,1,n) scanf("%lf%lf", &x[i], &y[i]); FOR(i,1,m) { scanf("%d%d", &u[i], &v[i]); if (u[i] != v[i]) w[i] = sqrt((x[u[i]]-x[v[i]])*(x[u[i]]-x[v[i]])+(y[u[i]]-y[v[i]])*(y[u[i]]-y[v[i]])); else w[i] = 1e18; } double ans = edmonds(1, n, m); if (ans == -1) puts("poor snoopy"); else printf("%.2f\n", ans); } return 0; }
Command Network
Time Limit: 1000MS Memory Limit: 131072KTotal Submissions: 15695 Accepted: 4499
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
相关文章推荐
- HTML DOCTYPE
- Net 高效开发
- VS2012+MySQL+EF5.0(DBFrist)开发指南
- Cookie与Session的简单讨论
- iOS音频播放(三):AudioFileStream
- 软件测试作业1--描述Error
- Ubuntu 14.04安装教程
- 面经阅读
- 同一台电脑设置多个公钥与不同GITHUB帐号交互
- 严重: Failed to initialize end point associated with ProtocolHandler ["http-bio-8080"]
- Missing mac app distribution signing identity
- C#编程实现连接ACCESS数据库实例详解
- 数据结构之一元多项式相加
- EFI基本概念之Event
- 【转载】聚类算法小结
- 十种常见的排序算法总结(java版)
- java 泛型的理解(一)
- 深入浅出看流媒体前世今生,分分钟二逼变牛逼
- 学习java的一点心得(二)
- spring配置整理