【poj 3164】Command Network 最小树形图
2016-06-01 15:17
323 查看
题目:http://poj.org/problem?id=3164
Command Network
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 16037 Accepted: 4623
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
思路:
可参考http://blog.csdn.net/shuangde800/article/details/8039359
即: *自边无用,删;
1.判断图的连通性,若不连通直接无解,否则一定有解。
2.所有节点找最短的入边,记录pre【】,minn【】,易得,如无环则为答案;
3.如有环,缩点后ans先+=edge【环内边】;edge【到缩点】的边-=minn【x】,如图;(*自边无用,删);
4.重复,至无环,break
代码:
Command Network
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 16037 Accepted: 4623
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
思路:
可参考http://blog.csdn.net/shuangde800/article/details/8039359
即: *自边无用,删;
1.判断图的连通性,若不连通直接无解,否则一定有解。
2.所有节点找最短的入边,记录pre【】,minn【】,易得,如无环则为答案;
3.如有环,缩点后ans先+=edge【环内边】;edge【到缩点】的边-=minn【x】,如图;(*自边无用,删);
4.重复,至无环,break
代码:
#include<iostream> #include<stdio.h> #include<string.h> #include<math.h> using namespace std; int n,m; struct node{ double x,y; }p[1005]; struct edge{ int x,y; double v; }q[1005*105]; double in[1005]; int pre[1005]; int vis[1005],id[1005]; double inf=99999999.9; double solve() { double ret=0.0; int nn=n; int rt=1; while(1) { for(int i=1;i<=nn;i++) in[i]=inf; for(int i=1;i<=m;i++) { if(q[i].x!=q[i].y&&in[q[i].y]>q[i].v) { pre[q[i].y]=q[i].x; in[q[i].y]=q[i].v; } } for(int i=1;i<=nn;i++) { if(i==rt) continue; if(in[i]==inf) return -1; } int cnt=0; memset(id,-1,sizeof(id)); memset(vis,-1,sizeof(vis)); in[rt]=0; for(int i=1;i<=nn;i++) { ret+=in[i]; int now=i; while(now!=rt&&vis[now]!=i&&id[now]==-1) { vis[now]=i; now=pre[now]; } if(now!=rt&&id[now]==-1) { cnt++; for(int nex=pre[now];nex!=now;nex=pre[nex]) id[nex]=cnt; id[now]=cnt; } } if(cnt==0) break; for(int i=1;i<=nn;i++) if(id[i]==-1) id[i]=++cnt; for(int i=1;i<=m;i++) { int xx=q[i].x; int yy=q[i].y; q[i].x=id[xx]; q[i].y=id[yy]; if(id[xx]!=id[yy]) q[i].v-=in[yy]; } nn=cnt; rt=id[rt]; } return ret; } double dis(node a, node b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } int main() { while(scanf("%d%d",&n,&m)!=EOF) { for(int i=1;i<=n;i++) scanf("%lf%lf",&p[i].x,&p[i].y); for(int i=1;i<=m;i++) { scanf("%d%d",&q[i].x,&q[i].y); if(q[i].x!=q[i].y) q[i].v=dis(p[q[i].x],p[q[i].y]); else q[i].v=inf; } double ans=solve(); if(ans==-1) printf("poor snoopy\n"); else printf("%.2f\n", ans); } }
相关文章推荐
- java基础第十四天_IO
- Javascript apply与call函数详解
- zeroc-ice网络请求使用介绍
- 函数
- jQuery.on() 函数详解[http://www.365mini.com/page/jquery-on.htm]
- jquery的跳转.禁止全url跳转.只需控制器+方法
- Android 编译命令 make j8 2>&1 | tee build.log 解释
- 移位运算符(JAVA)
- 面向对象基础
- 分享一个插件实现水珠自动下落效果
- C# .NET基础知识点
- Maven 手动添加 JAR 包到本地仓库
- SPDY、HTTP/2、QUIC协议
- mybatis的简单使用
- 循环获取eq选择器的值
- Core Data 学习: 数据模型(Data Model)
- OpenglES2.0 for Android:来画个矩形吧
- 反射
- " "和null的区别
- 二分查找和斐波那契查找优化