1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| #include <bits/stdc++.h> using namespace std; using ll = long long;
const int maxn = 50; const int maxm = 1e4+5; const ll inf = 1e9+1;
struct Edge{ int v,next; ll c; } edge[maxm];
int n,m,tot,source,sink; int head[maxn],dep[maxn];
void add(int u,int v,ll c) { edge[tot].v=v; edge[tot].c=c; edge[tot].next=head[u]; head[u]=tot++; }
void addedge(int u,int v,ll c) { add(u,v,c); add(v,u,0); }
bool bfs() { queue<int> que; memset(dep,-1,sizeof dep); dep[source] = 0; que.push(source); while (!que.empty()) { int cur = que.front(); que.pop(); for (int i=head[cur]; i!=-1; i=edge[i].next) { int to = edge[i].v; if (edge[i].c>0 && dep[to]==-1) { dep[to] = dep[cur] + 1; que.push(to); } } } return dep[sink] != -1; }
ll dfs(int u,ll delta) { if (u == sink) return delta; ll flow = 0; for (int i=head[u]; i!=-1; i=edge[i].next) { int to = edge[i].v; if (edge[i].c>0 && dep[to]==dep[u]+1) { ll temp = dfs(to,min(delta-flow,edge[i].c)); edge[i].c -= temp; edge[i^1].c += temp; flow += temp; } } if (!flow) dep[u] = -1; return flow; }
ll Dinic() { ll ans = 0; while(bfs()) { while (1) { ll temp = dfs(source,inf); if (!temp) break; ans += temp; } } return ans; }
struct Storage { int u,v; } storage[maxm];
int main() { memset(head,-1,sizeof head); scanf("%d%d",&n,&m); source = 1; sink = n; for (int i=1; i<=m; ++i) { int u,v; ll c; scanf("%d %d %lld",&u,&v,&c); addedge(u,v,c); storage[i].u = u; storage[i].v = v; } ll maxflow = Dinic();
memset(head,-1,sizeof head); memset(edge,0,sizeof edge); tot = 0;
for (int i=1; i<=m; ++i) addedge(storage[i].u,storage[i].v,1);
ll cut_count = Dinic();
cout << maxflow << " " << cut_count << endl; return 0; }
|