EK求助,8-10号点WA
查看原帖
EK求助,8-10号点WA
367619
otislee601楼主2022/1/26 16:32
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#define ll long long
#define int long long
using namespace std;
const ll maxn = 1e4 + 100;
const ll inf = 0x7fffffff;
struct edge{ll v,w,rev;};
ll n,m,S,T,pos[maxn][maxn],minn[maxn],pre[maxn],ans;
vector <edge> G[maxn];
bool vis[maxn];
bool bfs()
{
    queue <ll> q;
    q.push(S);
    minn[S]= inf;
    vis[S] = 1;
    while(!q.empty())
    {
        ll tmp = q.front();
        q.pop();
        for(ll i = 0;i < G[tmp].size();++i)
        if(G[tmp][i].w>0)
        {
            if(vis[G[tmp][i].v]) continue;
            minn[G[tmp][i].v] = min(minn[tmp],G[tmp][i].w);
            pre[G[tmp][i].v] = tmp;
            q.push(G[tmp][i].v);
            vis[G[tmp][i].v] = 1;
            if(G[tmp][i].v==T) return 1;
        }
    }
    return 0;
}
ll update()
{
    ll x = T;
    while(x != S)
    {
        G[pre[x]][pos[pre[x]][x]].w -= minn[T];
        G[x][G[pre[x]][pos[pre[x]][x]].rev].w += minn[T];
        x = pre[x];
    }
    return minn[T];
}
ll ek(ll s,ll t)
{
    ll flow = 0;
    while(1)
    {
        memset(vis,0,sizeof(vis));
        bool f = bfs();
        if(f == 0) return flow;
        flow += update();
    }
}
main()
{
    cin >> n >> m >> S >> T;
    for(ll i = 1,u,v,w;i <= m;++i)
    {
        scanf("%lld%lld%lld",&u,&v,&w);
        if(!pos[u][v])
        {
            G[u].push_back((edge){v,w,G[v].size()});
            G[v].push_back((edge){u,0,G[u].size()-1});
            pos[u][v] = G[u].size() - 1;
        }
        else  G[u][pos[u][v]].w += w;
    }
    printf("%lld\n",ek(S,T));
    return 0;
}

为什么会WA QAQ

2022/1/26 16:32
加载中...