码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ 3549 Dinitz

时间:2017-12-15 22:37:39      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:div   ini   style   ase   capacity   fine   name   empty   ems   

#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\n", i + 1, dinic(0, n - 1));
    }
    return 0;
}

 

HDOJ 3549 Dinitz

标签:div   ini   style   ase   capacity   fine   name   empty   ems   

原文地址:http://www.cnblogs.com/neopolitan/p/8044879.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!