20-2-22-最小费用的最大流-POJ2516

POJ2516 最小费用最大流

Minimum Cost

Time Limit: 4000MSMemory Limit: 65536K
Total Submissions: 20565Accepted: 7258

Description

Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area there are N shopkeepers (marked from 1 to N) which stocks goods from him.Dearboy has M supply places (marked from 1 to M), each provides K different kinds of goods (marked from 1 to K). Once shopkeepers order goods, Dearboy should arrange which supply place provide how much amount of goods to shopkeepers to cut down the total cost of transport.

It’s known that the cost to transport one unit goods for different kinds from different supply places to different shopkeepers may be different. Given each supply places’ storage of K kinds of goods, N shopkeepers’ order of K kinds of goods and the cost to transport goods for different kinds from different supply places to different shopkeepers, you should tell how to arrange the goods supply to minimize the total cost of transport.

Input

The input consists of multiple test cases. The first line of each test case contains three integers N, M, K (0 < N, M, K < 50), which are described above. The next N lines give the shopkeepers’ orders, with each line containing K integers (there integers are belong to [0, 3]), which represents the amount of goods each shopkeeper needs. The next M lines give the supply places’ storage, with each line containing K integers (there integers are also belong to [0, 3]), which represents the amount of goods stored in that supply place.

Then come K integer matrices (each with the size N * M), the integer (this integer is belong to (0, 100)) at the i-th row, j-th column in the k-th matrix represents the cost to transport one unit of k-th goods from the j-th supply place to the i-th shopkeeper.

The input is terminated with three "0"s. This test case should not be processed.

Output

For each test case, if Dearboy can satisfy all the needs of all the shopkeepers, print in one line an integer, which is the minimum cost; otherwise just output “-1”.

Sample Input

1 3 3   
1 1 1
0 1 1
1 2 2
1 0 1
1 2 3
1 1 1
2 1 1

1 1 1
3
2
20

0 0 0

Sample Output

4
-1

Source

POJ Monthly–2005.07.31, Wang Yijie

  • 最小费用最大流的板子题, 建好图之后啥都好说, 懒得写了,看博客戳我

  • 没有想到开始需要判断供之和与求之和的关系, 即k种物品, 每种物品的供是否能够满足求

  • 代码

  • 刘汝佳的最小费用最大流的板子

    struct Edge {
    	int from, to, cap, flow, cost;
    	Edge(int u, int v, int c, int f, int w) :from(u), to(v), cap(c), flow(f), cost(w)
    	{}
    };
    struct MCMF {
    	int n, m;
    	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) {
    		this->n = n;
    		for (int i = 0; i < n; i++) G[i].clear();
    		edges.clear();
    	}
    	void AddEdge(int from, int to, int cap, int cost) {
    		edges.push_back(Edge(from, to, cap, 0, cost));
    		edges.push_back(Edge(to, from, 0, 0, -cost));
    		m = edges.size();
    		G[from].push_back(m - 2);
    		G[to].push_back(m - 1);
    	}
    	bool BellmanFord(int s, int t, int& flow, long long& cost) {
    		for (int i = 0; i < n; i++) d[i] = INF;
    		memset(inq, 0, sizeof(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;
    		flow += a[t];
    		cost += (long long)d[t] * (long long)a[t];
    		for (int u = t; u != s; u = edges[p[u]].from) {
    			edges[p[u]].flow += a[t];
    			edges[p[u] ^ 1].flow -= a[t];
    		}
    		return true;
    	}
    	//需要保证初始网络中没有负权圈
    	int MincostMaxflow(int s, int t, long long& cost) {
    		int flow = 0; cost = 0;
    		while (BellmanFord(s, t, flow, cost));
    		return flow;
    	}
    };
    
    • 本题代码包括注释
#pragma warning(disable:4996)
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int MAXN = 110;
const int MAXM = 110 * 110;
const int maxn = 55;
const int INF = 0x3f3f3f3f;

int head[MAXN];
struct Edge {
	int u, v, cap, cost, next;
	Edge(int u_ = 0, int v_ = 0, int cap_ = 0, int cost_ = 0, int next_ = 0) {
		u = u_;
		v = v_;
		cap = cap_;
		cost = cost_;
		next = next_;
	}
}edge[MAXM];
int num_of_vertices;
int cur_edge_index;
int source, meet;//源点和汇点的序号

void addEdge(int u, int v, int cap, int cost) {
	//正向边
	edge[cur_edge_index].u = u; //
	edge[cur_edge_index].v = v;
	edge[cur_edge_index].cap = cap;
	edge[cur_edge_index].cost = cost;
	edge[cur_edge_index].next = head[u];
	head[u] = cur_edge_index++;

	//设置反向边
	edge[cur_edge_index].u = v; //
	edge[cur_edge_index].v = u;
	edge[cur_edge_index].cap = 0;
	edge[cur_edge_index].cost = -cost;
	edge[cur_edge_index].next = head[v];
	head[v] = cur_edge_index++;
}
//该顶点是在队列
bool vis[MAXN];
int d[MAXN];
int pre[MAXN];
bool spfa() {
	for (int i = 0; i < num_of_vertices; i++) {
		vis[i] = false;
		d[i] = INF;
	}
	vis[source] = true;
	d[source] = 0;

	queue<int> q;
	while (!q.empty()) {
		q.pop();
	}
	q.push(source);
	while (!q.empty()) {
		int u = q.front(); 
		q.pop();
		for (int i = head[u]; i != -1; i = edge[i].next) {
			int v = edge[i].v;
			//仍然存在曾广路并且优先每次找到最小费用的增广路
			if (edge[i].cap && d[v] > d[u] + edge[i].cost) {
				d[v] = d[u] + edge[i].cost;
				pre[v] = i;
				//这里的不会出现负权回路, 无需判环
				if (!vis[v]) {
					vis[v] = true;
					q.push(v);
				}
			}
		}
		vis[u] = false;
	}
	return d[meet] < INF;
}
int MCMF() {
	int res = 0;
	//找到一条增广路
	while (spfa()) {
		//路径回溯, 路径上的flow全部加上
		int flow = INF;
		int cost = 0;
		for (int u = meet; u != source; u = edge[pre[u]].u) {
			flow = min(flow, edge[pre[u]].cap);
		}
		for (int u = meet; u != source; u = edge[pre[u]].u) {
			edge[pre[u]].cap -= flow;
			edge[pre[u] ^ 1].cap += flow;
			cost += flow * edge[pre[u]].cost;
		}
		res += cost;
	}
	return res;
}

//(0,maxn)
int n, m, k;//n个shopkeeper, m个supplies place, 需要k types commodities
int g[maxn][maxn][maxn];

int to[maxn][maxn];//to[i][j]代表第i个商家需要的第j种商品的个数
int from[maxn][maxn];//from[i][j]代表第i个supper提供的第j种商品的个数

bool isEnough(int* needs, int* supplies) {
	for (int i = 0; i < k; i++) {
		if (needs[i] > supplies[i])
			return false;
	}
	return true;
}

int main() {
	int supplies[maxn];
	int needs[maxn];//needs[i]代表所有商家一共需要的第i种商品的个数
	while (~scanf("%d%d%d", &n, &m, &k) && (n || m || k)) {
		memset(supplies, 0, sizeof(supplies));
		memset(needs, 0, sizeof(needs));
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < k; j++) {
				scanf("%d", &to[i][j]);
				needs[j] += to[i][j];
			}
		}
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < k; j++) {
				scanf("%d", &from[i][j]);
				supplies[j] += from[i][j];
			}
		}

		for (int i = 0; i < k; i++) {
			for (int x = 0; x < n; x++) {
				for (int y = 0; y < m; y++) {
					scanf("%d", &g[i][x][y]);
				}
			}
		}
		if (isEnough(needs, supplies) == false) {
			puts("-1");
			continue;
		}
		//源点和汇点的序号
		source = n + m;
		meet = n + m + 1;
		num_of_vertices = n + m + 2;

		int res = 0;
		//处理每一件商品
		for (int t = 0; t < k; ++t) {
			memset(head, -1, sizeof(head));
			cur_edge_index = 0;
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					//这里是j到i
					addEdge(j, i + m, INF, g[t][i][j]);
				}
			}
			//源点和汇点加边, m个供应商, n个shopkeepers
			for (int i = 0; i < m; i++) {
				addEdge(source, i, from[i][t], 0);
			}
			for (int i = 0; i < n; i++) {
				addEdge(i + m, meet, to[i][t], 0);
			}
			res += MCMF();
		}
		printf("%d\n", res);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值