analysis
Face questions have clearly told this is a network flow, and apparently bounds Cost Flow
We all from s to the point of not even a positive cost 0 upper and lower bounds are side ai, and from all the negative to the point t 0 even a cost lower and upper bounds for the side -ai
This point represents a start there are so many electrical output or require so much electricity
Each point is then connected to the unlimited traffic t, cost-side output from the s point connected to each unlimited traffic, the cost of the input side
Then for wire consumption, this is a quadratic function, so be open to write
Weight will become a + b, 3 * a + b, 5 * a + b, 7 * a + b ......
First lower bound cost added to the list, then in order to prevent TLE, we use a method of dynamic Bordered
Upper and lower bounds network flow stamp this uncle
#include <iostream> #include <cstdio> #include <queue> #include <memory.h> using namespace std; const int N=2e2+10; const int M=6e2+10; const int Inf=2147483647; struct Edge { int u,v,l,r,a,b; }e[M]; struct Graph { int v,c,w,nx; }g[10000010]; int cnt=1,list[N],dis[N],vis[N],a[N],I[N][N],f[N]; int s,t,ss,tt; int n,m,ans; void Add_Edge(int u,int v,int c,int w) { g[++cnt]=(Graph){v,c,w,list[u]};list[u]=cnt; g[++cnt]=(Graph){u,0,-w,list[v]};list[v]=cnt; } void Add_Pipe(int u,int v,int l,int r,int w) { Add_Edge(u,v,r-l,w); if (l) a[u]-=l,a[v]+=l,ans+=w*l; } bool SPFA() { queue<int> q; while (!q.empty()) q.pop(); memset(dis,0x7f,sizeof dis);memset(vis,0,sizeof vis); q.push(ss);vis[ss]=1;dis[ss]=0; for (int i=1;i<=m;i++) if (I[e[i].u][e[i].v]==e[i].l&&e[i].l<e[i].r) { e[i].l++; Add_Edge(e[i].u,e[i].v,1,(2*e[i].l-1)*e[i].a+e[i].b); } while (!q.empty()) { int u=q.front();q.pop(); for (int i=list[u];i;i=g[i].nx) if (g[i].c&&dis[g[i].v]>dis[u]+g[i].w) { dis[g[i].v]=dis[u]+g[i].w;f[g[i].v]=i; if (!vis[g[i].v]) q.push(g[i].v); vis[g[i].v]=1; } vis[u]=0; } return dis[tt]!=0x7f7f7f7f; } void MCF() { int x=tt; while (f[x]) { ans+=g[f[x]].w; I[g[f[x]^1].v][g[f[x]].v]++;I[g[f[x]].v][g[f[x]^1].v]--; g[f[x]].c--;g[f[x]^1].c++; x=g[f[x]^1].v; } } void Dinic() { while (SPFA()) MCF(); } int main() { scanf("%d%d",&n,&m); s=0;t=n+1;ss=n+2;tt=n+3; Add_Pipe(t,s,0,Inf,0); for (int i=1;i<=n;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); if (a>0) Add_Pipe(s,i,a,a,0); else Add_Pipe(i,t,-a,-a,0); Add_Pipe(s,i,0,Inf,b);Add_Pipe(i,t,0,Inf,c); } for (int i=1;i<=m;i++) { scanf("%d%d%d%d%d%d",&e[i].u,&e[i].v,&e[i].a,&e[i].b,&e[i].l,&e[i].r); Add_Pipe(e[i].u,e[i].v,e[i].l,e[i].l,0); ans+=e[i].a*e[i].l*e[i].l+e[i].b*e[i].l; I[e[i].u][e[i].v]+=e[i].l;I[e[i].v][e[i].u]-=e[i].l; } for (int i=s;i<=t;i++) if (a[i]<0) Add_Pipe(i,tt,0,-a[i],0); else Add_Pipe(ss,i,0,a[i],0); Dinic(); printf("%d",ans); }