bzoj1449 [JSOI2009]球队收益
Description
Input
Output
Sample Input
1 0 2 1
1 1 10 1
0 1 3 3
1 2
2 3
3 1
Sample Output
#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; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。