社区讨论

求助

灌水区参与者 3已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@mi7yvrdy
此快照首次捕获于
2025/11/21 05:52
4 个月前
此快照最后确认于
2025/11/21 05:52
4 个月前
查看原帖
好像是第二次整体跑Dinic时炸了,但本蒟蒻改不出来,求救 WA代码:
CPP
#include<bits/stdc++.h>
using namespace std;
const  int  inf = 0x3f3f3f3 , MAXN = 210 , MAXM = 100019*2 ;
int  head[ MAXN ] , d[ MAXN ] , to[ MAXM ] , w[ MAXM ] , nex[ MAXM ] , A[ MAXN ] , L[ MAXM ];
int  n , m , s , t , SS , TT , tot = 1 , maxflow , outflow , Id ;
inline int read()
{
    int s = 0,w = 1;
    char g = getchar();
    while(g<'0'||g>'9'){if(g=='-')w*=-1;g = getchar();}
    while(g>='0'&&g<='9'){s = s*10+g-'0';g = getchar();}
    return s*w;
}
queue<int>q ;
void  add( int  x , int y , int z ){
	to[ ++tot ] = y , w[ tot ] = z , nex[ tot ] = head[ x ] , head[ x ] = tot ;
	to[ ++tot ] = x , w[ tot ] = 0 , nex[ tot ] = head[ x ] , head[ x ] = tot ;
}
bool  bfs(){
    memset( d , 0 , sizeof(d) ) ;
    while( q.size() ) q.pop() ; 
    q.push( SS ) ; d[ SS ] = 1 ; 
    while( q.size() ){
        int  x = q.front() ; q.pop() ;
        for( int  i = head[ x ] ; i ; i = nex[ i ])
            if( w[ i ] && !d[ to[i] ] ){
                q.push( to[ i ] ) ;
                d[ to [ i ] ] = d[ x ] + 1;
                if( to[ i ] == TT )return 1 ; 
            }
    }
    return 0 ;
}
int  dinic( int  x , int  flow ){ 
    if( x == TT )return flow ;
    int  rest = flow , k ;
    for( int  i = head[ x ] ; i && rest ; i = nex[ i ] )//当前可流入最大流量不为0 ,
        if( w[ i ] && d[ to [ i ] ] == d[ x ] + 1 ){//目标路径有剩余流量,且不存在环之类神奇的东西
            k = dinic( to[ i ] , min( rest , w[ i ] ) );//继续搜
            if( !k )d[ to [ i ] ] = 0 ; //剪枝
            w[ i ] -= k ; 
            w[ i ^ 1 ] += k ;//占用k流量,注意反边要加上k,不然无法退回
            rest -= k ; //当前节点的剩余汇入流量-k;
        }
       return flow - rest ; //递归完成
}
int  main()
{
    freopen("2.in","r",stdin);
    freopen("ans.out","w",stdout);
    n = read() , m = read() , s = read() , t = read() , SS = n + 1 , TT = n + 2 ;
    add( t , s , inf ) ;
    for( register int  i = 1 ; i <= m ; ++i ){
        int  x = read() , y = read() ; L[ i ] = read() ; int R = read () ;
        add( x , y , R - L[ i ] ) ;  
        A[ x ] -= L[ i ] , A[ y ] += L[ i ] ; //初始图下界
    }
    for( register int  i = 1 ; i <= n ; i++ ){
    	if( A[ i ] > 0 ){
    		outflow += A[ i ] ;add( SS , i , A[ i ] ) ;
    	}
    	if( A[ i ] < 0 )
    		add( i , TT , -A[ i ] ) ;
    }
    int  flow = 0 ; 
    while( bfs() ){//存在增广路
        while( flow = dinic ( SS , inf ))maxflow += flow ; 
    }
    if( maxflow != outflow )printf("please go home to sleep\n") ;
    else{
        SS = s , TT = t , maxflow = 0 ;
        while( bfs() )while( flow = dinic ( SS , inf ))maxflow += flow ; 
        printf("%d\n",maxflow);
    } 
    return  0 ;
}

回复

2 条回复,欢迎继续交流。

正在加载回复...