U2023006 2023-10-21 12:03:50

没有一个人做出来吗?

共 3 条回复

C2024013

#include<bits/stdc++.h> using namespace std; const int maxn = 110; const int inf = 0x7fffffff; struct Edge{ int to, w, next; }; Edge e[5000*2+10]; int depth[maxn];
int head[maxn];
int cur[maxn];
int n, m, s, t; int cnt;
void init(){ cnt = 0; //s = 1, t = n; for (int i = 0; i <= n; i++) head[i] = -1; } void add_edge(int u, int v, int w){
e[cnt].to = v; e[cnt].w = w; e[cnt].next = head[u]; head[u] = cnt++; } int bfs(int s,int t){
queue q; for (int i = 0; i <= n; i++) depth[i] = inf; q.push(s); depth[s] = 0; while (!q.empty()){ int p = q.front(); q.pop(); for (int i = head[p]; ~i; i = e[i].next){
if (e[i].w>0 && depth[p]+1<depth[e[i].to]){ depth[e[i].to] = depth[p] + 1;
q.push(e[i].to); } } } return depth[t] != inf; } int dfs(int u,int flow){
if (u == t)
return flow; for (int& i = cur[u]; ~i; i = e[i].next){
if (e[i].w > 0 && depth[u] + 1 == depth[e[i].to]){ int delta = dfs(e[i].to, min(flow,e[i].w));
if (delta > 0){
e[i].w -= delta; e[i ^ 1].w += delta; return delta;
} }

}
return 0; 

}

long long dinic(){
long long res = 0; while (bfs(s,t)){

    for (int i = 0; i <= n; i++) cur[i] = head[i];

    while (int delta = dfs(s,inf))
        res += 1ll*delta;
}
return res;

} int main() { ios::sync_with_stdio(false); cin >> n >> m>>s>>t;
init(); int a, b, c; for (int i = 0; i < m; i++){ cin >> a >> b >> c; add_edge(a, b, c); add_edge(b, a, 0);
} cout << dinic() << endl;

return 0;

}

C2024013

C2023030

6