HDU 4307 Matrix (最小割)
来源:互联网 发布:淘宝客api开发 编辑:程序博客网 时间:2024/05/22 03:41
Matrix
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 823 Accepted Submission(s): 210
Problem Description
Let A be a 1*N matrix, and each element of A is either 0 or 1. You are to find such A that maximize D=(A*B-C)*AT, where B is a given N*N matrix whose elements are non-negative, C is a given 1*N matrix whose elements are also non-negative, and AT is the transposition of A (i.e. a N*1 matrix).
Input
The first line contains the number of test cases T, followed by T test cases.
For each case, the first line contains an integer N (1<=N<=1000).
The next N lines, each of which contains N integers, illustrating the matrix B. The jth integer on the ith line is B[i][j].
Then one line followed, containing N integers, describing the matrix C, the ith one for C[i].
You may assume that sum{B[i][j]} < 2^31, and sum{C[i]} < 2^31.
For each case, the first line contains an integer N (1<=N<=1000).
The next N lines, each of which contains N integers, illustrating the matrix B. The jth integer on the ith line is B[i][j].
Then one line followed, containing N integers, describing the matrix C, the ith one for C[i].
You may assume that sum{B[i][j]} < 2^31, and sum{C[i]} < 2^31.
Output
For each case, output the the maximum D you may get.
Sample Input
131 2 13 1 01 2 32 3 7
Sample Output
2HintFor sample, A=[1, 1, 0] or A=[1, 1, 1] would get the maximum D.
Author
BUPT
Source
2012 Multi-University Training Contest 1
Recommend
zhuyuanchen520
网上看的题解,一看是矩阵就不是很想碰的题。看过题解才知道,最小割的应用还是很强的。
参考题解出处:http://blog.csdn.net/weiguang_123/article/details/8077385
思路:
如果Ai选择0会产生sum{Bij}(1<=j<=N)的代价,如果Ai选择1会产生Ci的代价,如果Ai选择1且aj选择0就会产生Bij的代价。这样就可以建模了。
设源点和汇点,如果Ai选0则从源点到i连接一条容量为sum(Bij)的边,如果Ai选1则从i到汇点连接一条容量为Ci的边,对于从i到j的边,即Ai选了1,并且Aj选了0的边,其容量就为Bij。则从源点到汇点的最大流即最小割,那么D最大即sum(Bij)(1<=i<=N,1<=j<=N)-最小割。
#include<cstdio>using namespace std;const int mm=2005005;const int mn=1005;const int oo=1000000000;int node,s,t,edge;int to[mm],flow[mm],next[mm];int head[mn],work[mn],dis[mn],q[mn];inline int min(int a,int b){ return a<b?a:b;}inline void init(int nn,int ss,int tt){ node=nn,s=ss,t=tt,edge=0; for(int i=0; i<node; ++i) head[i]=-1;}inline void add(int u,int v,int c1,int c2=0){ to[edge]=v,flow[edge]=c1,next[edge]=head[u],head[u]=edge++; to[edge]=u,flow[edge]=c2,next[edge]=head[v],head[v]=edge++;}bool bfs(){ int i,u,v,l,r=0; for(i=0; i<node; ++i) dis[i]=-1; dis[q[r++]=s]=0; for(l=0; l<r; ++l) for(i=head[u=q[l]]; i>=0; i=next[i]) if(flow[i]&&dis[v=to[i]]<0) { dis[q[r++]=v]=dis[u]+1; if(v==t)return 1; } return 0;}int dfs(int u,int maxf){ if(u==t) return maxf; for(int &i=work[u],v,tmp; i>=0; i=next[i]) if(flow[i]&&dis[v=to[i]]==dis[u]+1&&(tmp=dfs(v,min(maxf,flow[i])))>0) { flow[i]-=tmp; flow[i^1]+=tmp; return tmp; } return 0;}long long dinic(){ int i,delta; long long ret=0; while(bfs()) { for(i=0; i<node; ++i) work[i]=head[i]; while(delta=dfs(s,oo))ret+=delta; } return ret;}int main(){ int i,j,n,a,T; scanf("%d",&T); while(T--) { scanf("%d",&n); init(n+2,0,n+1); long long fcnt=0; for(i=1; i<=n; ++i) { int ss=0; for(j=1; j<=n; ++j) { scanf("%d",&a),ss+=a; add(i,j,a); } add(s,i,ss); fcnt+=ss; } for(int i=1; i<=n; i++) { scanf("%d",&a); add(i,t,a); } printf("%I64d\n",fcnt-dinic()); } return 0;}
- HDU 4307 Matrix (最小割)
- 【最小割】【hdu 4307】Matrix
- hdu 4307 Matrix( 公式化最小割)
- 【HDU】4307 Matrix 最小割——项目分配问题
- HDU 4307 Matrix 最小割 矩阵乘法展开
- HDU 4307 Matrix 最小割模型求未知矩阵最大值
- HDU 4037 Matrix 最小割模型转换
- hdu4307 Matrix 最小割
- 【TOJ 3872.】Matrix【最小割】
- hdu 3996 最小割
- HDU 3491 最小割
- HDU 3251 最小割
- hdu 3987 最小割
- hdu 3452 最小割
- HDU 4859 最小割
- hdu 3046 最小割
- HDU 3491 最小割
- HDU 3526 最小割
- 【python】python数据结构
- offline.appcache,html5缓存清单文件简单说明
- Ubuntu12.04 讯雷安装与使用
- MYSQL在众多表中查询表名和字段名
- MySql优化 10个技巧
- HDU 4307 Matrix (最小割)
- LoadRunner监控Linux与Windows方法
- innoDB锁问题-《深入浅出Mysql--数据库开发、优化与管理维护》
- shell 中for的测试脚本
- JAVA冒泡排序
- xml文件的解析--libxml库函数解析
- MTK feature phone 音频参数配置
- 点击checkbox使用js取table中tr下td中的各个标签value
- kernel 学习 : 文件系统