最小树形图poj3164 Command Network
来源:互联网 发布:淘宝差评师怎么找 编辑:程序博客网 时间:2024/06/16 00:32
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<cstdio>#include<algorithm>#include<cstring>#include<cmath>#include <iostream>using namespace std;const int M=10005,N=105,inf=1e9;//最小树形图:选择一些边,使得根节点能够到达图中所有的节点,并使得选出的边的边权和最小 (有向图,所有不可用最小生成树) struct Edge{ int u, v;double w;} edge[M];int n, m;int id[N],vis[N],pre[N];double in[N];double Directed_MST(int root) {//如果ret<0,则不存在最小树形图 double ret = 0; while(true) { for(int i=0;i<n;i++) in[i]=inf; for(int i=0;i<m;i++){ //找最小入边 int u = edge[i].u; int v = edge[i].v; if(edge[i].w < in[v] && u != v) { in[v] = edge[i].w; pre[v] = u; } } for(int i=0;i<n;i++) { //如果存在除root以外的孤立点,则不存在最小树形图 if(i == root) continue; if(in[i] == inf) { return -1;} } int cnt = 0; //顶点从0开始编号 memset(vis,-1,sizeof(vis)); memset(id,-1,sizeof(id)); in[root] = 0; for(int i=0;i<n;i++) { //找环 ret += 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<n;i++) if(id[i] == -1) id[i] = cnt++; //重新标号 for(int i=0;i<m;i++){ //更新其他点到环的距离 int v =edge[i].v; edge[i].u = id[edge[i].u]; edge[i].v = id[edge[i].v]; if(edge[i].u != edge[i].v) edge[i].w -= in[v]; } n = cnt; root = id[root]; } return ret;}double x[105],y[105];inline double dist(int i,int j){return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));}int main(){while(~scanf("%d%d",&n,&m)){for(int i=0;i<n;++i)scanf("%lf%lf",x+i,y+i);for(int i=0;i<m;++i){int u,v;scanf("%d%d",&u,&v);u--,v--;edge[i].u=u,edge[i].v=v,edge[i].w=dist(u,v);}double ans=Directed_MST(0);if(ans>=0)printf("%.2f\n",ans);else puts("poor snoopy");}return 0;}
- POJ3164 Command Network 【最小树形图】
- poj3164 Command Network(最小树形图)
- POJ3164 Command Network(最小树形图)
- 最小树形图poj3164 Command Network
- poj3164 Command Network 最小树形图-朱刘算法
- POJ3164 Command Network【最小树形图】【朱刘算法】
- 【POJ3164】Command Network 最小树形图模板题 重修版
- 【POJ3164】Command Network 最小树形图 朱刘算法
- poj3164 Command Network(最小树形图模板题)
- 最小树形图poj3164
- POJ3164-最小树形图
- POJ3164 最小树形图
- poj3164+最小树形图
- 最小树形图(poj3164)
- POJ3164:Command Network
- POJ3164--Command Network
- POJ3164---Command Network
- POJ3164-Command Network
- 整理篇:C++之标准输入
- Spring 学习笔记(六)—— AOP的简单理解
- webservice综合案例(四)
- U8数据字典生成表及字段备注
- ARC下OC对象和CF对象之间的桥接(bridge)
- 最小树形图poj3164 Command Network
- Ubuntu 12.04 or 14.04 下USB存储设备自动挂载和自动打开功能的开启和关闭
- CASE WHEN 及 SELECT CASE WHEN的用法
- ListVIew子项布局右对齐,TextVIew右对齐
- Linux 系统安装与软件配置相关
- 解决cv2.findContours返回值too many values to unpack (expected 2)的问题
- android学习之RecyclerView的简单使用
- elasticsearchException:Failed to load logging configuration Caused by: java.nio.file.NoSuchFileExcep
- ByteBuffer用法小结