zoukankan      html  css  js  c++  java
  • HDOJ 3549 Dinitz

    #include <cstdio>
    #include <vector>
    #include <algorithm>
    #include <cstring>
    #include <queue>
    
    using namespace std;
    
    #define N 100
    #define INF 233333333
    
    int capacity[N][N];
    int flow[N][N];
    int height[N];
    int num;
    
    bool dinic_bfs(int s, int t){
        memset(height, 0, sizeof(height));
        height[s] = 1;
        queue<int> q;
        q.push(s);
        while (!q.empty()){
            int temp = q.front();
            q.pop();
            for (int i = 0; i < num; i++)
                if (!height[i] && capacity[temp][i] > flow[temp][i]){
                    height[i] = height[temp] + 1;
                    q.push(i);
                }
        }
        return height[t] == 0? false: true;
    }
    
    int dinic_dfs(int s, int t, int minflow){
        int result = 0;
        for (int i = 0; i < num; i++)
            if (minflow && height[s] + 1 == height[i] && capacity[s][i] > flow[s][i]){
                int temp = i != t? dinic_dfs(i, t, min(minflow, capacity[s][i] - flow[s][i])): min(minflow, capacity[s][i] - flow[s][i]);
                flow[s][i] += temp;
                flow[i][s] -= temp;
                minflow -= temp;
                result += temp;
            }
        return result;
    }
    
    int dinic(int s, int t){
        int answer = 0;
        while (dinic_bfs(s, t))
            answer += dinic_dfs(s, t, INF);
        return answer;
    }
    
    int main(){
        int t, n, m, x, y, c;
        scanf("%d", &t);
        for (int i = 0; i < t; i++){
            scanf("%d %d", &n, &m);
            num = n;
            for (int j = 0; j < n; j++){
                for (int l = 0; l < n; l++){
                    capacity[j][l] = 0;
                    flow[j][l] = 0;
                }
                height[j] = 0;
            }
            for (int j = 0; j < m; j++){
                scanf("%d %d %d", &x, &y, &c);
                capacity[x - 1][y - 1] += c;
            }
            printf("Case %d: %d
    ", i + 1, dinic(0, n - 1));
        }
        return 0;
    }
  • 相关阅读:
    机器学习——朴素贝叶斯
    机器学习——决策树
    机器学习——线性回归
    机器学习——KNN
    机器学习——数据预处理
    爬虫——scrapy入门
    爬虫——生产者消费者
    想写篇技术性散文
    (景德镇)麻将规则服务描述
    Visual Studio 2013环境下操作vc6/vc7/vc8等低版本平台项目【编译|生成|调试】
  • 原文地址:https://www.cnblogs.com/neopolitan/p/8044879.html
Copyright © 2011-2022 走看看