poj 2516Minimum Cost

http://poj.org/problem?id=2516

  1 #include<cstdio>

  2 #include<cstring>

  3 #include<algorithm>

  4 #include<queue>

  5 #define maxn 110

  6 using namespace std;

  7 

  8 int need[maxn][maxn];

  9 int sup[maxn][maxn];

 10 int cost[maxn][maxn][maxn];

 11 int cap[maxn][maxn];

 12 int flow[maxn][maxn];

 13 int cost1[maxn][maxn];

 14 int d[maxn];

 15 int p[maxn];

 16 int n,m,k;

 17 const int inf=1<<30;

 18 

 19 int main()

 20 {

 21     while(scanf("%d%d%d",&n,&m,&k)!=EOF){

 22         memset(p,0,sizeof(p));

 23         memset(cost1,0,sizeof(cost1));

 24         memset(need,0,sizeof(need));

 25         if(n==0&&m==0&&k==0) break;

 26         for(int i=1; i<=n; i++){

 27             for(int j=1; j<=k; j++)

 28             {

 29                 scanf("%d",&need[i][j]);

 30             }

 31         }

 32         for(int i=1; i<=m; i++)

 33         {

 34             for(int j=1; j<=k; j++)

 35             {

 36                 scanf("%d",&sup[i][j]);

 37             }

 38         }

 39         for(int c=1; c<=k; c++)

 40         {

 41             for(int i=1; i<=n; i++)

 42             {

 43                 for(int j=1; j<=m; j++)

 44                 {

 45                     scanf("%d",&cost[c][j][i]);

 46                 }

 47             }

 48         }

 49         int s=0,t=n+m+1;

 50         bool flag=true;

 51         int tt=0;

 52         for(int i=1; i<=k; i++)

 53         {

 54             memset(cap,0,sizeof(cap));

 55             for(int j=1; j<=m; j++)

 56                  cap[s][j]=sup[j][i];

 57             for(int j=1; j<=m; j++)

 58             {

 59                 for(int c=1; c<=n; c++)

 60                 {

 61                     cap[j][m+c]=sup[j][i];

 62                 }

 63             }

 64             for(int j=1; j<=m; j++)

 65                cap[j+m][t]=need[j][i];

 66             memset(flow,0,sizeof(flow));

 67             for(int j=1; j<=m; j++)

 68             {

 69                 for(int c=1; c<=n; c++)

 70                 {

 71                     cost1[j][c+m]=cost[i][j][c];

 72                     cost1[c+m][j]=-cost1[j][c+m];

 73                 }

 74             }

 75             queue<int>q;

 76             memset(flow,0,sizeof(flow));

 77             int mm=0;

 78             for(;;)

 79             {

 80                 bool inq[maxn];

 81                 for(int i=0; i<=n+m+1; i++) d[i]=(i==s?0:inf);

 82                 memset(inq,false,sizeof(inq));

 83                 q.push(s);

 84                 while(!q.empty())

 85                 {

 86                     int u=q.front(); q.pop();

 87                     inq[u]=false;

 88                     for(int v=0; v<=n+m+1; v++) if(cap[u][v]>flow[u][v]&&d[v]>d[u]+cost1[u][v])

 89                     {

 90                         d[v]=d[u]+cost1[u][v];

 91                         p[v]=u;

 92                         if(!inq[v])

 93                         {

 94                             inq[v]=true;

 95                             q.push(v);

 96                         }

 97                     }

 98                 }

 99                 if(d[t]==inf) break;

100                 int a=inf;

101                 for(int u=t; u!=s; u=p[u])

102                 {

103                     if(cap[p[u]][u]-flow[p[u]][u]<a)

104                     {

105                         a=cap[p[u]][u]-flow[p[u]][u];

106                     }

107                 }

108                 for(int u=t; u!=s; u=p[u])

109                 {

110                     flow[p[u]][u]+=a;

111                     flow[u][p[u]]-=a;

112                 }

113                 mm+=d[t]*a;

114             }

115             for(int j=1; j<=n; j++)

116             {

117                 if(flow[j+m][t]!=cap[j+m][t])

118                 {

119                     flag=false;

120                     break;

121                 }

122             }

123             if(!flag) break;

124             if(flag) tt+=mm;

125         }

126         if(flag) printf("%d\n",tt);

127         else printf("-1\n");

128     }

129     return 0;

130 }
View Code

 

你可能感兴趣的:(poj)