POJ 3164Command Network
来源:互联网 发布:php环境配置教程 百度 编辑:程序博客网 时间:2024/05/16 15:48
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 60 64 60 07 201 21 32 33 43 13 24 30 01 00 11 21 34 12 3
Sample Output
31.19poor snoopy
最小树形图,朱刘算法模板。
#include<cmath>#include<vector>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define rep(i,j,k) for(int i=j;i<=k;i++)#define loop(i,j,k) for (int i=j;i!=-1;i=k[i])#define inone(x) scanf("%d",&x)#define intwo(x,y) scanf("%d%d",&x,&y)#define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z)const int N = 1e5+ 10;const double INF = 1e18;int n, m, pre[N], vis[N], f[N];double x[N], y[N], ans;double in[N];struct point{int x, y;double d;}a[N];double dis(int i, int j){return sqrt((x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j]));}double zhuliu(int rt){double res = 0;while (true){rep(i, 1, n) in[i] = INF;rep(i, 1, m){int x = a[i].x, y = a[i].y;if (a[i].d < in[y] && x != y){pre[y] = x; in[y] = a[i].d;}}rep(i, 1, n){if (i == rt) in[rt] = 0;if (in[i] == INF) return -1;}int cnt = 1;rep(i, 1, n) vis[i] = f[i] = -1;rep(i, 1, n){res += in[i];int j = i;while (j != rt&&vis[j] != i&&f[j] == -1){vis[j] = i; j = pre[j];}if (j != rt&&f[j] == -1){for (int k = pre[j]; k != j; k = pre[k]) f[k] = cnt;f[j] = cnt++;}}if (cnt == 1) break;rep(i, 1, n) if (f[i] == -1) f[i] = cnt++;rep(i, 1, m){int x = a[i].x, y = a[i].y;a[i].x = f[x]; a[i].y = f[y];if (f[x] != f[y]) a[i].d -= in[y];}n = cnt - 1; rt = f[rt];}return res;}int main(){while (intwo(n, m) != EOF){rep(i, 1, n) scanf("%lf%lf", &x[i], &y[i]);rep(i, 1, m){intwo(a[i].x, a[i].y);a[i].d = a[i].x - a[i].y ? dis(a[i].x, a[i].y) : INF;}if ((ans = zhuliu(1)) == -1) printf("poor snoopy\n");else printf("%.2lf\n", ans);}return 0;}
- POJ 3164 Command Network
- POJ 3164 Command Network
- POJ 3164 Command Network
- poj 3164 Command Network
- POJ 3164 Command Network
- poj 3164 Command Network
- POJ 3164 Command Network
- POJ 3164 Command Network
- poj 3164 Command Network
- POJ 3164Command Network
- POJ 3164 Command Network
- POJ 3164 Command Network 笔记
- poj 3164 Command Network 最小树形图
- POJ 3164 Command Network 最小树形图
- 最小树形图 【poj 3164 Command Network】
- POJ 3164 Command Network (最小树形图)
- poj 3164 Command Network 最小树形图
- Command Network - POJ 3164 朱刘算法
- C语言一些难点
- orcle打开编辑器
- (复习)[LeetCode]Single Number II
- web.xml中的contextConfigLocation在spring中的作用
- 43个优秀的Swift开源项目
- POJ 3164Command Network
- OC/Swift 实现的功能强大的界面布局框架
- 欢迎使用CSDN-markdown编辑器
- 第十四周5
- spring @component的作用
- 排序和统计记数
- 多态的强制转换
- Elasticsearch CURL操作方法
- Android开发艺术探索笔记(7)- IPC方式