随便建个最小割啦。。。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #define maxv 50050 #define maxe 500050 #define inf 0x7f7f7f7f7f7f7f7fLL using namespace std; long long n,m,vala[maxv],valb[maxv],sum=0,max_flow=0,x,y,a,b,c,s=1,t,nume=1,g[maxv]; long long dis[maxv]; struct edge { long long v,f,nxt; }e[maxe]; queue <long long> q; void addedge(long long u,long long v,long long f) { e[++nume].v=v;e[nume].f=f; e[nume].nxt=g[u];g[u]=nume; e[++nume].v=u;e[nume].f=0; e[nume].nxt=g[v];g[v]=nume; } bool bfs() { for (long long i=s;i<=t;i++) dis[i]=inf; dis[s]=0;q.push(s); while (!q.empty()) { long long head=q.front();q.pop(); for (long long i=g[head];i;i=e[i].nxt) { long long v=e[i].v; if ((e[i].f>0) && (dis[v]>dis[head]+1)) { dis[v]=dis[head]+1; q.push(v); } } } return dis[t]!=inf; } long long dinic(long long x,long long low) { if (x==t) return low; long long ret=0; for (long long i=g[x];low && i;i=e[i].nxt) { long long v=e[i].v; if ((e[i].f>0) && (dis[v]==dis[x]+1)) { long long dd=dinic(v,min(low,e[i].f)); ret+=dd;low-=dd; e[i].f-=dd;e[i^1].f+=dd; } } if (!ret) dis[x]=inf; return ret; } int main() { scanf("%lld%lld",&n,&m);t=n; for (long long i=2;i<=n-1;i++) {scanf("%lld",&vala[i]);sum+=vala[i];vala[i]<<=1;} for (long long i=2;i<=n-1;i++) {scanf("%lld",&valb[i]);sum+=valb[i];valb[i]<<=1;} for (long long i=1;i<=m;i++) { scanf("%lld%lld%lld%lld%lld",&x,&y,&a,&b,&c);sum+=a+b; if (x>y) swap(x,y); if ((x==1) && (y==n)) { sum-=a+b+c; continue; } if (x==1) vala[y]+=2*a+2*c,sum-=b; else if (y==n) valb[x]+=2*b+2*c,sum-=a; else vala[x]+=a,valb[x]+=b,vala[y]+=a,valb[y]+=b; if ((x!=1) && (y!=n)) addedge(x,y,a+b+2*c),addedge(y,x,a+b+2*c); } for (long long i=2;i<=n-1;i++) addedge(s,i,vala[i]),addedge(i,t,valb[i]); while (bfs()) max_flow+=dinic(s,inf); printf("%lld\n",sum-max_flow/2); return 0; }