題目鏈接:點擊查看
題目大意:現在有 n 個士兵,每個士兵有兩種職業可以選擇,一種是戰士,一種是法師,現在有 m 對關系,每對關系 ( x , y ) 的權值如下:
如果 x 和 y 同為戰士,則收益為 a如果 x 和 y 同為法師,則收益為 c如果 x 和 y 職業不同,則收益為 b
現在問如何分配職業使得收益之和最大
題目分析:因為每個士兵只有兩種職業可以選擇,所以我們不妨將其視為兩個集合,然后與源點和匯點相連求最小割,那么現在的問題是如何確定邊權:
?因為邊權涉及到了除以 2 ,所以我們不妨整體乘以 2 ,最后求出的結果再除以二即可
代碼:
?
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=510;template<typename T>
struct Dinic
{const static int N=510;const static int M=N*N;const T inf=0x3f3f3f3f3f3f3f3f;struct Edge{int to,next;T w;}edge[M];//邊數int head[N],cnt;void addedge(int u,int v,T w){edge[cnt].to=v;edge[cnt].w=w;edge[cnt].next=head[u];head[u]=cnt++;edge[cnt].to=u;edge[cnt].w=0;//反向邊邊權設置為0edge[cnt].next=head[v];head[v]=cnt++;}int d[N],now[N];//深度 當前弧優化bool bfs(int s,int t)//尋找增廣路{memset(d,0,sizeof(d));queue<int>q;q.push(s);now[s]=head[s];d[s]=1;while(!q.empty()){int u=q.front();q.pop();for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;T w=edge[i].w;if(d[v])continue;if(!w)continue;d[v]=d[u]+1;now[v]=head[v];q.push(v);if(v==t)return true;}}return false;}T dinic(int x,int t,T flow)//更新答案{if(x==t)return flow;T rest=flow,i;for(i=now[x];i!=-1&&rest;i=edge[i].next){int v=edge[i].to;T w=edge[i].w;if(w&&d[v]==d[x]+1){T k=dinic(v,t,min(rest,w));if(!k)d[v]=0;edge[i].w-=k;edge[i^1].w+=k;rest-=k;}}now[x]=i;return flow-rest;}void init(){memset(now,0,sizeof(now));memset(head,-1,sizeof(head));cnt=0;}T solve(int st,int ed){T ans=0,flow;while(bfs(st,ed))while(flow=dinic(st,ed,inf))ans+=flow;return ans;}
};Dinic<LL>t;int main()
{
#ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);int n,m,st=N-1,ed=st-1;while(scanf("%d%d",&n,&m)!=EOF){t.init();LL sum=0;while(m--){int u,v,A,B,C,a,b,c;scanf("%d%d%d%d%d",&u,&v,&A,&B,&C);sum+=A+B+C;a=A+B,b=C+B,c=A+C-2*B;t.addedge(st,u,a);t.addedge(st,v,a);t.addedge(u,v,c);t.addedge(v,u,c);t.addedge(u,ed,b);t.addedge(v,ed,b);}printf("%lld\n",(sum*2-t.solve(st,ed))/2);}return 0;
}
?
總結
以上是生活随笔為你收集整理的HDU - 6598 Harmonious Army(最大流最小割)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。