標籤:des style blog color os io strong for
解題報告
題意:
有n個商店,m個供應商,k種商品</span>n*k的矩陣,表示每個商店需要每個商品的數目;m*k矩陣,表示每個供應商擁有每個商品的個數然後對於每個物品k,都有n*m的矩陣i行j列表示從j供應商向i商店運送一個k商品的代價是多少判斷所有的倉庫能否滿足所有客戶的需求,如果可以,求出最少的運輸總費用
思路:
建圖的題,不能直接把所有資訊建成圖,因為n和m跟k都有關係,如果那樣子建圖的話,就要把k種拆成m類,每個倉庫連向該倉庫的第k種,然後再和n連線,有費用,
不過這樣其實不行的,會發現n跟k的關係沒辦法解決
如果把k種商品分k次處理就行了,相當於一次運一種
對於每一種商品,建一次圖,求最小費用,如果最大流不能滿足顧客的需求就pass。
#include <iostream>#include <cstdio>#include <cstring>#include <queue>#define inf 0x3f3f3f3fusing namespace std;struct E { int v,cost,cap,next;} edge[100000];int head[5000],dis[5000],pre[5000],vis[5000],f[5000],m1[55][55],m2[55][55],s,t,n,m,k,cnt,cost,flow;void add(int u,int v,int cost,int cap) { edge[cnt].v=v; edge[cnt].cost=cost; edge[cnt].cap=cap; edge[cnt].next=head[u]; head[u]=cnt++; edge[cnt].v=u; edge[cnt].cost=-cost; edge[cnt].cap=0; edge[cnt].next=head[v]; head[v]=cnt++;}int _spfa() { for(int i=s; i<=t; i++) { dis[i]=inf,vis[i]=pre[i]=f[i]=0; } dis[s]=0,vis[s]=1,pre[s]=-1,f[s]=inf; queue<int>Q; Q.push(s); while(!Q.empty()) { int u=Q.front(); Q.pop(); vis[u]=0; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(edge[i].cap&&dis[v]>dis[u]+edge[i].cost) { pre[v]=i; dis[v]=dis[u]+edge[i].cost; f[v]=min(f[u],edge[i].cap); if(!vis[v]) { vis[v]=1; Q.push(v); } } } } if(dis[t]==inf)return 0; flow+=f[t]; cost+=f[t]*dis[t]; for(int i=pre[t]; i!=-1; i=pre[edge[i^1].v]) { edge[i].cap-=f[t]; edge[i^1].cap+=f[t]; } return 1;}void mcmf() { cost=flow=0; while(_spfa());}int main() { int i,j,l,a,b,c; while(~scanf("%d%d%d",&n,&m,&k)) { int sum[1000]; s=0,t=m+n+1; if(!n&&!m&&!k)break; memset(sum,0,sizeof(sum)); memset(m1,0,sizeof(m1)); memset(m2,0,sizeof(m2)); for(i=1; i<=n; i++) { for(j=1; j<=k; j++) { scanf("%d",&m1[i][j]); sum[j]+=m1[i][j]; } } for(i=1; i<=m; i++) { for(j=1; j<=k; j++) scanf("%d",&m2[i][j]); } int f=0; int ans=0; for(l=1; l<=k; l++) { cnt=0; memset(head,-1,sizeof(head)); memset(edge,0,sizeof(edge)); for(i=1; i<=n; i++) { for(j=1; j<=m; j++) { scanf("%d",&a); add(j,m+i,a,inf); } } if(f)continue; for(i=1; i<=m; i++) add(s,i,0,m2[i][l]); for(i=1; i<=n; i++) add(m+i,t,0,m1[i][l]); mcmf(); if(flow<sum[l]) { f=1; } else ans+=cost; } if(f) printf("-1\n"); else printf("%d\n",ans); }}
Minimum Cost
| Time Limit: 4000MS |
|
Memory Limit: 65536K |
| Total Submissions: 13529 |
|
Accepted: 4633 |
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 10 1 11 2 21 0 11 2 31 1 12 1 11 1 132200 0 0
Sample Output
4-1