POJ3436 Command Network [最小树形图]
POJ3436 Command Network
最小树形图裸题
傻逼poj回我青春
wa wa wa 的原因竟然是需要%.2f而不是.2lf
我还有英语作业音乐作业写不完了啊啊啊啊啊啊啊啊啊
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#define fir first
#define sec second
using namespace std;
const int N = 1005, M = 1e6+5, inf = 1e9+7;
int n, m, x[N], y[N];
struct edge {
int u, v;
double w;
} e[M];
inline double dis(int u, int v) {
return sqrt((x[u]-x[v])*(x[u]-x[v]) + (y[u]-y[v])*(y[u]-y[v]));
}
pair<double, int> fa[N];
int vis[N], id[N];
double zhu_liu(int root = 1) {
double ans = 0;
while(true) {
for(int i=1; i<=n; i++) fa[i] = make_pair(inf, 0);
for(int i=1; i<=m; i++) {
int u = e[i].u, v = e[i].v;
fa[v] = min(fa[v], make_pair(e[i].w, u));
}
for(int i=1; i<=n; i++) if(i != root && !fa[i].sec) {
return -1;
}
fa[root] = make_pair(0, 0);
memset(vis, 0, sizeof(vis));
memset(id, 0, sizeof(id));
int num = 0;
for(int i=1; i<=n; i++) if(i != root) {
ans += fa[i].fir;
int u = i;
while(u != root && !vis[u]) vis[u] = i, u = fa[u].sec;
if(u != root && vis[u] == i) {
num++;
while(id[u] != num) id[u] = num, u = fa[u].sec;
}
}
if(!num) break;
for(int i=1; i<=n; i++) if(!id[i]) id[i] = ++num;
int cnt = 0;
for(int i=1; i<=m; i++) {
int u = e[i].u, v = e[i].v;
double tmp = fa[v].fir;
u = id[u];
v = id[v];
if(u == v) continue;
e[++cnt] = (edge) {u, v, e[i].w - tmp};
}
n = num;
m = cnt;
root = id[root];
}
return ans;
}
int main() {
//ios::sync_with_stdio(false); cin.tie(); cout.tie();
while(cin >> n >> m) {
for(int i=1; i<=n; i++) scanf("%d %d", &x[i], &y[i]);
int cnt = 0;
for(int i=1; i<=m; i++) {
int u, v;
scanf("%d %d", &u, &v);
if(u == v) continue;
e[++cnt] = (edge) {u, v, dis(u, v)};
}
m = cnt;
double ans = zhu_liu();
if(ans == -1) puts("poor snoopy");
else printf("%.2f\n", ans);
}
}
POJ3436 Command Network [最小树形图]的更多相关文章
- POJ 3164 Command Network 最小树形图
题目链接: 题目 Command Network Time Limit: 1000MS Memory Limit: 131072K 问题描述 After a long lasting war on w ...
- POJ3164 Command Network —— 最小树形图
题目链接:https://vjudge.net/problem/POJ-3164 Command Network Time Limit: 1000MS Memory Limit: 131072K ...
- POJ3164 Command Network(最小树形图)
图论填个小坑.以前就一直在想,无向图有最小生成树,那么有向图是不是也有最小生成树呢,想不到还真的有,叫做最小树形图,网上的介绍有很多,感觉下面这个博客介绍的靠谱点: http://www.cnblog ...
- POJ 3164 Command Network 最小树形图模板
最小树形图求的是有向图的最小生成树,跟无向图求最小生成树有很大的区别. 步骤大致如下: 1.求除了根节点以外每个节点的最小入边,记录前驱 2.判断除了根节点,是否每个节点都有入边,如果存在没有入边的点 ...
- POJ 3164 Command Network 最小树形图 朱刘算法
=============== 分割线之下摘自Sasuke_SCUT的blog============= 最 小树形图,就是给有向带权图中指定一个特殊的点root,求一棵以root为根的有向生成树T, ...
- POJ - 3164-Command Network 最小树形图——朱刘算法
POJ - 3164 题意: 一个有向图,存在从某个点为根的,可以到达所有点的一个最小生成树,则它就是最小树形图. 题目就是求这个最小的树形图. 参考资料:https://blog.csdn.net/ ...
- POJ 3164 Command Network ( 最小树形图 朱刘算法)
题目链接 Description After a long lasting war on words, a war on arms finally breaks out between littlek ...
- POJ 3164——Command Network——————【最小树形图、固定根】
Command Network Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 15080 Accepted: 4331 ...
- POJ 3164 Command Network (最小树形图)
[题目链接]http://poj.org/problem?id=3164 [解题思路]百度百科:最小树形图 ]里面有详细的解释,而Notonlysucess有精简的模板,下文有对其模板的一点解释,前提 ...
随机推荐
- mysql MHA扩展haproxy搭建从库只读负载均衡
[环境介绍] 系统环境:Red Hat Enterprise Linux 7 + 5.7.18 + MHA version 0.57 MHA架构中从库之间的负责均衡可选择mysql_route& ...
- Writage让你的Word支持Markdown
Writage 简单的执行后,word就可以在保存或打开的时候支持Markdown了!
- luogu P5324 [BJOI2019]删数
传送门 不如先考虑暴力,能删的序列首先有\(1,2,3...n\),还有就是升序排序后从后往前放数,第\(i\)位要么放\(i\),要么放\(i+1\)位置的数,例如\(1,2,4,4,5,6,9,9 ...
- 一些C++的语法
一.类的析构函数 类的析构函数是类的一种特殊的成员函数,它会在每次删除所创建的对象时执行. 析构函数的名称与类的名称是完全相同的,只是在前面加了个波浪号(~)作为前缀,它不会返回任何值,也不能带有任何 ...
- matplotlib 生成 eps 插入到 tex
matplotlib 生成 eps 插入到 tex matplotlib 生成 eps,就可以插入到 tex 中,而且是矢量图,放大不失真. 而且因为图中的元素都是嵌入到 pdf 中,所以图中的文字也 ...
- qml layout
一.使用总结 1.锚点:锚点锚在父控件左边anchors.left: parent.left ,才可以设置anchors.leftMargin:20 才有作用,设置anchors.topMargin: ...
- go语言关于值类型和引用类型
前言:关于值类型和引用类型确实是个比较绕的问题,之前在学校的时候学习C语言的时候,就感觉没有看太懂,后面看java,关于引用也是模模糊糊,这个东西也确实比较抽象,估计好多写了好几年代码的人有也有些迷惑 ...
- 【Flask】报错解决方法:AssertionError: View function mapping is overwriting an existing endpoint function: main.user
运行Flask时出现了一个错误, AssertionError: View function mapping is overwriting an existing endpoint function: ...
- 解决SecureCRT中文版"数据库里没找到防火墙'无'"的错误提示
问题描述: 最近从同事那拷贝到一个中文版的SecureCRT,但是每次打开都会有个防火墙的错误提示,“数据库里没找到防火墙“无”.此会话将尝试不通过防火墙进行连接. 出现这个错误的原因是在Secure ...
- vscode添加prettier格式化自动加分号问题
在vscode的settings.json中添加: "prettier.singleQuote": true, "prettier.semi": false, ...