Command Network

Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 21481   Accepted: 6117

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

unidirectional - > 单向  undirectional -> 双向

此题是要求有向图的最小生成树  - > 用朱刘算法

细节:double ,点的坐标也要用double 不然 Wa   C++ AC   G++ WA

#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <cmath>
#define inf 0x3f3f3f3f
#define Max 101
struct Node{double x, y;
}node[Max];
struct Edge{int u, v;double w;
}edge[Max*Max];
int vis[Max];
int id[Max];//结点所属环编号
double in[Max];
int pre[Max];//in[]为最小入边权,pre[]为其对应的起点
double zhuLiu(int root, int n, int m){//root结点、点数、边数double res = 0;//最小树形图总权值while(true){for(int i = 0; i < n; i++)//初始化为无穷大in[i] = inf;//寻找每个点的最小入边for(int i = 0; i < m; i++){//遍历每条边int x = edge[i].u;int y = edge[i].v;if(edge[i].w < in[y] && x != y){//更新最小入边pre[y] = x;//记录前驱in[y] = edge[i].w;//更新}}//判断是否存在最小树形图for(int i = 0; i < n; i++){if(i == root)continue;if(in[i] == inf)//除根节点外的点存在孤立点return -1;}//寻找所有的环int cnt = 0;//记录环数in[root] = 0;memset(id, -1, sizeof(id));memset(vis, -1, sizeof(vis));for(int i = 0; i < n; i++){//标记每个环res += in[i];//记录权值int y = i;while(vis[y] != i && id[y] == -1 && y != root){//寻找图中有向环//三种情况会终止:找到出现同样标记的点、结点已属其他环、遍历到根vis[y] = i;//标记y = pre[y];//向上找}if(y != root && id[y] == -1){//没有遍历到根或没有找到结点属于其他环,说明找到有向环for(int x = pre[y]; x != y; x = pre[x])//标记结点x为第几个环id[x] = cnt;//记录结点所属环号id[y] = 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 x = edge[i].u;int y = edge[i].v;edge[i].u = id[x];edge[i].v = id[y];if(id[x] != id[y])//两点不在同一环内,更新边权值edge[i].w -= in[y];//x到y的距离为边权-in[y]}n = cnt;//以环数为下次操作的点数,继续上述操作,直到无环root = id[root];}return res;
}
// int main(){
//     int n,m;//n个点m条有向边
//     scanf("%d%d",&n,&m);
//     for(int i=0;i<m;i++){//建图
//         scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].w);
//         if(edge[i].x==edge[i].y)//除去自环,即点到自身距离为INF
//             edge[i].w=INF;
//     }
//     int res=zhuLiu(0,n,m);
//     if(res==-1)
//         printf("No\n");
//     else
//         printf("%d\n",res);
//     return 0;
// }
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() {int n, m, u, v;while(scanf("%d %d",&n, &m) != EOF) {for(int i = 0; i < n; i++) {scanf("%lf %lf",&node[i].x, &node[i].y);}for(int i = 0; i < m; i++) {scanf("%d %d", &u, &v);u--;v--;edge[i].u = u; edge[i].v = v;edge[i].w = dis(node[u], node[v]);if(edge[i].u == edge[i].v) {edge[i].w = inf*1.0;}}double ans = zhuLiu(0, n, m);if(ans == -1) printf("poor snoopy\n");else printf("%.2lf\n",ans);}return 0;
}

POJ 3164 Command Network(朱刘算法)相关推荐

  1. POJ-3164 Command Network (朱刘算法)

    这是一道最小树形图的模板题 朱刘算法开始时的确不是太好理解,在网上看了好多文章才差不多理解. 在这里说一点,缩点时,如果弧(u,v)的v点在一个环中,这个环形成的缩点在新图中的编号是k,那么新图中(u ...

  2. POj 3164 Command Network最小树形图 模板题 朱刘算法

    Command Network After a long lasting war on words, a war on arms finally breaks out between littleke ...

  3. [POJ 3164]Command Network(最小树形图,朱刘算法)

    文章目录 title solution code title solution 读完翻译后,很明显就是个朱刘算法的板子题 最小树形图,就是给出一个带权有向图 从中指定一个特殊的结点 root 求一棵以 ...

  4. POJ 3164 Command Network (最小树形图)

    Command Network Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 10136   Accepted: 2946 ...

  5. POJ 3164 Command Network (最小树形图)

    [题目链接]http://poj.org/problem?id=3164 [解题思路]百度百科:最小树形图 ]里面有详细的解释,而Notonlysucess有精简的模板,下文有对其模板的一点解释,前提 ...

  6. POJ 3164 Command Network

    Description After a long lasting war on words, a war on arms finally breaks out between littleken's ...

  7. poj 3164 Command Network

    最小树形图,,不熟练啊,写错几个地方无限TLE #include<stdio.h> #include<string.h> #include<math.h> #def ...

  8. POJ3164 最小树形图 有向图的最小生成树 模板题 朱刘算法 朱永津-刘振宏算法

    Command Network Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 12833   Accepted: 3717 ...

  9. poj3164(最小树形图朱刘算法模板)

    题目链接:http://poj.org/problem?id=3164 题意:第一行为n, m,接下来n行为n个点的二维坐标, 再接下来m行每行输入两个数u, v,表点u到点v是单向可达的,求这个有向 ...

最新文章

  1. centos 6.3 安装reids
  2. [jQuery]10 Things I Learned from the jQuery Source
  3. linux空指针异常能捕获到吗,一次kernel panic分析--空指针in handle_IRQ_event
  4. 互评Alpha版本—SkyHunter
  5. FlyWeight(享元)
  6. LiveGBS国标GB/T28181视频平台获取海康大华宇视摄像机设备通道视频流直播地址 HLS/HTTP-FLV/WS-FLV/WebRTC/RTMP/RTSP直播流地址示例
  7. PS 矩形工具的使用
  8. 关于2013年1月21日的DNS故障分析文章
  9. . 尐儍苽 推荐一个专业的社团网站给您
  10. (1170, BLOB/TEXT column 'description' used in key specification without a key length)
  11. NetBeans 打开/保存具有指定编码的文件插件
  12. Feign传输MultipartFile 报错 Error converting request body
  13. 项目实训——2022
  14. 【跨境电商】5个最佳免费极简主义WordPress主题(一)
  15. 【前端期末作业 基于jQuery鲜花销售管理系统】
  16. 如何在电脑上制作请假条表格_单位请假条表格
  17. 利用stm32高级定时器的重复计数实现输出精确个数的pwm波
  18. 互为对偶的两个线性规划问题的解存在关系
  19. python正弦波叠加方波_傅立叶变换还能画简笔画?谷歌工程师开发的这个试玩网站火了...
  20. Selenium+Firefox在Ubuntu22.04上不工作

热门文章

  1. 【python二级-练习题】
  2. 第十九讲 信息安全管理【2021年软考-高级信息系统项目管理师】
  3. FreeSwitch呼入处理流程
  4. 第三方登录之QQ登录集成(二)
  5. web前端技术笔记(十六)bootstrap、表单正则和前端优化
  6. JQuery选择器细节-遁地龙卷风
  7. IOS(iPad、iPhone)删除已下载系统更新
  8. 扫描MAC地址脚本--保存
  9. 关于面试总结1-SQL学生表
  10. 手把手的 git 降伏指南——阿龙咸鱼经