神秘的挤奶机

Problem Detail - [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 - HydroOJ

#include
using namespace std;
const int INF = 1e9;
const int N = 205, M = 4e4 + 15;
int head[N], f[M << 1], cnt = 0;
int n, m, t;
struct edge{
    int to, nxt, w;
}e[M << 1];
void add(int u, int v, int w) {
    e[cnt].to = v;
    e[cnt].w = w;
    e[cnt].nxt = head[u];
    head[u] = cnt++;
}
int dep[N], cur[N];
bool bfs() {
    queue q;
    memset(dep, -1, sizeof(dep));
    dep[1] = 0; cur[1] = head[1];
    q.push(1);
    while (!q.empty()) {
        int u = q.front(); q.pop();
        for (int i = head[u]; ~i; i = e[i].nxt) {
            int v = e[i].to;
            if (dep[v] == -1 && f[i]) {
                dep[v] = dep[u] + 1;
                cur[v] = head[v];
                if (v == n) return true; 
                q.push(v);
            }
        }
    }
    return false;
}
int dfs(int u, int limit) {
    if (u == n) {
        return limit;
    }
    int flow = 0;
    for (int i = cur[u]; ~i && flow < limit; i = e[i].nxt) {
        cur[u] = i;
        int v = e[i].to;
        if (dep[v] == dep[u] + 1 && f[i]) {
	        int mif = dfs(v, min(f[i], limit - flow));
	        if (!mif) dep[v] = -1;
	        f[i] -= mif; f[i ^ 1] += mif;
	        flow += mif;        	
        }
    }
    return flow;
}
int dinic() {
    int res = 0;
    while (bfs()) {
        res += dfs(1, INF);
    }
    return res;
}
bool check(int mid) {
    for (int i = 0; i < cnt; i++) {
        if (e[i].w > mid) f[i] = 0;
        else f[i] = 1;
    }
    return dinic() >= t;
}
int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    memset(head, -1, sizeof(head));
    cin >> n >> m >> t;
    for (int i = 1; i <= m; i++) {
        int u, v, w; cin >> u >> v >> w;
        add(u, v, w);
        add(v, u, w);
    }
    int l = 1, r = 1e6;
    while (l < r) {
        int mid = l + r >> 1;
        if (check(mid)) 
            r = mid;
        else
            l = mid + 1;
    }
    cout << l << '\n';
}

你可能感兴趣的:(网络流,图论,网络流,二分)