费用流 csu1506 Double Shortest Paths
来源:互联网 发布:dota2新英雄语音知乎 编辑:程序博客网 时间:2024/06/05 04:21
传送门:点击打开链接
题意:Alice和Bob要从1走到n,每条边第一次被走过的时候的权值是d,第二次被走过的时候是d+a
思路:费用流裸题,对于将源点连到1,将n连到汇点,对于每条边建立两条边,一条费用为d,另一条费用为d+a然后跑一遍最小费用最大流答案就出来了
#include<map>#include<set>#include<cmath>#include<stack>#include<queue>#include<cstdio>#include<string>#include<vector>#include<cstring>#include<iostream>#include<algorithm>#include<functional>#define FIN freopen("input.txt","r",stdin)using namespace std;typedef long long LL;typedef pair<LL, int> PLI;const int MX = 2000 + 5;const int MM = 8000 + 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 = 2 * 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;}int main() { int n, m, ansk = 0;//FIN; while(~scanf("%d%d", &n, &m)) { init(n); int s = 0, t = 2 * n + 1; edge_add(s, 1, 2, 0); edge_add(n, t, 2, 0); for(int i = 1; i <= m; i++) { int u, v, d, a; scanf("%d%d%d%d", &u, &v, &d, &a); edge_add(u, v, 1, d); edge_add(u, v, 1, d + a); } int ans = 0; minCostMaxflow(s, t, ans); printf("Case %d: %d\n", ++ansk, ans); } return 0;}
1 0
- 费用流 csu1506 Double Shortest Paths
- CSU1506 Double Shortest Paths 最小费用最大流入门题
- 【费用流】CSU 1506 Double Shortest Paths
- CSU 1506 - Double Shortest Paths(网络流’最小费用流)
- CSU 1506: Double Shortest Paths(最小费用最大流)
- CSU 1506 Double Shortest Paths(最小费用流)
- Double Shortest Paths(费用流)2014年省赛D题
- CSU 1506 Double Shortest Paths(最小费用最大流)
- CSU 1506 Double Shortest Paths【最小费用最大流】
- Double Shortest Paths 网络流
- CSU 1506: Double Shortest Paths(最小费用流)(湖南省第十届省赛)
- CSU 1506 Problem D: Double Shortest Paths(最小费用最大流)
- poj3068 "Shortest" pair of paths 费用流
- Double Shortest Paths CSU
- uva 12821 Double Shortest Paths
- POJ3068 "Shortest" pair of paths最小费用流
- "Shortest" pair of paths - POJ 3068 费用流
- POJ3068--"Shortest" pair of paths(最小费用流)
- Android--文件监控FileObserver
- 用指针正确的操纵动态数组与正确用delete释放内存的方法
- java 子类重写父类的方法应注意的问题
- poj 1324 Holedox Moving(bfs+状态压缩)
- 黑马程序员------异常小结
- 费用流 csu1506 Double Shortest Paths
- pat(A) 2-06. 数列求和(模拟摆竖式相加)
- NYOJ 2 括号配对问题 (栈 stack)
- 黑马程序员------Foundatin之求单个文件的代码行数
- 文章标题
- Choose the best route
- CDN流量放大攻击思路
- 简单的视图切换(tag)
- Python Foundation - part two