POJ 2516 最小费用最大流
每一种货物都是独立的,分成k次最小费用最大流即可!
1: /**
2: 因为e ==0 所以 pe[v] pe[v]^1 是两条相对应的边
3: E[pe[v]].c -= aug; E[pe[v]^1].c += aug;
4:
5: */
6: #include <queue>
7: #include <iostream>
8: #include <string.h>
9: #include <stdio.h>
10: #include <map>
11: using namespace std;
12: #define V 30010 // vertex
13: #define E 150010 // edge
14: #define INF 0x3F3F3F3F
15: struct MinCostMaxFlow
16: {
17: struct Edge
18: {
19: int v, next, cap, cost; // cap 为容量, cost为单位流量费用
20: } edge[E];
21:
22: int head[V], pe[V], pv[V]; // 每个节点的第一条edge[idx]的编号.
23: int dis[V]; // the shortest path to the src
24: bool vis[V]; // visted
25: // pe[v] 存放在增广路上到达v的边(u,v) 在edge[]的位置
26: // pv[u] 存放在增广路上从u出发的边(u,v) 在edge[]中的位置
27: int e, src, sink; // the index of the edge
28: int VertexNum; // N the num of the vertex. M the num of edges
29: void addedge(int u, int v, int cap, int cost)
30: {
31: edge[e].v = v, edge[e].cap = cap;
32: edge[e].cost = cost,edge[e].next = head[u], head[u] = e++;
33: edge[e].v = u, edge[e].cap = 0;
34: edge[e].cost = -1*cost, edge[e].next = head[v], head[v] = e++;
35: }
36: // 求最短路,不存在负环的时候,比 DFS快
37: int SPFABFS()
38: {
39: memset(vis, 0 ,sizeof(vis));
40: memset(pv, -1, sizeof(pv));
41: for(int i=0; i<V; i++) dis[i] = INF;
42: queue<int> Q;
43: Q.push(src);
44: vis[src] = 1, dis[src] = 0;
45: while(!Q.empty())
46: {
47: int u = Q.front();
48: Q.pop();
49: vis[u] = 0;
50: for(int i=head[u]; i!=-1; i=edge[i].next)
51: {
52: int v = edge[i].v;
53: if(edge[i].cap > 0 && dis[v] > dis[u] + edge[i].cost )
54: {
55: dis[v] = dis[u] + edge[i].cost;
56: if(!vis[v])
57: {
58: Q.push(v);
59: vis[v] = 1;
60: }
61: pv[v] = u;
62: pe[v] = i;
63: }
64: }
65: }
66: if(dis[sink] == INF) return -2; // can't from src to sink.
67: return dis[sink];
68: }
69:
70: pair<int,int> MCMF()
71: {
72: int maxflow = 0, mincost = 0;
73: while(SPFABFS())
74: {
75: if(pv[sink] == -1) break;
76: int aug = INF;
77: for(int i= sink; i!= src; i = pv[i])
78: aug =min(aug, edge[pe[i]].cap);
79: maxflow += aug;
80: mincost += aug * dis[sink];
81: for(int i = sink; i!= src; i = pv[i])
82: {
83: edge[pe[i]].cap -= aug;
84: edge[pe[i]^1].cap += aug;
85: }
86: }
87: return make_pair(maxflow, mincost);
88: }
89: int solve()
90: {
91: int N,M,K;
92: while(scanf("%d%d%d", &N , &M, &K) && N!=0 && M!=0 && K!=0)
93: {
94: int tmp;
95: vector<int> need[55];
96: for(int i=0; i<N; i++)
97: {
98: for(int j=0; j<K; j++)
99: {
100: scanf("%d", &tmp);
101: need[i].push_back(tmp);
102: }
103: }
104: vector<int> supply[55];
105: for(int i =0; i<M; i++)
106: {
107: for(int j=0; j<K; j++)
108: {
109: scanf("%d", &tmp);
110: supply[i].push_back(tmp);
111: }
112: }
113: vector<vector<vector<int> > > cost;
114: for(int i=0; i<K; i++)
115: {
116: vector<vector<int> > y;
117: for(int j=0; j<N; j++)
118: {
119: vector<int> x;
120: for(int k = 0; k<M; k++)
121: {
122: scanf("%d", &tmp);
123: x.push_back(tmp);
124: }
125: y.push_back(x);
126: }
127: cost.push_back(y);
128: }
129: int ret = 0;
130: bool flag = true;
131: for(int k = 0; k < K; k++)
132: {
133: e=0;
134: memset(head, -1,sizeof(head));
135: // M suppliers
136: src = 0;
137: sink = M+N+1;
138: VertexNum = M+N+2;
139: for(int i=0; i<M; i++)
140: {
141: addedge(src,i+1,supply[i][k],0);
142: }
143: int x = 0;
144: for(int i=0; i<N; i++)
145: {
146: x+= need[i][k];
147: addedge(M+i+1,sink, need[i][k], 0);
148: }
149: for(int j=0; j<M; j++)
150: {
151: for(int i=0; i<N; i++)
152: {
153: addedge(j+1, M+i+1,INF,cost[k][i][j]);
154: }
155: }
156: pair<int,int> res = MCMF();
157: if(res.first < x)
158: {
159: flag = 0;
160: break;
161: }
162: ret += res.second;
163: }
164: if(flag) cout<<ret<<endl;
165: else cout<<-1<<endl;
166: }
167: }
168: } mcmf;
169:
170:
171: int main()
172: {
173: freopen("1.txt","r",stdin);
174: mcmf.solve();
175: return 0;
176: }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
POJ 2516 最小费用最大流的更多相关文章
- poj 3422(最小费用最大流)
题目链接:http://poj.org/problem?id=3422 思路:求从起点到终点走k次获得的最大值,最小费用最大流的应用:将点权转化为边权,需要拆点,边容量为1,费用为该点的点权,表示该点 ...
- POJ - 2195 最小费用最大流
题意:每个人到每个房子一一对应,费用为曼哈顿距离,求最小的费用 题解:单源点汇点最小费用最大流,每个人和房子对于建边 #include<map> #include<set> # ...
- poj 2195 最小费用最大流模板
/*Source Code Problem: 2195 User: HEU_daoguang Memory: 1172K Time: 94MS Language: G++ Result: Accept ...
- POJ 2135 最小费用最大流 入门题
Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19207 Accepted: 7441 Descri ...
- poj 2135最小费用最大流
最小费用最大流问题是经济学和管理学中的一类典型问题.在一个网络中每段路径都有"容量"和"费用"两个限制的条件下,此类问题的研究试图寻找出:流量从A到B,如何选择 ...
- poj 3680(最小费用最大流)
题目链接:http://poj.org/problem?id=3680 思路:因为N<=200,而区间范围为[1,100000],因此需要离散化,去重,然后就是建图了相连两点连边,容量为k,费用 ...
- POJ 2315 最小费用最大流
从1走到N然后从N走回来的最短路程是多少? 转换为费用流来建模. 1: /** 2: 因为e ==0 所以 pe[v] pe[v]^1 是两条相对应的边 3: E[pe[v]].c -= aug; E ...
- POJ 2135 最小费用最大流
题目链接 Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18961 Accepted: 7326 D ...
- POJ 2516 Minimum Cost (最小费用最大流)
POJ 2516 Minimum Cost 链接:http://poj.org/problem?id=2516 题意:有M个仓库.N个商人.K种物品.先输入N,M.K.然后输入N行K个数,每一行代表一 ...
随机推荐
- python中那些双下划线开头得函数和变量--转载
Python中下划线---完全解读 Python 用下划线作为变量前缀和后缀指定特殊变量 _xxx 不能用'from module import *'导入 __xxx__ 系统定义名字 __x ...
- Angular 2.0 从0到1 (七)
第一节:Angular 2.0 从0到1 (一)第二节:Angular 2.0 从0到1 (二)第三节:Angular 2.0 从0到1 (三)第四节:Angular 2.0 从0到1 (四)第五节: ...
- backboneJs 导图
- 【转载】Kafka实现篇之消息和日志
http://blog.csdn.net/honglei915/article/details/37760631 消息格式 日志 一个叫做“my_topic”且有两个分区的的topic,它的日志有两个 ...
- Excel日期格式单元格写成yyyy.MM.dd格式将无法读取到DataTable
最近在改公司的订单系统,遇到了一个奇怪的问题.C#程序需要从Excel文件中将数据全部读取到DataTable,其中Excel文件的第一列是日期格式yyyy/MM/dd,而这一列中大部分的单元格都是按 ...
- Linux命令(1):cd命令
1.作用:改变工作目录: 2.格式:cd [路径] 其中的路径为要改变的工作目录,可为相对路径或绝对路径 3.使用实例:[root@www uclinux]# cd /home/yourname/ ...
- ios Swift 国外资源
Swift国外资源汇总(No.1) 此类分享贴暂定每2天更新一次,主要目的是让大家能跟国外开发者们同步,共享知识和共同提高. 对于一些非常有价值的文章,大家有兴趣可以自行翻译(回贴跟我说一声,避免重复 ...
- 点线图中的A*算法
A*简介 A*(A-Star)算法是一种启发式算法,是静态路网中求解最短路最有效的方法.公式表示为:f(n)=g(n)+h(n), 其中f(n) 是节点n从初始点到目标点的估价函数, g(n) 是在状 ...
- C/C++ union
叙述原因: union data{ int a;double b;}; 对于union,实际中用的并不多,之前也知道怎样计算union的单元(在字对齐的基础上取最大成员所占的内存大小),比如 unio ...
- MFC中获取指针的方法
1.获取应用程序指针 CMyApp* pApp=(CMyApp*)AfxGetApp(); 2.获取主框架指针 CWinApp 中的公有成员变量 m_pMainWnd 就是主框架的指针 CMainFr ...