记录编号 569337 评测结果 AAAAAAAAAA
题目名称 运输问题1 最终得分 100
用户昵称 Gravataryrtiop 是否通过 通过
代码语言 C++ 运行时间 0.000 s
提交时间 2022-03-02 19:47:21 内存使用 0.00 MiB
显示代码纯文本
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 20005;
int ver[maxn << 1],nxt[maxn << 1],cap[maxn << 1],head[maxn];
int cnt = -1;
int cur[maxn],level[maxn];
queue <int> q; 
void add(int u,int v,int t) {
    nxt[++ cnt] = head[u];
    ver[cnt] = v;
    cap[cnt] = t;
    head[u] = cnt;
    return ;
}
int n;
bool bfs(int s,int t) {
    memset(level , 0 , sizeof(level));
    q.push(s);
    level[s] = 1;
    while(!q.empty()) {
        int u = q.front();
        q.pop();
        for(int i = head[u];~ i;i = nxt[i]) {
            int v = ver[i];
            if(!level[v]&&cap[i]) {
                level[v] = level[u] + 1;
                q.push(v);
            }
        }
    }
    return level[t] != 0;
}
int dfs(int x,int t,int maxflow) {
    if(x == t||!maxflow)return maxflow;
    int flow = 0,f;
    for(int& i = cur[x];~ i;i = nxt[i]) {
        int v = ver[i];
        if(level[v] == level[x] + 1&&(f = dfs(v , t , min(maxflow , cap[i])))) {
            if(!f) {
                level[v] = 0;
                break ;
            }
            flow += f;
            maxflow -= f;
            cap[i] -= f;
            cap[i ^ 1] += f;
            if(!maxflow)break;
        }
    }
    return flow;
}
int Dinic(int s,int t) {
    int flow = 0;
    while(bfs(s , t)) {
        memcpy(cur , head , sizeof(cur));
        flow += dfs(s , t , 0x3f3f3f3f);
    }
    return flow;
}
int main() {
    freopen("maxflowa.in","r",stdin);
    freopen("maxflowa.out","w",stdout);
    scanf("%d",&n);
    memset(head , -1 , sizeof(head));
    for(int i = 1;i < n;++ i) {
        for(int j = 1;j <= n;++ j) {
            int x;
            scanf("%d",&x);
            add(i , j , x);
            add(j , i , 0);
        }
    }
    printf("%d\n",Dinic(1 , n));
    fclose(stdin);
    fclose(stdout);
    return 0;
}