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个数,每一行代表一 ...
随机推荐
- LeetCode 274
H-Index Given an array of citations (each citation is a non-negative integer) of a researcher, write ...
- 使用 autoconf
在此之前先说一下autooconf在linux下安装的问题,因为不知道怎么安装,我就直接在终端上输入autoconf,结果它会提示是否安装它,下面还有指定安装的方法,我 就直接输入,是什么命令记不住了 ...
- linux nginx启动 重启 关闭命令
启动操作 nginx -c /usr/local/nginx/conf/nginx.conf -c参数指定了要加载的nginx配置文件路径 停止操作停止操作是通过向nginx进程发送信号来进行的 步骤 ...
- EWM Matrai B2B管理平台
该应用是一款企业管理的app,可以通过“分享”.“工作分派”.“审批”.“业务”.“工作计划”.“日程”等功能得到有效的管控.该项目主要分为5大模块,分别是近期动态,任务,日程,我,在线聊天.
- Linux 命令 - ps: 显示当前进程的快照
命令格式 ps [options] 实例 a) 查看所有的进程. huey@huey-K42JE:~$ ps aux | head USER PID %CPU %MEM VSZ RSS TTY STA ...
- JQuery合并列(可用于导出Word)
在网上找了一些JQuery合并列的例子,但是都是用.hide()的方式,这样导致了在导出Word的时候表格严重变形 自己写了一个用.remove()方式的合并列 function arrangeTab ...
- js cookie使用方法详解
代码如下 复制代码 <script>function getCookie(c_name){ if (document.cookie.length>0){ //先查询cookie是否为 ...
- JavaScript代码检查工具 — JSHint
静态代码检查是开发工作中不可缺少的一环,毕竟对于程序化的工作人的眼睛是不可靠的,更何况是自己的眼睛看自己的代码.即使最后的运行结果通过,但可能存在一些未定义的变量.定义了但最后没用过的变量.分号有没有 ...
- nginx编译
先下载openssl.pcre.zlib安装:然后找个上传模块nginx-upload-module-2.2添加到nginx上. root@liuhan888:~# mkdir nginxroot@l ...
- Java Calendar类简单用法
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3832307.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...