【POJ3164】【有向图的最小生成树】【自己的模板】
来源:互联网 发布:大数据培训好就业吗 编辑:程序博客网 时间:2024/06/16 06:23
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
Source
#include <iostream>#include <cstring>#include <cmath>#include <queue>#include <stack>#include <list>#include <map>#include <string>#include <cstdlib>#include <cstdio>#include <algorithm>using namespace std;#define rep(i,a,n) for (int i=a;i<n;i++)#define per(i,a,n) for (int i=n-1;i>=a;i--)#define mp push_backint n,m;struct Point{double x,y;}P[110];struct Edge{int a,b;double c;}edge[10010];double dis(Point a,Point b){return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));}double d[110];int p[110];int vis[110];int id[110];double zhuli(int root,int n,int m,Edge edge[]){double res = 0;while(true){for(int i=0;i<n;i++){d[i] = 0x3f3f3f3f;p[i] = -1;}for(int i=0;i<m;i++){Edge e = edge[i];if(d[e.b] > e.c){d[e.b] = e.c;p[e.b] = e.a;}}for(int i=0;i<n;i++){if(i != root && p[i] == -1) return 0x3f3f3f3f;}int cnt = 0;for(int i=0;i<n;i++){id[i] = -1;vis[i] = -1;}d[root] = 0;for(int i=0;i<n;i++){//if(i != root) res += d[i];int v = i;while(v!= -1 && vis[v] == -1 && v != root) {vis[v] = i;v = p[v];}if(v != root && v != -1 && vis[v] == i){int tt = v;do{id[tt] = cnt;tt = p[tt];}while(tt != 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;){int v = edge[i].b;edge[i].a = id[edge[i].a];edge[i].b = id[edge[i].b];if(edge[i].a != edge[i].b){edge[i++].c -= d[v];}else{//edge[i--] = edge[--m];swap(edge[i],edge[--m]);}}n = cnt;root = id[root];}return res;}int main(){freopen("in.txt","r",stdin);while(scanf("%d%d",&n,&m) != EOF){for(int i=0;i<n;i++){scanf("%lf%lf",&P[i].x,&P[i].y);}for(int i=0;i<m;i++){int u,v;scanf("%d%d",&u,&v);u --;v --;//if(u == v) continue;edge[i].a = u;edge[i].b = v;edge[i].c = dis(P[u],P[v]);if(u == v) edge[i].c = 0x3f3f3f3f;}//cout << "eee" << endl;double ans = zhuli(0,n,m,edge);if(ans == 0x3f3f3f3f){printf("poor snoopy\n");}else{printf("%.2f\n",ans);}}}
- 【POJ3164】【有向图的最小生成树】【自己的模板】
- POJ3164 最小树形图 有向图的最小生成树 模板题 朱刘算法 朱永津-刘振宏算法
- hdu 4009 Transfer water(最小树形图:有向图的最小生成树模板)
- 有向图的最小生成树
- 有向图的最小生成树,最小树形图
- 【UVA11183】【裸朱刘算法】【有向图的最小生成树】
- 【hdu4009】【建模】【有向图的最小生成树】
- POJ 3164 有向图的最小生成树
- 最小树形图的学习也可以叫 有向图的最小生成树
- UVA--- 11183 【有定根的有向图的最小生成树-裸最小树形图】
- 最小树形图 模板(poj3164)
- 最小树形图(有向图的最小生成树)朱刘算法
- hdu 4966 GGS-DDU 最小树形图/有向图的最小生成树
- 最小树形图(有向图的最小生成树)
- [转载]有向图的最小生成树,最小树形图
- 最小树形图(有向图的最小生成树)
- 最小树形图--有向图的最小生成树 poj 3164 Command Network
- 朱刘算法计算有向的最小生成树
- 教你如何迅速秒杀掉:99%的海量数据处理面试题
- ZOJ 3430 Detect the Virus ac自动机 超级精简的转化
- systick介绍
- URAL 1017Staircases dp练习
- typedef和define的用法和区别
- 【POJ3164】【有向图的最小生成树】【自己的模板】
- ios中创建可以拖动的view原理和实现详解(含代码)
- 关于spring依赖注入的一个知识点
- Qt 学习之路 2(1):序 笔记
- UI 18 单例
- github提交可能遇到的问题
- CF 316 E. Pig and Palindromes 求左上角走到右下角是回文的方法数 DP
- android 两次点击back键退出程序
- Block方法与Block属性结合使用的小实现