Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 826 Accepted Submission(s): 291
Problem Description
Now, Bob is playing an interesting game in which he is a general of a harmonious army. There are n soldiers in this army. Each soldier should be in one of the two occupations, Mage or Warrior. There are m pairs of soldiers having combination ability. There are three kinds of combination ability. If the two soldiers in a pair are both Warriors, the army power would be increased by a. If the two soldiers in a pair are both Mages, the army power would be increased by c. Otherwise the army power would be increased by b, and b=a/4+c/3, guaranteed that 4|a and 3|c. Your task is to output the maximum power Bob can increase by arranging the soldiers' occupations.
Note that the symbol a|b means that a divides b, e.g. , 3|12 and 8|24.
Input
There are multiple test cases.
Each case starts with a line containing two positive integers n(n≤500) and m(m≤104).
In the following m lines, each line contains five positive integers u,v,a,b,c (1≤u,v≤n,u≠v,1≤a,c≤4×106,b=a/4+c/3), denoting soldiers u and vhave combination ability, guaranteed that the pair (u,v) would not appear more than once.
It is guaranteed that the sum of n in all test cases is no larger than 5×103, and the sum of m in all test cases is no larger than 5×104.
Output
For each test case, output one line containing the maximum power Bob can increase by arranging the soldiers' occupations.
Sample Input
3 2 1 2 8 3 3 2 3 4 3 6
Sample Output
12
题意:有n对士兵,其中每队如果都加入A阵营就会得到a价值,都加入B阵营就会得到c价值,否则就会得到b价值,求如何分配这n对士兵使得价值总和最大?
题解:跟我上次写得洛谷1361是同种类型的,都是构造出最小割模型,然后用总的减去跑出的最小割(也就是最大割)
对每个士兵建立一个点x ,点x 向源点s 连一条边,向汇点t 连一条边, 分别表示选择两种职业,然后就可以先加上所有的贡献,通过两点关系用 最小割建模,如下图所示。
然后就可以得到
a+b=A+B(该对士兵选B阵营)
c+d=B+C(该对士兵选A阵营)
a+e+d=A+C(一人选A另一人选B)
b+e+c=A+C(一人选B另一人选A)
然后假设a=b=(A+B)/2,可求出c=d=(B+C)/2,e=(a+c-2*b)/2
然后建图跑就行了
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e6+9;
const int INF = 0x3f3f3f3f;
typedef long long ll;
int a[maxn],b[maxn];
int head[maxn],cur[maxn];
int tot=0;
struct rt{
int next,v,c;
}edge[maxn];
void add_edge(int u,int v,int c){
edge[tot].v=v;
edge[tot].c=c;
edge[tot].next=head[u];
head[u]=tot++;
}
int level[maxn];
bool bfs(int s,int t){
for(int i=0;i<=t+100;i++)level[i]=-1;
level[s]=0;
queue<int>que;
que.push(s);
while(!que.empty()){
int u=que.front(); que.pop();
if(u==t)break;
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].v;
if(level[v]==-1&&edge[i].c>0){
level[v]=level[u]+1;
que.push(v);
}
}
}
if(level[t]==-1)return false;
return true;
}
int dfs(int u,int t,int f){
if(u==t)return f;
// int add=0,tempflow;
for(int &i=cur[u];i!=-1;i=edge[i].next){
int v=edge[i].v;
if(level[v]>level[u]&&edge[i].c>0){
int d=dfs(v,t,min(f,edge[i].c));
if(d>0){
edge[i].c-=d;
edge[i^1].c+=d;
return d;
}
}
}
return 0;
}
ll dinic(int s,int t){
ll max_flow=0;
while(bfs(s,t)){
// cout<<777<<endl;
ll flow;
for(int i=0;i<=t+10;i++){
cur[i]=head[i];
}
while((flow=dfs(s,t,INF))>0){
max_flow+=flow;
}
}
return max_flow;
}
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
int s=0,t=n+100;
for(int i=0;i<=t+10;i++)head[i]=-1;
int u,v,a,b,c;
ll sum=0;
for(int i=1;i<=m;i++){
scanf("%d%d%d%d%d",&u,&v,&a,&b,&c);
sum+=(a+b+c);
add_edge(s,u,a+b); add_edge(u,s,0);
add_edge(s,v,a+b); add_edge(v,s,0);
add_edge(u,v,a+c-2*b); add_edge(v,u,a+c-2*b);
add_edge(u,t,b+c); add_edge(t,u,0);
add_edge(v,t,b+c); add_edge(t,v,0);
}
printf("%lld\n",sum-dinic(s,t)/2);
}
return 0;
}