POJ 3164 Command Network (最小树形图)
来源:互联网 发布:oracle创建数据库语句 编辑:程序博客网 时间:2024/06/15 09:33
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 Mlines 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
Source
题意:
确定根的最小树形图模版题。
#include <iostream>#include <cmath>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#include <string>typedef long long LL;using namespace std;const int maxn = 222;const int maxm = 222*222;const double inf = 999999999;int n,m;struct node{ double x,y;}d[maxn];struct edge{ int u,v; double w;}len[maxm];double zhuliu(int root){ double in[maxn]; int pre[maxn]; double res=0; while(1){ for(int i=1;i<=n;i++) in[i]=inf; for(int i=1;i<=m;i++) { if(len[i].u==len[i].v) continue; if(in[len[i].v]>len[i].w) { pre[len[i].v]=len[i].u; in[len[i].v]=len[i].w; } } in[root]=0; for(int i=1;i<=n;i++) if(in[i]==inf) return -1; int tn=0; int id[maxn]; int vis[maxn]; memset(id,-1,sizeof id);memset(vis,-1,sizeof vis); for(int i=1;i<=n;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) { tn++; for(int u=pre[v];u!=v;u=pre[u]) { id[u]=tn; } id[v]=tn; } } if(tn==0) break; for(int i=1;i<=n;i++) { if(id[i]==-1) id[i]=++tn; } for(int i=1;i<=m;) { int v=len[i].v; len[i].v=id[len[i].v]; len[i].u=id[len[i].u]; if(len[i].u!=len[i].v) { len[i++].w-=in[v]; } else swap(len[i],len[m--]); } n=tn; root=id[root]; } return res;}int main(){ while(~scanf("%d %d",&n,&m)) { for(int i=1;i<=n;i++) scanf("%lf %lf",&d[i].x,&d[i].y); for(int i=1;i<=m;i++) { int u,v;scanf("%d %d",&u,&v); len[i].u=u,len[i].v=v; double x=fabs(d[u].x-d[v].x); double y=fabs(d[u].y-d[v].y); len[i].w=sqrt(x*x+y*y); } double ans=zhuliu(1); if(ans==-1) printf("poor snoopy\n"); else printf("%.2lf\n",ans); }}
- 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 3164 Command Network【最小树形图】
- POJ 3164 Command Network(最小树形图)
- 【poj 3164】Command Network 最小树形图
- Command Network poj 3164 (最小树形图)
- poj 3164 Command Network【最小树形图】
- POJ 3164 Command Network(最小树形图模板)
- POJ 题目3164 Command Network(最小树形图)
- POJ 3164 Command Network(最小树形图)
- POJ 3164 Command Network(最小树形图)
- 小工具整理
- 设计模式(二)——工厂设计模式(1)
- javaweb刷新和不缓存
- 1086: ASCII码排序(多实例测试)
- Java中ArrayList remove会遇到的坑
- POJ 3164 Command Network (最小树形图)
- pg_dump导出psql导入举例
- 数据结构第二周项目3(1)--体验复杂度之两种排序算法的运行时间
- 前端分页案例
- POJ 1144 Network (无向图求割点)
- KMP算法查找相同字符串
- 第二周项目--函数参数传值的几种方式
- js-return ture to win
- 算法提高 ADV-179 解二元一次方程组