CSU 1506 - Double Shortest Paths(网络流’最小费用流)
来源:互联网 发布:brew 卸载软件 编辑:程序博客网 时间:2024/05/16 04:39
题目:
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=75853#problem/D
题意:
求出两个人从1走到n的最小花费,前一个人走过的路径权值会改变为d+a。
思路:
两点之间建两条边,分别为 容量1费用d 和 容量1费用d+a。由于模板求的是最小费用最大流,而本题需要限制流量为2,所以需要另设源点和汇点(容量为2) 来限制流量.
AC.
#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 505;const int maxm = 2005;struct Edge { int to, next, cap, flow, cost;}edge[maxm];int head[maxn], tol;int pre[maxn], dis[maxn];bool vis[maxn];int N;void init(int n){ N = n; tol = 0; memset(head, -1, sizeof(head)); //memset(pre, -1, sizeof(pre));}void addedge(int u, int v, int cap, int cost){ edge[tol].to = v; edge[tol].cap = cap; edge[tol].cost = cost; edge[tol].flow = 0; edge[tol].next = head[u]; head[u] = tol++; edge[tol].to = u; edge[tol].cap = 0; edge[tol].cost = -cost; edge[tol].flow = 0; edge[tol].next = head[v]; 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 = edge[i].next) { int v = edge[i].to; if(edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost) { dis[v] = dis[u] + edge[i].cost; pre[v] = i; if(!vis[v]) { vis[v] = true; q.push(v); } } } } if(pre[t] == -1) return false; else return true;}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[edge[i^1].to]) { if(Min > edge[i].cap - edge[i].flow) { Min = edge[i].cap - edge[i].flow; } } for(int i = pre[t]; i != -1; i = pre[edge[i^1].to]) { edge[i].flow += Min; edge[i^1].flow -= Min; cost += edge[i].cost * Min; }//printf("%d\n", flow); flow += Min; } return flow;}int main(){ //freopen("in", "r", stdin); int ca = 1; int n, m; while(~scanf("%d %d", &n, &m)) { int s = 0, t = n + 1; int ans; init(t+1); // for(int i = 0; i < m; ++i) { int u, v, c, a; scanf("%d %d %d %d", &u, &v, &c, &a); addedge(u, v, 1, c); addedge(u, v, 1, c+a); } addedge(s, 1, 2, 0); addedge(n, t, 2, 0); mincostmaxflow(s, t, ans); printf("Case %d: %d\n", ca++, ans); } return 0;}
0 0
- CSU 1506 - Double Shortest Paths(网络流’最小费用流)
- CSU 1506: Double Shortest Paths(最小费用最大流)
- CSU 1506 Double Shortest Paths(最小费用流)
- CSU 1506 Double Shortest Paths(最小费用最大流)
- CSU 1506 Double Shortest Paths【最小费用最大流】
- 【费用流】CSU 1506 Double Shortest Paths
- CSU 1506: Double Shortest Paths(最小费用流)(湖南省第十届省赛)
- CSU 1506 Problem D: Double Shortest Paths(最小费用最大流)
- Double Shortest Paths 网络流
- 费用流 csu1506 Double Shortest Paths
- Double Shortest Paths CSU
- Double Shortest Paths(费用流)2014年省赛D题
- CSU1506 Double Shortest Paths 最小费用最大流入门题
- POJ3068 "Shortest" pair of paths最小费用流
- POJ3068--"Shortest" pair of paths(最小费用流)
- poj 3068 "Shortest" pair of paths(最小费用流)
- poj3068 "Shortest" pair of paths 费用流
- POJ 3068 "Shortest" pair of paths(最小费用最大流-mcmf)
- android.app.Fragment$InstantiationException的原因分析
- 如何成为一名优秀的web前端工程师
- 二进制加法(算导课后习题)
- 控件的layer边框用法
- Google Code Jam 2015 Round 1A: Problem A. Mushroom Monster
- CSU 1506 - Double Shortest Paths(网络流’最小费用流)
- ORACLE9i_性能调优基础九(Managing Statistics)
- MFC对话框(Dialog类)中添加客户区(View类)实现绘图
- HDU 4035 Maze (概率dp)
- 嵌入式CGI 与HTML的数据交换
- 10 CI where数据库操作 以及查询最后一次执行的sql语句
- Find Minimum in Rotated Sorted Array - LeetCode 153
- POJ 3461 Oulipo
- IOS中拨打电话