题目链接:土地划分
考虑最小割分割。
然后我们可以列出5个未知数,4个方程。
然后根据对称性消去方程即可得到最后的解。
AC代码:
#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int inf=0x3f3f3f3f;
const int N=1e4+10,M=2e6+10;
int n,m,s,t,h[N],res;
int head[N],nex[M],to[M],w[M],tot=1;
char *fs,*ft,buf[1<<20];
#define gc() (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<20,stdin),fs==ft))?0:*fs++;
inline int read(){
int x=0,f=1; char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=gc();}
return x*f;
}
inline void ade(int a,int b,int c){
to[++tot]=b; nex[tot]=head[a]; w[tot]=c; head[a]=tot;
}
inline void add(int a,int b,int c){ade(a,b,c); ade(b,a,0);}
inline int bfs(){
queue<int> q; q.push(s); memset(h,0,sizeof h); h[s]=1;
while(q.size()){
int u=q.front(); q.pop();
for(int i=head[u];i;i=nex[i]){
if(w[i]&&!h[to[i]]) h[to[i]]=h[u]+1,q.push(to[i]);
}
}
return h[t];
}
int dfs(int x,int f){
if(x==t) return f; int fl=0;
for(int i=head[x];i&&f;i=nex[i]){
if(w[i]&&h[to[i]]==h[x]+1){
int mi=dfs(to[i],min(w[i],f));
w[i]-=mi,w[i^1]+=mi,fl+=mi,f-=mi;
}
}
if(!fl) h[x]=-1;
return fl;
}
int dinic(){
int res=0;
while(bfs()) res+=dfs(s,inf);
return res;
}
signed main(){
n=read(),m=read(); t=n+1;
add(s,1,inf),add(n,t,inf);
for(int i=2,x;i<=n-1;i++) x=read(),add(s,i,x*2),res+=x;
for(int i=2,x;i<=n-1;i++) x=read(),add(i,t,x*2),res+=x;
for(int i=1,x,y,ea,eb,ec;i<=m;i++){
x=read(),y=read(),ea=read(),eb=read(),ec=read();
add(x,y,ea+eb+ec*2),add(y,x,ea+eb+ec*2);
add(s,x,ea),add(s,y,ea),add(x,t,eb),add(y,t,eb);
res+=(ea+eb);
}
cout<<res-dinic()/2;
return 0;
}