费用流 hdu1853 Cyclic Tour
来源:互联网 发布:微信树洞表白墙源码 编辑:程序博客网 时间:2024/04/29 22:14
传送门:点击打开链接
题意:给n个点和M条有向边,要找出许多个环出来,每个环点至少有2个,所有的点都要被环覆盖1次,且只能为1次。问所有环的长度之和
这题也可以用KM来做,这里主要是练习费用流的建图
对于这题,建图也是非常的奇妙的
由于每个点的入度都是1,出度都是1
所以会想到把每个点拆分成2个点,用i和i+n来表示
然后将源点与所有的i连接起来,将汇点与所有的i+n连接起来,容量都是1
对于每一条边(u,v),添加边(u,v+n,1,cost),让第一层的点连接到第二层去
其实就是将点拆分成两层,一层是输入层,一层是输出层。(脑补一下..)
最后检查最大流是否等于n,如果不等于就无解,否则就输出最小费用
#include<map>#include<set>#include<cmath>#include<stack>#include<queue>#include<cctype>#include<cstdio>#include<string>#include<vector>#include<cstring>#include<iostream>#include<algorithm>#include<functional>#define FIN freopen("input.txt","r",stdin)#define FOUT freopen("output.txt","w+",stdout)using namespace std;typedef long long LL;typedef pair<int, int> PII;const int MX = 1e3 + 5;const int MM = 2e5 + 5;const int INF = 0x3f3f3f3f;struct Edge { int to, next, cap, flow, cost; Edge() {} Edge(int _to, int _next, int _cap, int _flow, int _cost) { to = _to; next = _next; cap = _cap; flow = _flow; cost = _cost; }} E[MM];int Head[MX], tol;int pre[MX]; //储存前驱顶点int dis[MX]; //储存到源点s的距离bool vis[MX];int N;//节点总个数,节点编号从0~N-1void init(int n) { tol = 0; N = n + 2; memset(Head, -1, sizeof(Head));}void edge_add(int u, int v, int cap, int cost) { E[tol] = Edge(v, Head[u], cap, 0, cost); Head[u] = tol++; E[tol] = Edge(u, Head[v], 0, 0, -cost); Head[v] = tol++;}bool spfa(int s, int t) { queue<int>q; for (int i = 0; i < N; i++) { dis[i] = INF; vis[i] = false; pre[i] = -1; } dis[s] = 0; vis[s] = true; q.push(s); while (!q.empty()) { int u = q.front(); q.pop(); vis[u] = false; for (int i = Head[u]; i != -1; i = E[i].next) { int v = E[i].to; if (E[i].cap > E[i].flow && dis[v] > dis[u] + E[i].cost) { dis[v] = dis[u] + E[i].cost; pre[v] = i; if (!vis[v]) { vis[v] = true; q.push(v); } } } } if (pre[t] == -1) return false; else return true;}//返回的是最大流, cost存的是最小费用int minCostMaxflow(int s, int t, int &cost) { int flow = 0; cost = 0; while (spfa(s, t)) { int Min = INF; for (int i = pre[t]; i != -1; i = pre[E[i ^ 1].to]) { if (Min > E[i].cap - E[i].flow) Min = E[i].cap - E[i].flow; } for (int i = pre[t]; i != -1; i = pre[E[i ^ 1].to]) { E[i].flow += Min; E[i ^ 1].flow -= Min; cost += E[i].cost * Min; } flow += Min; } return flow;}inline int read() { char c = getchar(); while(!isdigit(c)) c = getchar(); int x = 0; while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); } return x;}int main() { int n, m; //FIN; while(~scanf("%d%d", &n, &m)) { int s = 0, t = 2 * n + 1; init(t); for(int i = 1; i <= n; i++) { edge_add(s, i, 1, 0); edge_add(i + n, t, 1, 0); } for(int i = 1; i <= m; i++) { int u, v, cost; u = read(); v = read(); cost = read(); edge_add(u, n + v, 1, cost); } int ans = 0; if(minCostMaxflow(s, t, ans)!=n) printf("-1\n"); else printf("%d\n", ans); } return 0;}
0 0
- 费用流 hdu1853 Cyclic Tour
- [费用流] HDU1853 Cyclic Tour
- 【拆点费用流】【HDU1853】【 Cyclic Tour】
- hdu1853 Cyclic Tour . 最小费用流 StL解法
- hdu1853 Cyclic Tour (KM算法求最小费用流)
- HDU1853 Cyclic Tour(KM或费用流)
- 【网络流】hdu1853 Cyclic Tour
- HDU1853-Cyclic Tour KM
- HDU1853 Cyclic Tour
- hdu1853 Cyclic Tour
- HDU1853 Cyclic Tour KM算法 模版题
- HDU 1853 Cyclic Tour //费用流
- hdu Cyclic Tour 1853 费用流
- HDU 1853 Cyclic Tour(最小费用流)
- HDU 1853 Cyclic Tour 费用流
- hdu 1853 Cyclic Tour 费用流
- HDU 1853 Cyclic Tour && HDU 3488Tour 最小费用流
- hdu1853 最小费用流
- Hdu oj 1596 find the safest road
- python 数据类型 序列——列表
- vijos - P1543极值问题(斐波那契数列 + 公式推导 + python)
- hdu1312
- 【汉化硬盘版】Muv-luv+Muv-Luv Alternative(带全CG存档+免安装,卸载补丁+打开存档文件夹补丁+中文攻略)
- 费用流 hdu1853 Cyclic Tour
- Count Complete Tree Nodes
- C99中的新增数据类型
- 爱程序网网站
- CSS选择符的种类以及对比分析
- 黑马程序员------Foundation初识
- 第一章--概述
- GDOI2016模拟8.18找数
- HDU 2094 产生冠军 <拓扑算法+map函数>