poj3422 来一道费用流
来源:互联网 发布:剃头理发器软件 编辑:程序博客网 时间:2024/06/06 07:39
题目链接
poj3422
真是感觉网络流越来越神奇了。
1、最小费用流 -- 最大费用流之间转化
2、拆点
3、费用 OR 流量 (cost, flow)
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <cmath>#include <queue>#include <stack>#include <map>#include <set>using namespace std;#define clr(x) memset(x,0,sizeof(x))#define fp1 freopen("in.txt","r",stdin)#define fp2 freopen("out.txt","w",stdout)#define pb push_back#define INF 0x3c3c3c3ctypedef long long LL;//网络中可以有负权边,但不能有负权圈。//每次先调用init(); 初始maxn、INF之类。//Addedge(cap, flow):cap是容量,flow是流量。const int maxn = 100000;struct Edge{ int from, to, cap, flow, cost;};struct MCMF{ int n, m, s, t; vector<Edge> edges; vector<int> G[maxn]; int inq[maxn]; //是否在队列中 int d[maxn]; //Bellman-Ford int p[maxn]; //上一条弧 int a[maxn]; //可改进量 void init(int n) //感觉每次调用都要调用init初始化哪 { this->n = n; for(int i = 0;i < n;i++) G[i].clear(); edges.clear(); //clr(p); clr(a); clr(d); } void addedge(int from, int to, int cost, int cap) { edges.pb((Edge){from, to, cap, 0, cost}); edges.pb((Edge){to, from, 0, 0, -cost}); m = edges.size(); G[from].pb(m-2); G[to].pb(m-1); } //其他成员函数 bool spfa(int s, int t, int &flow, int &cost) { for(int i = 0;i < n;i++) d[i] = INF; clr(inq); d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF; queue<int> Q; Q.push(s); while(!Q.empty()){ int u = Q.front(); Q.pop(); inq[u] = 0; for(int i = 0;i < G[u].size();i++){ Edge &e = edges[G[u][i]]; if(e.cap>e.flow && d[e.to]>d[u]+e.cost){ d[e.to] = d[u] + e.cost; p[e.to] = G[u][i]; a[e.to] = min(a[u], e.cap-e.flow); if(!inq[e.to]){ Q.push(e.to); inq[e.to] = 1; } } } } if(d[t] == INF) return false; //s-t不连通,失败退出 flow += a[t]; cost += d[t]*a[t]; int u = t; while(u != s){ edges[p[u]].flow += a[t]; edges[p[u]^1].flow -= a[t]; u = edges[p[u]].from; } return true; } int Mincost(int s, int t) { int flow = 0, cost = 0; while(spfa(s, t, flow, cost)); return cost; }};MCMF flow;int map1[55][55];int main(){ //freopen("in.txt", "r", stdin); int n, m; while (scanf("%d %d", &n, &m) == 2) { flow.init(n*n*2+100);int i, j;for (int i=1; i <= n;i++) {for (int j=1; j <= n;j++) {scanf("%d", &map1[i][j]);}}for (i = 1; i <= n; i++) {for (j = 1; j <= n; j++) {int u = (i - 1) * n + j;flow.addedge(u, n*n+u, -map1[i][j], 1); //reversed flow.addedge(u, n*n+u, 0, m-1);if(j != n){ flow.addedge(u, u+1, 0, m); flow.addedge(n*n+u, u+1, 0, m);}if(i != n){ flow.addedge(u, u+n, 0, m); flow.addedge(n*n+u, u+n, 0, m);}}}flow.addedge(0, 1, 0, m); //SuperSourceflow.addedge(n*n*2, n * n * 2 + 1, 0, m); //SuperSink cout<<-flow.Mincost(0, n*n*2+1);} return 0;}
0 0
- poj3422 来一道费用流
- POJ3422 费用流
- POJ3422简单费用流
- poj3422 最小费用最大流
- poj3422 最小费用最大流
- poj3422(*最小费用流)
- 费用流 poj3422 Kaka's Matrix Travels
- POJ3422 Kaka's Matrix Travels(费用流)
- Kaka's Matrix poj3422 费用流
- poj3422 Kaka's Matrix Travels 最大费用最大流
- poj3422 Kaka's Matrix Travels 最大费用最大流
- POJ3422 Kaka's Matrix Travels 【最大费用最大流】
- poj3422 Kaka's Matrix Travels 拆点+费用流
- POJ3422 Kaka's Matrix Travels 拆点+费用流(SPFA)
- POJ3422 Kaka's Matrix Travels 最小费用流
- poj3422 Kaka's Matrix Travels 最大费用流 (拆点)
- POJ3422 Kaka's Matrix Travels(最大费用最大流 + 拆点)
- POj3422 Kaka's Matrix Travels 最小费用最大流 拆点
- python 进阶学习之15
- Direct2D随笔2——在C#的WinForm环境下创建Gameloop
- POJ 2115 C Looooops
- hdu 1087 Super Jumping! Jumping! Jumping!(dp 最长上升子序列和)
- Django安装
- poj3422 来一道费用流
- Java对象的序列化和反序列化实践
- Windows Program Files 和 Program Files (x86) 的区别
- 小希的迷宫 并查集+hdu
- 使用json-lib完成json的序列化和反序列化
- 黑马程序员_黑马入学测试复习重点
- POJ 3295 Tautology
- Android开发笔记(3) 应用程序之间内容分享详解
- ZOJ--2750--Idiomatic Phrases Game【dijkstra】