HDU1532 最大流-模板题

简介: 题目是网络流-最大流的模板题 这里作为学习,把《指南》上Dinic的模板用了一下,代码如下: #include #include #include #include #include using namespace std; const int MAXN = 10000; ...

题目是网络流-最大流的模板题

这里作为学习,把《指南》上Dinic的模板用了一下,代码如下:

#include <stdio.h>
#include <iostream>
#include <queue>
#include <string.h>
#include <vector>
using namespace std;
const int MAXN = 10000;
const int INF = 0x3f3f3f3f;
int min(int a, int b){
    return a
}
struct Edge{
    int from, to, cap, flow;
    Edge(int a, int b, int c, int d): from(a), to(b), cap(c), flow(d) {}
};

struct Dinic {
    int n, m, s, t;
    vector edges;
    vectorG[MAXN];
    bool vis[MAXN];
    int d[MAXN];
    int cur[MAXN];

    void AddEdge(){
        int from, to, cap;
        scanf("%d%d%d", &from, &to, &cap);
        Edge tedge = Edge(from, to, cap, 0);
        edges.push_back(tedge);
        tedge = Edge(to, from, 0, 0);
        edges.push_back(tedge);
        int m = edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }
   
    bool BFS() {
        memset(vis, 0, sizeof(vis));
        queueq;
        q.push(s);
        d[s] = 0;
        vis[s] = 1;
        while(!q.empty()){
            int x = q.front();
            q.pop();
            for(int i=0; i
                Edge &e = edges[G[x][i]];
                if(!vis[e.to] && e.cap > e.flow){
                    vis[e.to] = 1;
                    d[e.to] = d[x] + 1;
                    q.push(e.to);
                }
            }
        }
        return vis[t];
    }
    int DFS(int x, int a){
        if(x==t || a==0) return a;
        int flow = 0, f;
        for(int& i=cur[x]; i
            Edge& e = edges[G[x][i]];
            if(d[x]+1==d[e.to] && (f=DFS(e.to, min(a, e.cap-e.flow))) > 0) {
                e.flow += f;
                edges[G[x][i]^1].flow -= f;
                flow += f;
                a -= f;
                if(a==0) break;
            }
        }
        return flow;
    }
    int Maxflow(int s, int t){
        this->s = s;
        this->t = t;
        int flow = 0;
        while(BFS()) {
            memset(cur, 0, sizeof(cur));
            flow += DFS(s, INF);
        }
        return flow;
    }
};
int main(){
//    freopen("in.txt", "r", stdin);
    int n, m;
    while(scanf("%d%d", &n, &m)!=EOF){
        int i;
        Dinic din;
        for(i=0; i
            din.AddEdge();
        }
        printf("%d\n", din.Maxflow(1, m));
    }
}



目录
相关文章
|
7天前
|
机器学习/深度学习
N皇后问题(HDU—2253)
N皇后问题(HDU—2253)
HDU-1874,畅通工程续(Floyd最短路)
HDU-1874,畅通工程续(Floyd最短路)
|
Java 机器学习/深度学习
UESTC 30 &&HDU 2544最短路【Floyd求解裸题】
最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 65817    Accepted Submission(s): 28794 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。
1125 0
|
并行计算 算法 Java
HDU 1874 畅通工程续【Floyd算法实现】
畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 53806    Accepted Submission(s): 20092 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路。
1064 0
|
Java 测试技术
HDU 1248 寒冰王座(完全背包裸题)
寒冰王座 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17092    Accepted Submission(s): 8800 ...
1185 0
【HDU 4451 Dressing】水题,组合数
有衣服、裤子、鞋数量分别为n,m,k,给出p对不和谐的衣-裤或裤-鞋搭配,问一共有多少种和谐的衣裤鞋的搭配。 全部的组合有Cn1Cm1Ck1种。 设p对中有p1对衣-裤,p2对裤-鞋,则不和谐的搭配共有p1*Ck1+p2*Cn1种,但有被重复计算两次的搭配共p3对,它们引用了同一裤。
884 0
|
容器
hdu3388 Coprime【容斥原理】
Problem Description Please write a program to calculate the k-th positive integer that is coprime with m and n simultaneously.
1106 0