最大流的裸题,直接贴了模板。

#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define repd(i, a, b) for(int i = b; i >= a; i--)
#define sfi(n) scanf("%d", &n)
#define pfi(n) printf("%d\n", n)
#define sfi2(n, m) scanf("%d%d", &n, &m)
#define pfi2(n, m) printf("%d %d\n", n, m)
#define pfi3(a, b, c) printf("%d %d %d\n", a, b, c)
#define maxn 101
const int inf = 0x3f3f3f3f;
struct SAP
{
int cap[maxn][maxn], flow[maxn][maxn];///容量,流量
int n; ///顶点数
int h[maxn], vh[maxn], source, sink;
///某个点到sink点的最短距离,h[i]的计数, source点, sink点
void init(int n)
{
this->n = n;
memset(cap, , sizeof(cap));
}
void addCap(int i, int j, int val)
{
cap[i][j] += val;
}
/**
参数: 节点编号,和该节点能用的最大流量
返回: 此次找到的最大流量
*/
int sap(const int idx, const int maxCap)
{
if(idx == sink)
return maxCap;///最后一个结点。。。
int l = maxCap, d, minH = n;
///此次的残余流量, 某次使用的流量, 邻居的最小流量
for(int i = ; i < n; i ++)
{
if(cap[idx][i]-flow[idx][i] > )
{
if(h[idx]==h[i]+)
{
d = sap(i, min(l, cap[idx][i]-flow[idx][i]));
///下次找到的流量
flow[idx][i] += d; ///更新边的残余流量
flow[i][idx] -= d;
l -= d; ///更新本次参与流量
if(h[source]==n||l==) return maxCap-l;///GAP
}
minH=min(minH, h[i]+); ///更新h[idx]
}
}
if(l == maxCap) ///not found!
{
vh[h[idx]] --; ///GAP
vh[minH] ++;
if(vh[h[idx]] == )
h[source] = n;
h[idx] = minH;
}
return maxCap - l;
}
int solve(int source, int sink)
{
if(source == sink) return inf;
this->sink = sink;
this->source = source;
memset(flow, , sizeof(flow));
memset(h, , sizeof(h));
memset(vh, , sizeof(vh));
int ans = ;
while(h[source] != n)
ans += sap(source, inf);
return ans;
}
}sap; int main()
{
int s, t, c, n, x, y, w, kase = ;
while(sfi(n) && n)
{
kase++;
sap.init(n);
sfi2(s, t), sfi(c);
s--, t--;
repu(i, , c)
{
sfi2(x, y), sfi(w);
x--, y--;
sap.addCap(x, y, w);
sap.addCap(y, x, w);
}
printf("Network %d\n", kase);
printf("The bandwidth is %d.\n\n", sap.solve(s, t));
}
return ;
}

uva 820(最大流)的更多相关文章

  1. UVA 820 --- POJ 1273 最大流

    找了好久这两个的区别...UVA820 WA了 好多次.不过以后就做模板了,可以求任意两点之间的最大流. UVA 是无向图,因此可能有重边,POJ 1273是有向图,而且是单源点求最大流,因此改模板的 ...

  2. UVA 820 Internet Bandwidth 因特网宽带(无向图,最大流,常规)

    题意:给一个无向图,每条边上都有容量的限制,要求求出给定起点和终点的最大流. 思路:每条无向边就得拆成2条,每条还得有反向边,所以共4条.源点汇点已经给出,所以不用建了.直接在图上跑最大流就可以了. ...

  3. UVA - 820 Internet Bandwidth(最大流模板题)

    题目: 思路: 直接套最大流的模板就OK了,注意一下输出的格式. 代码: #include <bits/stdc++.h> #define inf 0x3f3f3f3f #define M ...

  4. UVA - 820 Internet Bandwidth (因特网带宽)(最大流)

    题意:给出所有计算机之间的路径和路径容量后,求出两个给定结点之间的流通总容量.(假设路径是双向的,且两方向流动的容量相同) 分析:裸最大流.标号从1开始,初始化的时候注意. #pragma comme ...

  5. UVa 820 因特网带宽(最大流)

    https://vjudge.net/problem/UVA-820 题意: 给出所有计算机之间的路径和路径容量后求出两个给定结点之间的流通总容量. 思路: 裸的最大流问题.注意有个比较坑的地方,最后 ...

  6. 紫书 习题 11-3 UVa 820 (最大流裸题)

    注意这道题是双向边, 然后直接套模板就ok了. #include<cstdio> #include<algorithm> #include<vector> #inc ...

  7. UVa 11082 & 最大流的行列模型

    题意: 给出一个矩阵前i行的和与前j列的和,(i∈[1,r],j属于[1,c]),每个元素ai,j∈[1,20],请你还原出这个矩阵,保证有解. SOL: 给网络流建模跪了,神一样的建图,如果我我会怎 ...

  8. uva 12549 最大流

    思路:这题的原型题是比较经典的网络流.原型题模型就是把所有的障碍去掉. 有障碍做法还是一样的,只用将每个列和行重新划分,求最大流就行了. #include <cstring> #inclu ...

  9. uva 10330 最大流

    拆点  将节点 i 的容量拆成从 i 到 i+n 的边的容量 套用最大流模板 ac #include <cstdio> #include <cstdlib> #include ...

随机推荐

  1. jQuery Mobile应用之火车票查询

    效果图: 在CMD中输入如下代码 corsproxy (前提是有node.js环境,并先安装corsproxy) html: <!DOCTYPE html> <html> &l ...

  2. 20161020001 DataGridView 选中的 DataGridViewCheckBoxCell 不添加重复项

    private void btn_add_Click(object sender, EventArgs e)        {            string str_P_ID = "& ...

  3. iOS常见面试题

    一.为什么要在主线程中更新UI,这样做有什么好处? UIKit中的大部分类都不是“线程安全”的,为了解决这个线程不安全的问题,苹果推荐所有应用程序的UI操作都在主线程中执行,这样就不会出现多个线程同时 ...

  4. JS函数 计算 今日,昨日,本周,上周,本月

    最近有个功能会进行数据的筛选于是便写了几个快速计算 今日,昨日,本周,上周,本月 范围的function 以便以后遇到同样的问题可以直接进行复用,代码如下: /* *获取今日的起始和结束时间 *返回值 ...

  5. Github的命令清除

    最近折腾Github,对于我这个英语词汇量不多的人来说就是折磨,几乎是翻着字典探索的. 功能上就不多说了,还没发现新东西,只是刚摸索出如何上传(下载还没弄明白了,苦啊....) 就是在使用GitBas ...

  6. Query Designer:Exception,不同的值显示不同的颜色

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  7. mysql 数据库 表字段添加表情兼容

    项目中的几个需要支持Emoji表情符号,手机自带的表情,其实添加也很简单: 1 修改数据库 配置my.cnf  init-connect='SET NAMES utf8mb4'             ...

  8. angularjs指令系统系列课程(3):替换replace,内容保留transclude,作用方式restrict

    这一节我们主要看一下replace,transclude,restrict这三个参数 1.replace 可取值:bool 默认为:true 对于replace属性,设置为false表示原有指令标识不 ...

  9. iOS - SQLite 数据库存储

    1.SQLite 数据库 SQLite 是一种轻型的嵌入式数据库,安卓和 iOS 开发使用的都是 SQLite 数据库.它占用资源非常低,在嵌入式设备中,可能需要几百 K 的内存数据就够了.他的处理速 ...

  10. html中css、div命名规范

    html中css.div命名规范 1.类class的命名规范示例 头:header 内容:content/container 尾:footer 导航:nav 侧栏:sidebar 栏目:column ...