HDU3996 Gold Mine(最大权闭合子图)

 

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<queue>
  4 #include<algorithm>
  5 using namespace std;
  6 #define INF (1LL<<60)
  7 #define MAXN 3333
  8 #define MAXM 3333*3333*2
  9 
 10 struct Edge{
 11     int v,next;
 12     __int64 cap,flow;
 13 }edge[MAXM];
 14 int vs,vt,NE,NV;
 15 int head[MAXN];
 16 
 17 void addEdge(int u,int v,__int64 cap){
 18     edge[NE].v=v; edge[NE].cap=cap; edge[NE].flow=0;
 19     edge[NE].next=head[u]; head[u]=NE++;
 20     edge[NE].v=u; edge[NE].cap=0; edge[NE].flow=0;
 21     edge[NE].next=head[v]; head[v]=NE++;
 22 }
 23 
 24 int level[MAXN];
 25 int gap[MAXN];
 26 void bfs(){
 27     memset(level,-1,sizeof(level));
 28     memset(gap,0,sizeof(gap));
 29     level[vt]=0;
 30     gap[level[vt]]++;
 31     queue<int> que;
 32     que.push(vt);
 33     while(!que.empty()){
 34         int u=que.front(); que.pop();
 35         for(int i=head[u]; i!=-1; i=edge[i].next){
 36             int v=edge[i].v;
 37             if(level[v]!=-1) continue;
 38             level[v]=level[u]+1;
 39             gap[level[v]]++;
 40             que.push(v);
 41         }
 42     }
 43 }
 44 
 45 int pre[MAXN];
 46 int cur[MAXN];
 47 __int64 ISAP(){
 48     bfs();
 49     memset(pre,-1,sizeof(pre));
 50     memcpy(cur,head,sizeof(head));
 51     int u=pre[vs]=vs;
 52     __int64 flow=0,aug=INF;
 53     gap[0]=NV;
 54     while(level[vs]<NV){
 55         bool flag=false;
 56         for(int &i=cur[u]; i!=-1; i=edge[i].next){
 57             int v=edge[i].v;
 58             if(edge[i].cap!=edge[i].flow && level[u]==level[v]+1){
 59                 flag=true;
 60                 pre[v]=u;
 61                 u=v;
 62                 //aug=(aug==-1?edge[i].cap:min(aug,edge[i].cap));
 63                 aug=min(aug,(__int64)edge[i].cap-edge[i].flow);
 64                 if(v==vt){
 65                     flow+=aug;
 66                     for(u=pre[v]; v!=vs; v=u,u=pre[u]){
 67                         edge[cur[u]].flow+=aug;
 68                         edge[cur[u]^1].flow-=aug;
 69                     }
 70                     //aug=-1;
 71                     aug=INF;
 72                 }
 73                 break;
 74             }
 75         }
 76         if(flag) continue;
 77         int minlevel=NV;
 78         for(int i=head[u]; i!=-1; i=edge[i].next){
 79             int v=edge[i].v;
 80             if(edge[i].cap!=edge[i].flow && level[v]<minlevel){
 81                 minlevel=level[v];
 82                 cur[u]=i;
 83             }
 84         }
 85         if(--gap[level[u]]==0) break;
 86         level[u]=minlevel+1;
 87         gap[level[u]]++;
 88         u=pre[u];
 89     }
 90     return flow;
 91 }
 92 
 93 int main(){
 94     int t,n,m,a,b,c;
 95     scanf("%d",&t);
 96     for(int cse=1; cse<=t; ++cse){
 97         scanf("%d",&n);
 98 
 99         vs=0; vt=3332; NV=3333; NE=0;
100         memset(head,-1,sizeof(head));
101 
102         __int64 res=0;
103         for(int i=1; i<=n; ++i){
104             scanf("%d",&m);
105             for(int j=1; j<=m; ++j){
106                 scanf("%d%d%d",&a,&b,&c);
107 
108                 if(b>a) addEdge(vs,i*30+j,b-a),res+=b-a;
109                 else addEdge(i*30+j,vt,a-b);
110 
111                 while(c--){
112                     scanf("%d%d",&a,&b);
113                     addEdge(i*30+j,a*30+b,INF);
114                 }
115             }
116         }
117         printf("Case #%d: %I64d\n",cse,res-ISAP());
118     } 
119     return 0;
120 }

 

posted @ 2015-10-03 16:44  WABoss  阅读(189)  评论(0编辑  收藏  举报