//点标从0-n-1, 開始时先init 复杂度n^3 //对于边(u,v,flow): //g[u][v]+=flow; //g[v][u]+=flow; typedef long long ll; const int N = 305; const ll inf = 1e18; ll g[N][N], w[N]; int a[N], v[N], na[N]; ll mincut(int n) { int i, j, pv, zj; ll best = inf; for(i = 0; i < n; i ++) v[i] = i; while(n > 1) { for(a[v[0]] = 1, i = 1; i < n; i ++) { a[v[i]] = 0; na[i-1] = i; w[i] = g[v[0]][v[i]]; } for(pv = v[0], i = 1; i < n; i ++) { for(zj = -1, j = 1; j < n; j ++) if(!a[v[j]] && (zj < 0 || w[j] > w[zj])) zj = j; a[v[zj]] = 1; if(i == n-1) { if(best > w[zj]) best = w[zj]; for(i = 0; i < n; i ++) { g[v[i]][pv] = g[pv][v[i]] += g[v[zj]][v[i]]; } v[zj] = v[--n]; break; } pv = v[zj]; for(j = 1; j < n; j ++) if(!a[v[j]]) w[j] += g[v[zj]][v[j]]; } } return best; } void init(int n){ for(int i = 0; i < n; i ++) for(int j = 0; j < n; j ++) g[i][j] = g[j][i] = 0; }
时间: 2024-10-23 21:11:55