您的位置:首页 > 其它

(根据物品多次建图+最小费用最大流) poj 2516 Minimum Cost

2010-09-23 00:26 513 查看
Minimum Cost

Time Limit: 4000MSMemory Limit: 65536K
Total Submissions: 7054Accepted: 2305
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
/*
题目大意:k种物品有M个供货点提供给N个商家,不同供货点运送不同物品到不同商家需要不同费用。求最小费用,如果没有输出-1
题目解答:根据物品多次建图,然后用最小费用最大流解答
Source Code

Problem: 2516  User: wawadimu
Memory: 6292K  Time: 547MS
Language: C++  Result: Accepted

Source Code
*/
#include<iostream>
#include<queue>
using namespace std;

#define max 60
#define inf INT_MAX
int SK[max][max],SP[max][max],dis[max][2*max][2*max];//商店(人)SK
[K],供货商SP[M][k],顶点距离dis[K][M][M+N]
int map[max][max][max],cap[max][2*max][2*max],flow[2*max][2*max];//地图map[K]
[M],容量cap[K][M][M+N],当前流量flow[K][M][M+N]
int sum[max];//第i种物品需要的数目==sum[M]
int p[2*max],d[2*max];//路径数组,距离
bool vis[2*max];//标记数组
int K,N,M;

//
//SPFA求增广路径
//
bool Bellman_ford(int k,int S,int T)
{
int i,j;
int u,v;
queue<int> q;
q.push(S);
memset(p,-1,sizeof(p));
memset(vis,0,sizeof(vis));
vis[S]=true;
for(i=0;i<=T;i++) d[i]=inf;
d[S]=0;
while(!q.empty())
{
u=q.front(); q.pop();
vis[u]=false;
for(v=0;v<=T;v++)
{
//为了避免溢出(dis[k][u][v]!=inf),不然溢出后就整形符号位变1,变成负数
if(cap[k][u][v] > flow[u][v]  && dis[k][u][v]!=inf && d[v] > d[u] + dis[k][u][v] )
{
//cout<<u<<"->"<<v<<":"<<d[u]<<"->"<<d[v]<<"  ";
d[v]=d[u] + dis[k][u][v];
p[v]=u;

if(!vis[v]) { vis[v]=true; q.push(v);}
}
}
}
if(d[T]!=inf) return true;
else return false;

}
int min_fee_max_flow(int k,int S,int T)
{
memset(flow,0,sizeof(flow));
int c=0,f=0;
int u,a;
while(Bellman_ford(k,S,T))
{
a=inf;
//
//计算最大增益流量
//
for(u=T;u!=S;u=p[u])
{
//cout<<u<<"  ";
a= a > cap[k][p[u]][u]-flow[p[u]][u] ? cap[k][p[u]][u]-flow[p[u]][u] : a ;
}

for(u=T;u!=S;u=p[u])
{
flow[p[u]][u]+=a;
flow[u][p[u]]-=a;
}
c+=a*d[T];//这里的费用=数目*d[T](即单件费用)
f+=a;     //记录最大流量
}
if(f!=sum[k]) return -1;
else return c;//满载返回总费用
}
int minicost(int k,int S,int T)
{
int i;
int ans=0;
for(i=1;i<=k;i++)//对每一件物品最大流最小费用
{
int cost=min_fee_max_flow(i,S,T);
if(cost>0) ans+=cost;
else {ans=-1;break;}//不能达成最大流
}
return ans;
}
int main()
{
//freopen("2516.txt","r",stdin);
int i,j,h;
while(scanf("%d%d%d",&N,&M,&K)!=EOF && !(N==0 && M==0 && K==0))
{
memset(sum,0,sizeof(sum));
memset(cap,0,sizeof(cap));
for(i=1;i<=N;i++)
{
for(j=1;j<=K;j++)
{
scanf("%d",&SK[i][j]);
//printf("%d ",SK[i][j]);
sum[j]+=SK[i][j];//第j种物品的需求量
}
}
for(i=1;i<=M;i++)
{
for(j=1;j<=K;j++)
{
scanf("%d", &SP[i][j]);
//printf("%d ",SP[i][j]);
}
}
for(i=0;i<=K;i++)
for(j=0;j<=N+M+1;j++)
for(h=0;h<=N+M+1;h++)
dis[i][j][h]=inf;
for(i=1;i<=K;i++)
{
for(j=1;j<=N;j++)
{
cap[i][M+j][N+M+1]=SK[j][i];//人->汇点流量
dis[i][M+j][N+M+1]=0;//人->汇点距离(可以不用汇点到人,因为这里不会有退流)dis[i][N+M+1][M+j]=
for(h=1;h<=M;h++)
{
cap[i][0][h]=SP[h][i];  //源点->供应流量
cap[i][h][M+j]=SP[h][i];
dis[i][0][h]=dis[i][h][0]=0;//源点->供应距离,源点<-供应距离
scanf("%d",&map[i][j][h]);
//printf("%d ",map[i][j][h]);
dis[i][h][M+j]=map[i][j][h];//供应->人距离
dis[i][M+j][h]=-dis[i][h][M+j];//反向
}
}
}
int ans=minicost(K,0,N+M+1);
printf("%d/n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: