首页 > 其他分享 >P3376 【模板】网络最大流

P3376 【模板】网络最大流

时间:2022-12-06 22:11:12浏览次数:47  
标签:int sum 网络 st dep ans P3376 now 模板

P3376 【模板】网络最大流

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=205;
const int M=5005;

int h[N],ne[M<<1],e[M<<1],w[M<<1],tot=1;
void add(int from,int to,int wi) {
    e[++tot]=to;
    w[tot]=wi;
    ne[tot]=h[from];
    h[from]=tot;
}

int n,m,st,ed;
int cur[N],dep[N];

bool bfs() {
    for(int i=1;i<=n;i++)cur[i]=h[i],dep[i]=0;
    queue<int>q;
    q.push(st);
    dep[st]=1;
    while(!q.empty()) {
        int now=q.front();
        q.pop();
        for(int i=h[now];i;i=ne[i]) {
            int to=e[i];
            if(dep[to]==0&&w[i]>0) {
                q.push(to);
                dep[to]=dep[now]+1;
            }
        }
    }
    return dep[ed];
}

//sum是流过该点的流量
//ans也就上记录的答案
int dfs(int now,int sum) {
    if(now==ed)return sum;
    int ans=0;
    for(int i=cur[now];i&&sum;i=ne[i]) {//这个点还有流量
        cur[now]=i;
        int to=e[i];
        if(w[i]>0&&(dep[to]==dep[now]+1)) {
            int k=dfs(to,min(sum,w[i]));//从这个点流下去不行了,进行一下剪枝
            if(k==0)dep[to]=0;
            w[i]-=k;
            w[i^1]+=k;
            ans+=k;
            sum-=k;
        }
    }
    return ans;
}

signed main() {
    cin>>n>>m>>st>>ed;
    while(m--) {
        int x,y,wi;
        cin>>x>>y>>wi;
        add(x,y,wi);
        add(y,x,0);//反向边提供后悔的操作
    }
    int ans=0;
    while(bfs())ans+=dfs(st,1e9);
    cout<<ans;
    return 0;
}

标签:int,sum,网络,st,dep,ans,P3376,now,模板
From: https://www.cnblogs.com/basicecho/p/16960828.html

相关文章

  • 实验六 模板类和文件IO
    任务四代码:Vector.hpp:#pragmaonce#include<iostream>usingnamespacestd;template<classT>classVector{public:Vector(intsize0):size{size......
  • 网络渗透基础之HTML(下)
    HTML表格表格由<table>标签来定义。每个表格均有若干行(由<tr>标签定义),每行被分割为若干单元格(由<td>标签定义)。字母td指表格数据(tabledata),即数据单元格的内容。......
  • 实验六 模板类和文件IO
    Task4//VEctor.hpp#include<bits/stdc++.h>usingnamespacestd;template<typenameT>classVector{private:intlength;T*base;publ......
  • 无线传感器网络技术-实验一-TINYOS/6LOWPAN 编程基础
    【实验目的】1.掌握TinyOS/6LoWPAN开发环境的搭建方法2.掌握VisualStudioCode中TinyOS编程的相关设置3.掌握VSCode中CC2530节点(平台名称cc2530zn)和CC......
  • 实验6 模板类和文件IO
    task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){ usingnamespacestd; array<int,N>x{97,98,99,100,101}; ofstr......
  • 实验六 模板类和文件IO
    1.1Vector.hpp:#include<iostream>#include<string>#include<iomanip>usingnamespacestd;template<classT>classVector{public:Vector(intn):s......
  • 详解redis网络IO模型
    前言"redis是单线程的"这句话我们耳熟能详。但它有一定的前提,redis整个服务不可能只用到一个线程完成所有工作,它还有持久化、key过期删除、集群管理等其它模块,redis会通......
  • <五>模板的完全特例化和非完全特例化
    模板作为C++泛型编程的基础十分重要,其使得一份代码能用于处理多种数据类型。而有些时候,我们会希望对一些特定的数据类型执行不同的代码,这时就需要使用模板特例化(template......
  • 网络安全攻防之缓冲区溢出攻击
    什么是缓冲区溢出?缓冲区溢出是一种异常现象,当软件向缓冲区中写入数据使缓冲区容量溢出时,会导致相邻存储器位置被覆盖。换句话说,过量的信息被传递到没有足够空间的容器中,而这......
  • 实验六 模板和文件IO
    task3_11#include<iostream>2#include<fstream>3#include<array>4#defineN556intmain(){7usingnamespacestd;89array<int,......