Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 225 Accepted Submission(s): 58
Case #1: 270
#include<cstdio> #include<cstring> #include<vector> #define N 5005 #define M 1000005 #define inf 1ll<<60 #define min(a,b) ((a)<(b)?(a):(b)) using namespace std; __int64 n,m,s,t,num,adj[N],dis[N],q[N]; struct edge { __int64 v,w,pre; }e[M]; void insert(__int64 u,__int64 v,__int64 w) { e[num]=(edge){v,w,adj[u]}; adj[u]=num++; e[num]=(edge){u,0,adj[v]}; adj[v]=num++; } __int64 bfs() { __int64 i,x,v,head=0,tail=0; memset(dis,0,sizeof(dis)); dis[s]=1; q[++tail]=s; while(head!=tail) { x=q[head=(head+1)%N]; for(i=adj[x];~i;i=e[i].pre) if(e[i].w&&!dis[v=e[i].v]) { dis[v]=dis[x]+1; if(v==t) return 1; q[tail=(tail+1)%N]=v; } } return 0; } __int64 dfs(__int64 x,__int64 limit) { if(x==t) return limit; __int64 i,v,tmp,cost=0; for(i=adj[x];~i&&cost<limit;i=e[i].pre) if(e[i].w&&dis[x]==dis[v=e[i].v]-1) { tmp=dfs(v,min(limit-cost,e[i].w)); if(tmp) { e[i].w-=tmp; e[i^1].w+=tmp; cost+=tmp; } else dis[v]=-1; } return cost; } __int64 Dinic() { __int64 ans=0; while(bfs()) ans+=dfs(s,inf); return ans; } int main() { __int64 c,cc=0; scanf("%I64d",&c); while(c--) { __int64 i,j,k,u,v,w1,w2,a,b,idx=2,sum=0,h[105][35]={0}; num=0; memset(adj,-1,sizeof(adj)); s=0; t=1; scanf("%I64d",&n); for(i=0;i<n;i++) { scanf("%I64d",&m); for(j=0;j<m;j++) { scanf("%I64d%I64d%I64d",&w1,&w2,&k); sum+=w2; u=h[i][j]=idx++; insert(s,u,w1); insert(u,t,w2); while(k--) { scanf("%I64d%I64d",&a,&b); v=h[a-1][b-1]; insert(v,u,inf); } } } printf("Case #%I64d: %I64d\n",++cc,sum-Dinic()); } }