题意:给你一个有向图 问你跑完最大流之后可以通过改变多少条边使得最大流流量增加
思路:能够改变流量的边一定是最小割上的边 因此要找到最小割 我们的做法是找到满流的边 但是如果一条增广路上不止一条边满流 那么改变这条边的容量是没有意义的 因为另一条满流边还是会限制流量的增加 正确做法是 不仅要从源点找满流边 还要从汇点逆向找 正向访问过的点标记为1 逆向访问过的点标记为2 如果一条边的起点是1 终点是2那么这条边才满足题意
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define REP( i, a, b ) for( int i = a; i < b; i++ )
#define FOR( i, a, b ) for( int i = a; i <= b; i++ )
#define CLR( a, x ) memset( a, x, sizeof a )
#define CPY( a, x ) memcpy( a, x, sizeof a )
const int maxn = 500 + 10;
const int maxe = 10000 + 10;
const int INF = 1e9;
struct Edge{
int v, c, f;
int next;
Edge() {}
Edge(int v, int c, int f, int next) : v(v), c(c), f(f), next(next) {}
};
struct ISAP{
int n, s, t;
int num[maxn], cur[maxn], d[maxn], p[maxn];
int Head[maxn], cntE;
int Q[maxn], head, tail;
int vis[maxn], ans;
Edge edge[maxe];
void Init(int n){
this -> n = n;
cntE = ans = 0;
CLR(Head, -1);
CLR(vis, 0);
}
void Add(int u, int v, int c){
edge[cntE] = Edge(v, c, 0, Head[u]);
Head[u] = cntE++;
edge[cntE] = Edge(u, 0, 0, Head[v]);
Head[v] = cntE++;
}
void Bfs(){
CLR(d, -1);
CLR(num, 0);
d[t] = 0;
head = tail = 0;
Q[tail++] = t;
num[0] = 1;
while(head != tail){
int u = Q[head++];
for(int i = Head[u]; ~i; i = edge[i].next){
Edge &e = edge[i];
if(~d[e.v]) continue;
d[e.v] = d[u] + 1;
Q[tail++] = e.v;
num[d[e.v]] ++;
}
}
}
void Maxflow(int s, int t){
this -> s = s;
this -> t = t;
CPY(cur, Head);
Bfs();
int flow = 0, u = p[s] = s;
while(d[s] < n){
if(u == t){
int f = INF, neck;
for(int i = s; i != t; i = edge[cur[i]].v){
if(f > edge[cur[i]].c - edge[cur[i]].f){
f = edge[cur[i]].c - edge[cur[i]].f;
neck = i;
}
}
for(int i = s; i != t; i = edge[cur[i]].v){
edge[cur[i]].f += f;
edge[cur[i]^1].f -= f;
}
flow += f;
u = neck;
}
int ok = 0;
for(int i = cur[u]; ~i; i = edge[i].next){
Edge &e = edge[i];
if(e.c > e.f && d[e.v] + 1 == d[u]){
ok = 1;
cur[u] = i;
p[e.v] = u;
u = e.v;
break;
}
}
if(!ok){
int m = n - 1;
if(--num[d[u]] == 0) break;
for(int i = Head[u]; ~i; i = edge[i].next){
Edge &e = edge[i];
if(e.c - e.f > 0 && m > d[e.v]){
cur[u] = i;
m = d[e.v];
}
}
++num[d[u] = m + 1];
u = p[u];
}
}
//return flow;
}
void Findcut1(int x){
vis[x] = 1;
for(int i = Head[x]; ~i; i = edge[i].next){
if(edge[i].c > edge[i].f && !vis[edge[i].v])
Findcut1(edge[i].v);
}
}
void Findcut2(int x){
vis[x] = 2;
for(int i = Head[x]; ~i; i = edge[i].next){
if(edge[i^1].c > edge[i^1].f && !vis[edge[i].v])
Findcut2(edge[i].v);
}
}
int Ans(){
REP(u, 0, n) for(int i = Head[u]; ~i; i = edge[i].next){
Edge &e = edge[i];
if(e.c && vis[u] == 1 && vis[e.v] == 2) ans++;
}
return ans;
}
}solver;
void solve(){
int n, m;
scanf("%d%d", &n, &m);
solver.Init(n);
REP(i, 0, m){
int u, v, d;
scanf("%d%d%d", &u, &v, &d);
solver.Add(u, v, d);
}
solver.Maxflow(0, n - 1);
solver.Findcut1(0); solver.Findcut2(n - 1);
printf("%d\n", solver.Ans());
}
int main()
{
//freopen("in.txt", "r", stdin);
solve();
return 0;
}