zoukankan      html  css  js  c++  java
  • 洛谷P3376:【模板】网络最大流

    https://www.luogu.org/problemnew/show/P3376

    题目描述

    如题,给出一个网络图,以及其源点和汇点,求出其网络最大流。

    输入输出格式

    输入格式:

    第一行包含四个正整数N、M、S、T,分别表示点的个数、有向边的个数、源点序号、汇点序号。

    接下来M行每行包含三个正整数ui、vi、wi,表示第i条有向边从ui出发,到达vi,边权为wi(即该边最大流量为wi)

    输出格式:

    一行,包含一个正整数,即为该网络的最大流。

    输入输出样例

    输入样例#1: 复制

    4 5 4 3
    4 2 30
    4 3 20
    2 3 20
    2 1 30
    1 3 40

    输出样例#1: 复制

    50

    说明

    时空限制:1000ms,128M

    数据规模:

    对于30%的数据:N<=10,M<=25

    对于70%的数据:N<=200,M<=1000

    对于100%的数据:N<=10000,M<=100000

    样例说明:

    题目中存在3条路径:

    4-->2-->3,该路线可通过20的流量

    4-->3,可通过20的流量

    4-->2-->1-->3,可通过10的流量(边4-->2之前已经耗费了20的流量)

    故流量总计20+20+10=50。输出50。

    最大流模板:

    #include <stdio.h>
    #include <string.h>
    #include <vector>
    #include <algorithm>
    using namespace std;
    #define N 10020
    int book[N], l[N], n, m, inf=99999999;
    struct edge{
    	int to;
    	int cap;
    	int rev;
    }; 
    vector<edge>G[N];
    void add (int from, int to, int cap)
    {
    	G[from].push_back((edge){to, cap, G[to].size()});
    	G[to].push_back((edge){from, 0, G[from].size()-1});
    }
    int dfs(int v, int t, int f)
    {
    	int i;
    	if(v==t)
    		return f;
    	book[v]=1;
    	for(i=0; i<G[v].size(); i++)
    	{
    		edge &e=G[v][i];
    		if(!book[e.to] && e.cap>0){
    			int d=dfs(e.to, t, min(f, e.cap));
    			if(d>0)
    			{
    				e.cap-=d;
    				G[e.to][e.rev].cap+=d;
    				return d;
    			}
    		}
    	}
    	return 0;
    }
    int max_flow(int s, int t)
    {
    	int flow=0;
    	while(1)
    	{
    		memset(book, 0, sizeof(book));
    		int f=dfs(s, t, inf);
    		if(f==0)
    			return flow;
    		flow+=f;
    	}
    }
    int main()
    {
    	int i, u, v, w, S, T;
    	scanf("%d%d%d%d", &n, &m, &S, &T);
    	while(m--)
    	{
    		scanf("%d%d%d", &u, &v, &w);
    		add(u, v, w);
    	}
    	printf("%d
    ", max_flow(S, T));
    	return 0;
    }
  • 相关阅读:
    mysql show profiles 使用分析sql 性能
    面向对象三大特征---封装、继承、多态
    http_build_query用法,挺方便的
    请求数据
    多模匹配算法之Aho-Corasick
    php命名空间如何引入一个变量类名?
    MySQL错误:Can't connect to MySQL server (10060)
    Vim完全教程
    路由
    wireshark
  • 原文地址:https://www.cnblogs.com/zyq1758043090/p/11852610.html
Copyright © 2011-2022 走看看