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;
    }
  • 相关阅读:
    ssh 使用密钥文件
    VS2015企业版,社区版,专业版详细对比
    Redis 与 数据库处理数据的两种模式(转)
    工业级物联网项目架构设计思想(转)
    C# and Redis,安装作为服务
    C# CRC32
    c++,C# 转换
    app配置智能硬件的解决方案
    C# 与C++的数据转换
    C++ 对数组sizeof 和对数组元素sizeof
  • 原文地址:https://www.cnblogs.com/neopolitan/p/8044879.html
Copyright © 2011-2022 走看看