网络流最大流dinic

hdu 6214 

#include <bits/stdc++.h>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#define TS printf("!!!\n")
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 201; //点数的最大值
const int MAXM = 10500; //边数的最大值

struct Node
{
        int from, to, next;
        int cap;
} edge[MAXM];
int tol;

int dep[MAXN];//dep为点的层次
int head[MAXN];

int n;
void init()
{
        tol = 0;
        memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int w) //第一条变下标必须为偶数
{
        edge[tol].from = u;
        edge[tol].to = v;
        edge[tol].cap = w;
        edge[tol].next = head[u];
        head[u] = tol++;
        edge[tol].from = v;
        edge[tol].to = u;
        edge[tol].cap = 0;
        edge[tol].next = head[v];
        head[v] = tol++;
}

int BFS(int start, int end)
{
        int que[MAXN];
        int front, rear;
        front = rear = 0;
        memset(dep, -1, sizeof(dep));
        que[rear++] = start;
        dep[start] = 0;
        while (front != rear)
        {
                int u = que[front++];
                if (front == MAXN)
                {
                        front = 0;
                }
                for (int i = head[u]; i != -1; i = edge[i].next)
                {
                        int v = edge[i].to;
                        if (edge[i].cap > 0 && dep[v] == -1)
                        {
                                dep[v] = dep[u] + 1;
                                que[rear++] = v;
                                if (rear >= MAXN)
                                {
                                        rear = 0;
                                }
                                if (v == end)
                                {
                                        return 1;
                                }
                        }
                }
        }
        return 0;
}
int dinic(int start, int end)
{
        int res = 0;
        int top;
        int stack[MAXN];//stack为栈,存储当前增广路
        int cur[MAXN];//存储当前点的后继
        while (BFS(start, end))
        {
                memcpy(cur, head, sizeof(head));
                int u = start;
                top = 0;
                while (1)
                {
                        if (u == end)
                        {
                                int min = INF;
                                int loc;
                                for (int i = 0; i < top; i++)
                                        if (min > edge[stack[i]].cap)
                                        {
                                                min = edge[stack[i]].cap;
                                                loc = i;
                                        }
                                for (int i = 0; i < top; i++)
                                {
                                        edge[stack[i]].cap -= min;
                                        edge[stack[i] ^ 1].cap += min;
                                }
                                res += min;
                                top = loc;
                                u = edge[stack[top]].from;
                        }
                        for (int i = cur[u]; i != -1; cur[u] = i = edge[i].next)
                                if (edge[i].cap != 0 && dep[u] + 1 == dep[edge[i].to])
                                {
                                        break;
                                }
                        if (cur[u] != -1)
                        {
                                stack[top++] = cur[u];
                                u = edge[cur[u]].to;
                        }
                        else
                        {
                                if (top == 0)
                                {
                                        break;
                                }
                                dep[u] = -1;
                                u = edge[stack[--top]].from;
                        }
                }
        }
        return res;
}

int main()
{
        int time;
        cin >> time;
        int st, tp;
        int u, v, w;
        while (time--)
        {
                init();
                int n, m;
                scanf("%d %d", &n, &m);
                scanf("%d %d", &st, &tp);
                //TS;
                for (int i = 1; i <= m; i++)
                {
                        scanf("%d %d %d", &u, &v, &w);
                        addedge(u, v, w * 10000 + 1);
                }
                //TS;
                cout << dinic(st,tp) % 10000 << endl;
        }
}

 

posted @ 2017-10-17 01:23  Aragaki  阅读(248)  评论(0编辑  收藏  举报