2014多校聯合第十場A題||hdu 4971 最小割定理在最大權閉合圖上的應用
http://acm.hdu.edu.cn/showproblem.php?pid=4971
Problem Description
There's a company with several projects to be done. Finish a project will get you profits. However, there are some technical problems for some specific projects. To solve the problem, the manager will train his employee which may cost his budget. There may
be dependencies between technical problems, for example, A requires B means you need to solve problem B before solving problem A. If A requires B and B requires A, it means that you should solve them at the same time. You can select which problems to be solved
and how to solve them freely before finish your projects. Can you tell me the maximum profit?
Input
The first line of the input is a single integer T(<=100) which is the number of test cases.
Each test case contains a line with two integer n(<=20) and m(<=50) which is the number of project to select to complete and the number of technical problem.
Then a line with n integers. The i-th integer(<=1000) means the profit of complete the i-th project.
Then a line with m integers. The i-th integer(<=1000) means the cost of training to solve the i-th technical problem.
Then n lines. Each line contains some integers. The first integer k is the number of technical problems, followed by k integers implying the technical problems need to solve for the i-th project.
After that, there are m lines with each line contains m integers. If the i-th row of the j-th column is 1, it means that you need to solve the i-th problem before solve the j-th problem. Otherwise the i-th row of the j-th column is 0.
Each test case contains a line with two integer n(<=20) and m(<=50) which is the number of project to select to complete and the number of technical problem.
Then a line with n integers. The i-th integer(<=1000) means the profit of complete the i-th project.
Then a line with m integers. The i-th integer(<=1000) means the cost of training to solve the i-th technical problem.
Then n lines. Each line contains some integers. The first integer k is the number of technical problems, followed by k integers implying the technical problems need to solve for the i-th project.
After that, there are m lines with each line contains m integers. If the i-th row of the j-th column is 1, it means that you need to solve the i-th problem before solve the j-th problem. Otherwise the i-th row of the j-th column is 0.
Output
For each test case, please output a line which is "Case #X: Y ", X means the number of the test case and Y means the the maximum profit.
Sample Input
4
2 3
10 10
6 6 6
2 0 1
2 1 2
0 1 0
1 0 0
0 0 0
2 3
10 10
8 10 6
1 0
1 2
0 1 0
1 0 0
0 0 0
2 3
10 10
8 10 6
1 0
1 2
0 1 0
0 0 0
0 0 0
2 3
10 10
8 10 6
1 0
1 2
0 0 0
1 0 0
0 0 0
Sample Output
Case #1: 2
Case #2: 4
Case #3: 4
Case #4: 6
解題思路:最大權閉合圖問題,經典解法見胡伯濤的論文,最小割模型在資訊學競賽中的應用,有一個公式:最大權閉合圖的權值=所有的正權邊 - 最小割。
程式碼如下:
#include<cstdio>
#include<iostream>
using namespace std;
const int oo=1e9;
const int mm=111111;
const int mn=999;
int node,src,dest,edge;
int ver[mm],flow[mm],next[mm];
int head[mn],work[mn],dis[mn],q[mn];
void prepare(int _node,int _src,int _dest)
{
node=_node,src=_src,dest=_dest;
for(int i=0; i<node; ++i)head[i]=-1;
edge=0;
}
void addedge(int u,int v,int c)
{
ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;
ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}
bool Dinic_bfs()
{
int i,u,v,l,r=0;
for(i=0; i<node; ++i)dis[i]=-1;
dis[q[r++]=src]=0;
for(l=0; l<r; ++l)
for(i=head[u=q[l]]; i>=0; i=next[i])
if(flow[i]&&dis[v=ver[i]]<0)
{
dis[q[r++]=v]=dis[u]+1;
if(v==dest)return 1;
}
return 0;
}
int Dinic_dfs(int u,int exp)
{
if(u==dest)return exp;
for(int &i=work[u],v,tmp; i>=0; i=next[i])
if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
{
flow[i]-=tmp;
flow[i^1]+=tmp;
return tmp;
}
return 0;
}
int Dinic_flow()
{
int i,ret=0,delta;
while(Dinic_bfs())
{
for(i=0; i<node; ++i)work[i]=head[i];
while(delta=Dinic_dfs(src,oo))ret+=delta;
}
return ret;
}
int n,m;
int main()
{
int T,tt=0;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
prepare(n+m+2,0,n+m+1);
int x,sum=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
sum+=x;
addedge(src,i,x);
}
for(int i=n+1;i<=n+m;i++)
{
scanf("%d",&x);
addedge(i,dest,x);
}
int k;
for(int i=1;i<=n;i++)
{
scanf("%d",&k);
while(k--)
{
scanf("%d",&x);
addedge(i,x+n+1,oo);
}
}
for(int i=n+1;i<=m+n;i++)
for(int j=n+1;j<=m+n;j++)
{
scanf("%d",&x);
if(x&1)
addedge(i,j,oo);
}
printf("Case #%d: %d\n",++tt,sum-Dinic_flow());
}
return 0;
}
相關文章
- 2014多校聯合第9場1011題||hdu 4970 樹狀陣列陣列
- 2014多校聯合第9場1006||hdu 4965 矩陣乘法和快速冪矩陣
- 關於二分圖上的最大匹配、最小點覆蓋、最大獨立集以及最大權閉合子圖的聯絡
- 1.1.3.3 最小割之最小權覆蓋集、最大權獨立集
- HDU 5389 Zero Escape(2015年多校聯合第八場 動態規劃)動態規劃
- spoj 839 最小割應用
- hdu5289||2015多校聯合第一場1002貪心+RMQMQ
- hdu5336 多校聯合第四場1010 模擬+bfs優先佇列佇列
- HDU 5375 Gray code(2015年多校聯合 動態規劃)動態規劃
- 網路流(最大流,最小割)
- HDU4965Fast Matrix Calculation(2014多校第九場)AST
- 陳老師的多校聯合 D題 字串處理起來挺麻煩字串
- matlab練習程式(最大流/最小割)Matlab
- 偷寶石(最大流轉化最小割)
- 快速求圖上最小點定聯通塊權值的Trick
- 計訊物聯智慧合杆在智慧城市中的應用
- POJ 3308 Paratroopers 最小割、最大流OOP
- [精]mysql聯合主鍵應用MySql
- 滲透測試工具多個應用場合介紹
- HDU 4349 Xiao Ming's Hope (Lucas定理的應用)
- 聯合體在微控制器程式設計中的應用程式設計
- 【dp+組合數學】hdu 2018 多校第九場 1001 Rikka with Nash Equilibrium hdu 6415UI
- 陳老師的多校聯合20140816A題||spoj 10228 動態規劃動態規劃
- 網路流最大流、最小割學習筆記筆記
- 在已存在的表結構上新增主鍵、外來鍵、聯合主鍵、聯合索引的例子索引
- MerkleTree在資料校驗上的應用
- 智慧合約從入門到精通:智慧合約的應用場景
- 兩個TABALE在聯合查詢很慢的問題
- P1351 [NOIP2014 提高組] 聯合權值
- 模擬量輸出的常用應用場合
- Polygon馬蹄鏈在以太坊上的智慧合約開發應用Go
- X問題(中國剩餘定理+不互質版應用)hdu1573
- .NET Attribute在資料校驗上的應用
- 在實際應用中聯合體union的妙用
- Java Volatile的一個實際應用場合Java
- 系統最小的服務最小的許可權最大的安全。
- HTML標籤 閉合還是不閉合?HTML
- HTML標籤,閉合還是不閉合?HTML