【网络流】【费用流】[POJ 2516]Minimum Cost
题目描述
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.
输入
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
输出
4
-1
分析:首先排序,然后发现如果不冲突的话就可以多次使用把每个点拆点然后第二个点连接到自己到达之后的不冲突的第一个进入点,然后费用为零表示可以同时停下,然后把所用的费用*-1求得就是最大值了。
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <queue> using namespace std; const int MAXN = 200; const int INF = 1e9+7; const int MAXM = 20070; struct node{ int v, cost, cap; node *next, *back; }Edges[MAXM*2+10], *ecnt=Edges, *adj[MAXN+10]; int dis[MAXN+10], res[MAXN+10]; node *Fa[MAXN+10]; void addedge(int u, int v, int ca, int co){ ++ecnt; ecnt->v = v; ecnt->cap = ca; ecnt->cost = co; ecnt->next = adj[u]; ecnt->back = ecnt+1; adj[u] = ecnt; ++ecnt; ecnt->v = u; ecnt->cap = 0; ecnt->cost = -co; ecnt->next = adj[v]; ecnt->back = ecnt-1; adj[v] = ecnt; } int ncnt, s, t; bool SPFA(){ for(int i=1;i<=ncnt;i++) dis[i]=INF; dis[s] = 0; queue<int> que; que.push(s); res[s] = INF; while(!que.empty()){ int u = que.front(); que.pop(); for(node *p=adj[u];p;p=p->next){ if(p->cap == 0) continue; if(dis[p->v] <= dis[u] + p->cost) continue; dis[p->v] = dis[u] + p->cost; Fa[p->v] = p; res[p->v] = min(res[u], p->cap); que.push(p->v); } } return !(dis[t] == INF); } int work(){ int ret = 0; while(SPFA()){ ret += res[t] * dis[t]; int now = t; while(now != s){ Fa[now]->cap -= res[t]; Fa[now]->back->cap += res[t]; now = Fa[now]->back->v; } } return ret; } struct Ts{int t[60], co[60][60];}T[110], Ha[110]; int Sum[60]; int main(){ int N, M, K; while(~scanf("%d%d%d", &N, &M, &K)&&N&&M&&K){ int ans = 0; memset(Sum, 0, sizeof Sum); for(int i=1;i<=N;i++) for(int j=1;j<=K;j++){ scanf("%d", &T[i].t[j]); Sum[j] += T[i].t[j]; } for(int i=1;i<=M;i++) for(int j=1;j<=K;j++){ scanf("%d", &Ha[i].t[j]); Sum[j] -= Ha[i].t[j]; } bool flag = false; for(int j=1;j<=K;j++) if(Sum[j] > 0){ printf("-1\n"); flag = true; break; } for(int k=1;k<=K;k++) for(int i=1;i<=N;i++) for(int j=1;j<=M;j++) scanf("%d", &T[i].co[k][j]); if(flag) continue; ncnt = N+M+2; s=1, t=N+M+2; for(int k=1;k<=K;k++){ memset(adj, 0, sizeof adj); ecnt=Edges; for(int i=1;i<=M;i++) addedge(s, i+1, Ha[i].t[k], 0); for(int i=1;i<=N;i++){ addedge(i+M+1, t, T[i].t[k], 0); for(int j=1;j<=M;j++){ addedge(j+1, i+M+1, INF, T[i].co[k][j]); } } ans += work(); } printf("%d\n", ans); } return 0; }