标签:
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> #include<queue> #include<deque> #include<set> #include<map> #include<ctime> #define LL long long #define inf 0x3ffffff #define S 0 #define T n+m+1 #define N 6010 using namespace std; inline LL read() { LL x=0,f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } struct edge{int to,next,from,v,c;}e[10*N]; struct match{int x,y;}M[100010]; int head[N],from[N],dist[N],q[N]; bool mrk[N]; int n,m,ans,cnt=1; int a[N],b[N],C[N],D[N]; inline void ins(int u,int v,int w,int c) { e[++cnt].to=v; e[cnt].v=w; e[cnt].c=c; e[cnt].from=u; e[cnt].next=head[u]; head[u]=cnt; } inline void insert(int u,int v,int w,int c) { ins(u,v,w,c); ins(v,u,0,-c); } inline bool spfa() { for(int i=0;i<=T;i++)dist[i]=inf; int t=0,w=1; dist[S]=0;q[0]=S;mrk[S]=1; while (t!=w) { int now=q[t++];if (t==6005)t=0; for (int i=head[now];i;i=e[i].next) if(e[i].v&&dist[now]+e[i].c<dist[e[i].to]) { dist[e[i].to]=dist[now]+e[i].c; from[e[i].to]=i; if (!mrk[e[i].to]) { mrk[e[i].to]=1; q[w++]=e[i].to; if(w==6005)w=0; } } mrk[now]=0; } return dist[T]!=inf; } inline void mcf() { int x=inf; for (int i=from[T];i;i=from[e[i].from]) x=min(x,e[i].v); for (int i=from[T];i;i=from[e[i].from]) { e[i].v-=x; e[i^1].v+=x; ans+=x*e[i].c; } } int main() { n=read();m=read(); for (int i=1;i<=n;i++) { a[i]=read(); b[i]=read(); C[i]=read(); D[i]=read(); } for(int i=1;i<=m;i++) { M[i].x=read(); M[i].y=read(); b[M[i].x]++; b[M[i].y]++; insert(S,i,1,0); insert(i,m+M[i].x,1,0); insert(i,m+M[i].y,1,0); } for (int i=1;i<=n;i++) ans+=a[i]*a[i]*C[i]+b[i]*b[i]*D[i]; for (int i=1;i<=m;i++) { int x=M[i].x,y=M[i].y; insert(m+x,T,1,2*a[x]*C[x]-2*b[x]*D[x]+C[x]+D[x]); a[x]++;b[x]--; insert(m+y,T,1,2*a[y]*C[y]-2*b[y]*D[y]+C[y]+D[y]); a[y]++;b[y]--; } while (spfa())mcf(); printf("%d\n",ans); return 0; }
标签:
原文地址:http://www.cnblogs.com/zhber/p/4215838.html