91分,dinic求助

P3376 【模板】网络最大流

Otomachi_Una_ @ 2021-10-07 10:16:18

RT

#include<iostream>
#include<queue>
using namespace std;
#define ll long long
const int MAXN=205;
const int MR=10005;
ll n,m,s,t;
ll cnt=1;
ll last[MAXN];
ll lv[MAXN],cur[MAXN];//层数 , 弧优化 
struct edge{
    ll v,w,la;
}a[MR];
void add(ll u,ll v,ll w){
    ++cnt;
    a[cnt].v=v;
    a[cnt].w=w;
    a[cnt].la=last[u];
    last[u]=cnt;
    return;
}
bool bfs(){
    for(int i=1;i<=n;i++)
        lv[i]=-1;
    queue<int> q;
    q.push(s);
    lv[s]=0;
    while(!q.empty()){
        int p=q.front();
        q.pop();
        for(int i=last[p];i;i=a[i].la){
            ll to=a[i].v,flow=a[i].w;
            if(lv[to]==-1&&flow){
                lv[to]=lv[p]+1;
                q.push(to);
            }
        }
    }
    return lv[t]!=-1;
}
ll dfs(ll p,ll flow){
    if(p==t)
        return flow;
    ll rnm=flow;
    for(int i=cur[p];i&&rnm;i=a[i].la){
        cur[p]=i;
        ll to=a[i].v,vol=a[i].w;
        if(vol&&lv[to]==lv[p]+1){
            ll c=dfs(to,min(flow,vol));
            a[i].w-=c;
            a[i^1].w+=c;
            rnm-=c;
        }
    }
    return flow-rnm;
}
int main(){
    //freopen("in.txt","r",stdin);
    cin>>n>>m>>s>>t;
    for(int i=1;i<=m;i++){
        ll u,v,w;
        cin>>u>>v>>w;
        add(u,v,w);
        add(v,u,0);
    }
    ll ans=0;
    while(bfs()){
        for(int i=1;i<=n;i++)
            cur[i]=last[i];
        ans+=dfs(s,1e18);
    }
    cout<<ans;
    return 0;
} 

by RainFestival @ 2021-10-07 10:18:52

@ushg8877 您到 loj101 交一发试试?


by Otomachi_Una_ @ 2021-10-07 10:38:21

@RainFestival 还是91...


by RainFestival @ 2021-10-07 10:49:02

@ushg8877 感觉您这个没啥问题呀qwq


by RainFestival @ 2021-10-07 10:52:31

@ushg8877 您尝试把

ll c=dfs(to,min(flow,vol));

改成

ll c=dfs(to,min(rnm,vol));

试试?qwq


by Otomachi_Una_ @ 2021-10-07 10:54:10

@RainFestival 过了,谢谢大佬


by RainFestival @ 2021-10-07 10:57:16

@ushg8877 QWQ


by BeautifulWater @ 2021-10-27 19:24:38

rnm代表是实际过程中剩下的空间,flow代表你的初始运算,但博主你这个变量命名。。


by Retribuamus @ 2021-11-04 20:13:42

,但博主你这个变量命名。。


|