题意
有n个人,可以染成黑白两色,给定m个形如(x,y,a,b,c)的限制条件,表示若x和y同为黑色则获得a的价值,若xy同为白色则获得c的价值,否则获得b的价值
求能获得的最大价值
做法
看到n<=500的条件很容易想到用网络流做
考虑怎么建图。看到黑白染色不难想到最小割,既然是答案最大那么就是用总的和减去最小的将若干人分成两个集合的最小代价
观察这么一个基本图,我们割掉a、b代表x和y都染成了t色,割掉c、d代表x和y都染成了s色,割掉aed代表x和y染成了t和s,割掉bec代表x和y染成了s和t
根据这个可以列若干方程,解出来就可以上了
代码
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
#define drp(i,st,ed) for (int i=st;i>=ed;--i)
#define fill(x,t) memset(x,t,sizeof(x))
#define copy(x,t) memcpy(x,t,sizeof(x))
#define fi first
#define se second
typedef std:: pair <int,int> pair;
typedef long double ld;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
const int ny2=(MOD+1)/2;
const int N=2000005;
const ld pi=acos(-1);
const ld eps=1e-8;
struct edge {int y,w,next;} e[N];
int dis[N],ls[N],edCnt;
int read() {
int x=0,v=1; char ch=getchar();
for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):v,ch=getchar());
for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());
return x*v;
}
void add_edge(int x,int y,int w) {
e[++edCnt]=(edge) {y,w,ls[x]}; ls[x]=edCnt;
e[++edCnt]=(edge) {x,0,ls[y]}; ls[y]=edCnt;
}
bool bfs(int st,int ed) {
std:: queue <int> que;
rep(i,st,ed) dis[i]=0; dis[st]=1;
for (que.push(st);!que.empty();) {
int x=que.front(); que.pop();
for (int i=ls[x];i;i=e[i].next) {
if (e[i].w>0&&!dis[e[i].y]) {
dis[e[i].y]=dis[x]+1;
if (e[i].y==ed) return true;
que.push(e[i].y);
}
}
} return false;
}
int find(int x,int ed,int mn) {
if (x==ed||!mn) return mn;
int res=0;
for (int i=ls[x];i;i=e[i].next) {
if (e[i].w>0&&dis[x]+1==dis[e[i].y]) {
int d=find(e[i].y,ed,std:: min(mn-res,e[i].w));
e[i].w-=d; e[i^1].w+=d; res+=d;
if (mn==res) break;
}
}
return res;
}
LL dinic(int st,int ed) {
LL res=0;
for (;bfs(st,ed);) res+=find(st,ed,INF);
return res;
}
int main(void) {
for (int n,m;~scanf("%d%d",&n,&m);) {
rep(i,0,n+1) ls[i]=0;
LL ans=0; edCnt=1;
rep(i,1,m) {
int x,y; LL a,b,c;
scanf("%d%d%lld%lld%lld",&x,&y,&a,&b,&c);
add_edge(0,x,(a+b));
add_edge(0,y,(a+b));
add_edge(x,n+1,(b+c));
add_edge(y,n+1,(b+c));
add_edge(x,y,(a+c)-b*2);
add_edge(y,x,(a+c)-b*2);
ans+=(a+b+c)*2;
}
ans-=dinic(0,n+1);
printf("%lld\n", ans/2);
}
return 0;
}