Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
求助路过大牛!为什么WA?做了六个小时了。(附C程序)我用的HLPP /* Name: Destroying the Graph Copyright: Author: Date: 24/01/09 15:30 Description: */ #include <stdio.h> #define MAXN 1000 #define MAXM 100000 #define OO 2139062143 typedef struct { long d , w , next ; } arctype ; long Vertex[MAXN] , Cur[MAXN] , H[MAXN] , E[MAXN] , Q[MAXN] ; long Ins[MAXN] ; long Level[MAXN<<1][MAXN] , Len[MAXN<<1] ; long Show[MAXM] ; arctype Arc[MAXM] ; long N , M , S , T , Tot , All , K ; void Addarc( long x , long y , long w ) { ++Tot ; Arc[Tot].d = y ; Arc[Tot].w = w ; Arc[Tot].next = Vertex[x] ; Vertex[x] = Tot ; } void Addvertex( long v0 ) { Len[H[v0]]++ ; Level[H[v0]][Len[H[v0]]] = v0 ; } void Enter() { long i , x , y , w ; memset( Vertex , 0xff , sizeof( Vertex ) ) ; memset( E , 0 , sizeof( E ) ) ; memset( Len , 0 , sizeof( Len ) ) ; Tot = 0 ; S = 0 ; T = ( N << 1 ) + 1 ; All = T+1 ; for ( i = 1 ; i <= N ; i++ ) { scanf( "%ld" , &w ) ; Addarc( i+N , T , w ) ; Addarc( T , i+N , 0 ) ; } for ( i = 1 ; i <= N ; i++ ) { scanf( "%ld" , &w ) ; Addarc( S , i , w ) ; Addarc( i , S , 0 ) ; } for ( i = 1 ; i <= M ; i++ ) { scanf( "%ld%ld" , &x , &y ) ; Addarc( x , y+N , OO ) ; Addarc( y+N , x , 0 ) ; } } void Bfs() { long head , tail , p ; memset( H , 0x7f , sizeof( H ) ) ; Q[1] = T ; H[T] = 0 ; head = 0 ; tail = 1 ; while ( head < tail ) { ++head ; p = Vertex[Q[head]] ; while ( p != -1 ) { if ( H[Arc[p].d] == OO ) { H[Arc[p].d] = H[Q[head]]+1 ; ++tail ; Q[tail] = Arc[p].d ; if ( Arc[p].d != S ) Addvertex( Arc[p].d ) ; } p = Arc[p].next ; } } H[S] = All ; for ( p = 0 ; p < All ; p++ ) if ( H[p] == OO ) { H[p] = 0 ; Addvertex( p ) ; } } void Push( long u , long p ) { long x ; if ( E[u] < Arc[p].w ) x = E[u] ; else x = Arc[p].w ; E[Arc[p].d] += x ; E[u] -= x ; Arc[p].w -= x ; if ( p & 1 == 1 ) Arc[p+1].w += x ; else Arc[p-1].w += x ; } void Relabel( long u ) { long x , p ; x = OO ; p = Vertex[u] ; while ( p != -1 ) { if ( ( Arc[p].w > 0 ) && ( H[Arc[p].d] < x ) ) x = H[Arc[p].d] ; p = Arc[p].next ; } H[u] = 1+x ; } long Discharge( long u ) { long result = 0 ; while ( E[u] > 0 ) if ( Cur[u] == -1 ) { Relabel( u ) ; result = 1 ; Cur[u] = Vertex[u] ; } else if ( ( Arc[Cur[u]].w > 0 ) && ( H[u] == H[Arc[Cur[u]].d]+1 ) ) Push( u , Cur[u] ) ; else Cur[u] = Arc[Cur[u]].next ; return result ; } void Blow( long height ) { long i , j ; for ( i = height+1 ; i <= All ; i++ ) { for ( j = 1 ; j <= Len[i] ; j++ ) { H[Level[i][j]] = All + 1 ; Addvertex( Level[i][j] ) ; if ( ( E[Level[i][j]] > 0 ) && ( All+2 > K ) ) K = All + 2 ; } Len[i] = 0 ; } } void Initialize() { long p ; Bfs() ; for ( p = 0 ; p < All ; p++ ) Cur[p] = Vertex[p] ; p = Vertex[S] ; while ( p != -1 ) { if ( Arc[p].w > 0 ) { E[Arc[p].d] += Arc[p].w ; E[S] -= Arc[p].w ; if ( p & 1 == 1 ) Arc[p+1].w += Arc[p].w ; else Arc[p-1].w += Arc[p].w ; Arc[p].w = 0 ; } p = Arc[p].next ; } } void Solve() { long now , i ; Initialize() ; now = All ; while ( now > 0 ) { now-- ; for ( i = Len[now] ; i >= 1 ; i-- ) if ( Discharge( Level[now][i] ) ) { K = H[Level[now][i]] ; if ( ( now > 0 ) && ( Len[now] == 1 ) ) Blow( now ) ; Addvertex( Level[now][i] ) ; Level[now][i] = Level[now][Len[now]] ; Len[now]-- ; now = K ; break ; } } } void Dfs( long u ) { long p ; Ins[u] = 1 ; p = Vertex[u] ; while ( p != -1 ) { if ( ( Arc[p].w > 0 ) && ( ! Ins[Arc[p].d] ) ) Dfs( Arc[p].d ) ; p = Arc[p].next ; } } void Print() { long i , p ; memset( Ins , 0 , sizeof( Ins ) ) ; Dfs( S ) ; Show[0] = 0 ; for ( i = 0 ; i < All ; i++ ) if ( Ins[i] ) { p = Vertex[i] ; while ( p != -1 ) { if ( ! Ins[Arc[p].d] ) { ++Show[0] ; Show[Show[0]] = p ; } p = Arc[p].next ; } } printf( "%ld\n" , E[T] ) ; printf( "%ld\n" , Show[0] ) ; for ( i = 1 ; i <= Show[0] ; i++ ) if ( Show[i] <= ( N << 1 ) ) printf( "%ld +\n" , ( Show[i] + 1 ) >> 1 ) ; else printf( "%ld -\n" , ( Show[i] - ( N << 1 ) + 1 ) >> 1 ) ; } int main() { while ( scanf( "%ld%ld" , &N , &M ) != EOF ) { Enter() ; Solve() ; Print() ; } return 0 ; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator