1076. Trash(KM算法 二分最佳完美匹配)
2015-06-25 20:35
330 查看
1076. Trash
Time limit: 1.0 secondMemory limit: 64 MB
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-thcontainer 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 | output |
---|---|
4 62 41 86 94 73 58 11 12 69 93 89 88 81 40 69 13 | 650 |
Tags: graph theory (
hide tags for unsolved problems
)
天呢!原谅哥这次套一次 自己没有理解的模版吧 ~感觉好难受~ ,时间紧迫 !~也只能这样了。测试了一下模版是对的。1a,然后我就会把它弄到我的小模版库~,的确 有些东西确实很难理解,但是应该用这个时间干点儿更有意义的事儿 !第一次套黑盒代码。嘿嘿~
#include<iostream> #include<sstream> #include<algorithm> #include<cstdio> #include<string.h> #include<cctype> #include<string> #include<cmath> #include<vector> #include<stack> #include<queue> #include<map> #include<set> using namespace std; const int INF=10000; int W[INF][INF],n; int Lx[INF],Ly[INF];// int Left[INF]; // bool S[INF],T[INF];// int sum[INF]; int matrix[INF][INF]; bool match(int i) { S[i]=true; for(int j=1; j<=n; j++) if(Lx[i]+Ly[j]==W[i][j]&&!T[j]) { T[j]=true; if(!Left[j]||match(Left[j])) { Left[j]=i; return true; } } return false; } void update() { int a=1<<30; for(int i=1; i<=n; i++)if(S[i]) for(int j=1; j<=n; j++)if(!T[j]) a=min(a,Lx[i]+Ly[j]-W[i][j]); for(int i=1; i<=n; i++) { if(S[i])Lx[i]-=a; if(T[i])Ly[i]+=a; } } int KM() { for(int i=1; i<=n; i++) { Left[i]=Lx[i]=Ly[i]=0; for(int j=1; j<=n; j++) Lx[i]=max(Lx[i],W[i][j]); } for(int i=1; i<=n; i++) { for(;;) { for(int j=1; j<=n; j++) S[j]=T[j]=0; if(match(i))break; else update(); } } int sum = 0; for(int i = 1; i <= n; i ++) if(match[i] > 0) sum += W[Left[i]][i]; return sum; } int main() { while(cin>>n) { memset(sum,0,sizeof(sum)); for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) { scanf("%d",&matrix[i][j]); sum[j]+=matrix[i][j]; } for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) W[i][j]=-(sum[j]-matrix[i][j]); cout<<-KM()<<endl; } return 0; }
相关文章推荐
- 【poj 2533】Longest Ordered Subsequence
- linux zip/unzip命令详解
- POJ 1166 暴力搜索 即 枚举
- [Codeforces Round #309 (Div. 2)] #ABC题解
- python中的__new__和__init__
- [LeetCode] Jump Game II
- Java 中的成员内部类(转载)
- 求算符文法的FIRSTVT集的算法
- Android 中 使用 Gson解析json
- pycharm的vim插件安装
- Android基础知识_Intent概念及实现
- C与C++代码互相访问
- Hibernate入门之配置文件
- Java命名规范
- lintcode:Continuous Subarray Sum
- SpringMVC将数据返回到页面
- ixgbe 82599 固定源与目标, UDP, 64字节小包, 1488w pps 单核CPU软中断sirq 100%
- shuffle和排序
- 为HT公司搭建一台 web 服务器
- 微软100题第46题:四对括号可以有多少种匹配排列方式?