洛谷P4716 【模板】最小树形图(朱刘算法)
题意
Sol
朱刘算法?感觉又是一种神仙贪心算法
大概就是每次贪心的用每个点边权最小的入边更新答案,如果不行的话就缩起来找其他的边
不详细说了,丢链接走人..
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10, INF = 1e9 + 10;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M, R, fa[MAXN], mn[MAXN], id[MAXN], vis[MAXN];
struct Edge {
int u, v, w, nxt;
}E[MAXN];
int head[MAXN], num = 1;
inline void AddEdge(int x, int y, int z) {
E[num] = (Edge) {x, y, z, head[x]}; head[x] = num++;
}
int ZhuLiu() {
int ans = 0;
while("attack is a pig") {
for(int i = 1; i <= N; i++) id[i] = vis[i] = 0, mn[i] = INF; int cnt = 0;
for(int i = 1; i <= M; i++) if((E[i].u != E[i].v) && (E[i].w < mn[E[i].v])) mn[E[i].v] = E[i].w, fa[E[i].v] = E[i].u;
int x; mn[R] = 0;//tag
for(int i = 1; i <= N; i++) {
if(mn[i] == INF) return -1; ans += mn[i];
for(x = i; (!id[x]) && x != R && (vis[x] != i); x = fa[x]) vis[x] = i;
if(x != R && (!id[x])) {
id[x] = ++cnt; for(int t = fa[x]; t != x; t = fa[t]) id[t] = cnt;
}
}
if(!cnt) return ans;
for(int i = 1; i <= N; i++) if(!id[i]) id[i] = ++cnt;
for(int i = 1; i <= M; i++) {
int tmp = mn[E[i].v];
if((E[i].u = id[E[i].u]) != (E[i].v = id[E[i].v])) E[i].w -= tmp;
}
N = cnt; R = id[R];
}
return ans;
}
int main() {
memset(head, -1, sizeof(head));
N = read(); M = read(); R = read();
for(int i = 1; i <= M; i++) {
int x = read(), y = read(), z = read();
AddEdge(x, y, z);
}
printf("%d", ZhuLiu());
return 0;
}
洛谷P4716 【模板】最小树形图(朱刘算法)的更多相关文章
- 最小树形图——朱刘算法(Edmonds)
定义:一个有向图,存在从某个点为根的,可以到达所有点的一个最小生成树,则它就是最小树形图. 朱刘算法实现过程: [在选出入边集后(看步骤1),若有向图中不存在有向环,说明该图就是最小树形图] 1,选入 ...
- poj3164(最小树形图&朱刘算法模板)
题目链接:http://poj.org/problem?id=3164 题意:第一行为n, m,接下来n行为n个点的二维坐标, 再接下来m行每行输入两个数u, v,表点u到点v是单向可达的,求这个有向 ...
- 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 最小树形图 朱刘算法
=============== 分割线之下摘自Sasuke_SCUT的blog============= 最 小树形图,就是给有向带权图中指定一个特殊的点root,求一棵以root为根的有向生成树T, ...
- 最小树形图--朱刘算法([JSOI2008]小店购物)
题面 luogu Sol 首先设一个 \(0\) 号点,向所有点连边,表示初始价值 显然这个图的一个 \(0\) 为根的最小有向生成树的边权和就是每个买一次的最小价值 再买就一定能优惠(包含 \(0\ ...
- POJ - 3164-Command Network 最小树形图——朱刘算法
POJ - 3164 题意: 一个有向图,存在从某个点为根的,可以到达所有点的一个最小生成树,则它就是最小树形图. 题目就是求这个最小的树形图. 参考资料:https://blog.csdn.net/ ...
- bzoj 4349 最小树形图——朱刘算法
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4349. 学习博客:http://www.cnblogs.com/xzxl/p/7243466 ...
- 洛谷.4897.[模板]最小割树(Dinic)
题目链接 最小割树模板.具体见:https://www.cnblogs.com/SovietPower/p/9734013.html. ISAP不知为啥T成0分了.. Dinic: //1566ms ...
- 洛谷.3381.[模板]最小费用最大流(zkw)
题目链接 Update:我好像刚知道多路增广就是zkw费用流.. //1314ms 2.66MB 本题优化明显 #include <queue> #include <cstdio&g ...
随机推荐
- UVA10140 Prime Distance
UVA10140 Prime Distance 给定两个整数L,R(1<=L<=R<=2^{31},R-L<=10^6)L,R(1<=L<=R<=231,R− ...
- 使用python 模仿mybinlog 命令 二进制分析mysql binlog
出处:黑洞中的奇点 的博客 http://www.cnblogs.com/kelvin19840813/ 您的支持是对博主最大的鼓励,感谢您的认真阅读.本文版权归作者所有,欢迎转载,但请保留该声明. ...
- 可以修改类不用重启Tomcat加载整个项目
修改类不重启Tomcat(不用手动重启) 修改tomcat conf目录下的server.xml <Context path="/struts2" docBase=&quo ...
- tp5.0 根据经纬度 获取附近信息
自己备注一下 /* *参数说明: *$lng 经度 *$lat 纬度 *$distance 周边半径 默认是500米(0.5Km) */ public function returnSquarePoi ...
- mysql启动不起来
在刚编译安装完成mysql,启动mysql时报了下面错误: /etc/init.d/mysqld start Starting MySQL... ERROR! The server quit with ...
- 用js制作简易计算器及猜随机数字游戏
<!doctype html><html><head> <meta charset="utf-8"> <title>JS ...
- spring boot快速入门 4: jpa数据库操作 实现增删改查
spring boot jpa逆向生成表 简单实例: 第一步:pom文件: <?xml version="1.0" encoding="UTF-8"?&g ...
- javascript中childNodes与children的区别
1.childNodes:获取节点,不同浏览器表现不同: IE:只获取元素节点: 非IE:获取元素节点与文本节点: 解决方案:if(childNode.nodeName=="#text&qu ...
- vertical-aligin
垂直对齐元素只应用于行内元素(图像.文本)和表单元素 垂直对齐文本会影响行高 默认值为baseline
- python-五个面试题
问题一:以下的代码的输出将是什么? 说出你的答案并解释. class Parent(object): x = 1 class Child1(Parent): pass class Child2(Par ...