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个数,每一行代表一 ...
随机推荐
- 在Flex4中使用RemoteObjectAMF0来连接fluorine网关 转
转http://vipnews.csdn.net/newscontent.aspx?pointid=2011_04_21_142132661 RemoteObjectAMF0是一个开源组件,可以很方便 ...
- Mysql打开日志信息
还可参考博客:http://pangge.blog.51cto.com/6013757/1319304 1)怎么查看mysql是否启用了日志 mysql>show variables like ...
- Linux串口编程(转载)
在嵌入式Linux中,串口是一个字设备,访问具体的串行端口的编程与读/写文件 的操作类似,只需打开相应的设备文件即可操作.串口编程特殊在于串 口通信时相关参数与属性的设置.嵌入式Linux的串口编程时 ...
- MSP430常见问题之AD转换类
Q1:MSP430F149 AD 的输入阻抗有多大?A1:RC<2000欧*30PF Q2:MSP430 ADC12 模块的速度?A2: ADC12 的转换速率是转换所需的ADC12CLK 以及 ...
- (转)MSMQ续
原文作者:虔诚者 点此传送至原文 在上一篇我简单介绍了MSMQ的相关概念,本篇将以代码说明 Message Message是MSMQ的数据存储单元,我们的用户数据一般也被填充在Message的b ...
- Jersey(1.19.1) - Extracting Request Parameters
Parameters of a resource method may be annotated with parameter-based annotations to extract informa ...
- unity3d游戏无法部署到windows phone8手机上的解决方法
今天搞了个unity3d游戏,准备部署到自己的lumia 920上,数据线连接正常,操作正常,但是“build”以后,始终无法部署到手机上,也没有在选择的目录下生产任何相关文件.(你的系统必须是win ...
- 百度手机号码归属地查询api与返回json处理
前天无意间在网上看到百度ApiStore,然后好奇就进去看了看.正好最近在某博培训Android,刚学到java基础.抱着锻炼的心态选择手机号码归属地查询api进行练手.api地址 (http://a ...
- Ajax之数据连接信息捕获
connDB.properties: DB_CLASS_NAME=com.mysql.jdbc.Driver DB_URL=jdbc:mysql://127.0.0.1:3306/db_datab ...
- RDD机制实现模型Spark初识
Spark简介 Spark是基于内存计算的大数据分布式计算框架.Spark基于内存计算,提高了在大数据环境下数据处理的实时性,同时保证了高容错性和高可伸缩性. 在Spark中,通过RDD( ...