Ural-1076 Trash(最小费用最大流算法)

Description
You were just hired as CEO of the local junkyard.One of your jobs is dealing with the incoming trash and sorting it for recycling.The trash comes every day in N containers and each of these containers contains certain amount of each of the N types of trash. Given the amount of trash in the containers find the optimal way to sort the trash. Sorting the trash means putting every type of trash in separate container. Each of the given containers has infinite capacity. The effort for moving one unit of trash from container i to j is 1 if i ≠ j otherwise it is 0.You are to minimize the total effort.
Input
The first line contains the number N (1 ≤ N ≤ 150), the rest of the input contains the descriptions of the containers.The (1 + i)-th line contains the description of the i-th container the j-th amount (0 ≤ amount ≤ 100) on this line denotes the amount of the j-th type of trash in the i-th container.
Output
You should write the minimal effort that is required for sorting the trash.
Sample Input
input
4
62 41 86 94
73 58 11 12
69 93 89 88

81 40 69 13

output

650

最小费用最大流模板题。

但是要注意一定要建立反向边!! 这一点不要忘了。




  1. #include<iostream>  
  2. #include<stdio.h>  
  3. #include<string.h>  
  4. using namespace std;  
  5. #define Max 310  
  6. #define inf 0x3f3f3f3f  
  7. int n, ans,en,st;  
  8. int cap[Max][Max], pre[Max];  
  9. int cost[Max][Max], dis[Max];  
  10. int que[Max];  
  11. bool vis[Max];  
  12. int r[Max][Max],s[Max];  
  13. bool spfa(){                  //  源点为0,汇点为n。  
  14.     int i, head = 0, tail = 1;  
  15.     for(i = 1; i <= n; i ++){  
  16.         dis[i] = inf;  
  17.         vis[i] = false;  
  18.     }  
  19.     dis[st] = 0;  
  20.     que[0] = st;  
  21.     vis[st] = true;  
  22.     while(tail != head){      //  循环队列。  
  23.         int u = que[head];  
  24.         for(i = 1; i <= n; i ++)  
  25.             if(cap[u][i] && dis[i] > dis[u] + cost[u][i]){    //  存在路径,且权值变小。  
  26.                 dis[i] = dis[u] + cost[u][i];  
  27.                 pre[i] = u;  
  28.                 if(!vis[i]){  
  29.                     vis[i] = true;  
  30.                     que[tail ++] = i;  
  31.                     if(tail == Max) tail = 0;  
  32.                 }  
  33.             }  
  34.         vis[u] = false;  
  35.         head ++;  
  36.         if(head == Max) head = 0;  
  37.     }  
  38.     if(dis[en] == inf) return false;  
  39.     return true;  
  40. }  
  41.    
  42. void end(){  
  43.     int i, sum = inf;  
  44.     for(i = en; i != st; i = pre[i])  
  45.         sum =sum<cap[pre[i]][i]?sum:cap[pre[i]][i];  
  46.     for(i = en; i != st; i = pre[i]){  
  47.         cap[pre[i]][i] -= sum;  
  48.         cap[i][pre[i]] += sum;  
  49.         ans += cost[pre[i]][i] * sum;   //  cost[][]记录的为单位流量费用,必须得乘以流量。  
  50.     }  
  51. }  
  52.    
  53. int main(){  
  54.     int i,j;  
  55.     scanf("%d",&n);  
  56.     memset(cap,0,sizeof(cap));  
  57.     memset(s,0,sizeof(s));  
  58.     for(i=1;i<=n;i++)  
  59.         for(j=1;j<=n;j++)  
  60.         {  
  61.             scanf("%d",&r[i][j+n]);  
  62.             s[i]+=r[i][j+n];  
  63.         }  
  64.     for(i=1;i<=n;i++)  
  65.         for(j=1;j<=n;j++)  
  66.         {  
  67.             //scanf("%d",&cost[i][j+n]);  
  68.             cost[i][j+n]=s[i]-r[i][j+n];  
  69.             cap[i][j+n]=1;  
  70.             cap[j+n][i]=0;  
  71.             cost[j+n][i]=-cost[i][j+n];  
  72.         }  
  73.         st=2*n+1;  
  74.         en=2*n+2;  
  75.         for(i=1;i<=n;i++)  
  76.         {  
  77.             cap[st][i]=1;  
  78.             cap[i][st]=0;  
  79.             cost[st][i]=cost[i][st]=0;  
  80.             cap[i+n][en]=1;  
  81.             cap[en][i+n]=0;  
  82.             cost[en][i+n]=cost[i+n][en]=0;  
  83.         }  
  84.     ans = 0;  
  85.     n=2*n+2;  
  86.     while(spfa()) end();  
  87.     printf("%d\n",ans);  
  88.     return 0;  
  89. }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值