hdu Flow Problem (最大流 裸题)
最大流裸题,贴下模版

view code#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1000;
int n, m, s, t, cas=1, _;
int pre[maxn], d[maxn], cur[maxn];
bool vis[maxn]; struct Edge
{
int from, to, cap, flow, p;
Edge() {}
Edge(int from, int to, int cap, int flow, int pre):from(from), to(to), cap(cap), flow(flow), p(pre) {}
}e[maxn*maxn];
int ecnt = 0; void init()
{
memset(pre, -1, sizeof(pre));
ecnt = 0;
s = 1, t = n;
} bool BFS()
{
memset(vis, 0, sizeof(vis));
queue<int > q;
q.push(s);
vis[s] = 1; d[s] = 0;
while(!q.empty())
{
int u = q.front(); q.pop();
for(int i = pre[u]; ~i; i=e[i].p)
{
int v = e[i].to;
if(!vis[v] && e[i].flow<e[i].cap)
{
d[v] = d[u] + 1;
vis[v] = true;
q.push(v);
}
}
}
return vis[t];
} int DFS(int u, int c)
{
if(u==t || c==0) return c;
int flow = 0, f;
for(int &i=cur[u]; ~i; i=e[i].p)
{
int v = e[i].to;
if(d[v]==d[u]+1 && (f=DFS(v, min(c, e[i].cap-e[i].flow)))>0)
{
e[i].flow += f;
e[i^1].flow -= f;
flow += f;
c -= f;
if(c==0) break;
}
}
return flow;
} int main()
{
// freopen("in.txt", "r", stdin);
scanf("%d", &_);
while(_--)
{
scanf("%d%d", &n, &m);
init();
int u, v, w;
for(int i=0; i<m; i++)
{
scanf("%d%d%d", &u, &v, &w);
e[ecnt] = Edge(u, v, w, 0, pre[u]);
pre[u] = ecnt++;
e[ecnt] = Edge(v, u, 0, 0, pre[v]);
pre[v] = ecnt++;
}
int ans = 0;
while(BFS())
{
for(int i=1; i<=n; i++) cur[i] = pre[i];
ans += DFS(s, INF);
}
printf("Case %d: %d\n", cas++, ans);
}
}
hdu Flow Problem (最大流 裸题)的更多相关文章
- hdu - 3549 Flow Problem (最大流模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=3549 Ford-Fulkerson算法. #include <iostream> #include ...
- [hdu3549]Flow Problem(最大流模板题)
解题关键:使用的挑战程序设计竞赛上的模板,第一道网络流题目,效率比较低,且用不习惯的vector来建图. 看到网上其他人说此题有重边,需要注意下,此问题只在邻接矩阵建图时会出问题,邻接表不会存在的,也 ...
- POJ 1087 最大流裸题 + map
A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15597 Accepted: 5308 ...
- hdu 3549 Flow Problem 最大流问题 (模板题)
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tota ...
- HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Flow Problem(最大流模板)
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tota ...
- hdu 3549 Flow Problem (最大流)
裸最大流,做模板用 m条边,n个点,求最大流 #include <iostream> #include <cstdio> #include <cstring> #i ...
- hdu 3549 Flow Problem 最大流 Dinic
题目链接 题意 裸的最大流. 学习参考 http://www.cnblogs.com/SYCstudio/p/7260613.html Code #include <bits/stdc++.h& ...
- HDU 3376 && 2686 方格取数 最大和 费用流裸题
题意: 1.一个人从[1,1] ->[n,n] ->[1,1] 2.仅仅能走最短路 3.走过的点不能再走 问最大和. 对每一个点拆点限流为1就可以满足3. 费用流流量为2满足1 最大费用流 ...
随机推荐
- .net4.0注册到IIS ,重新注册IIS ,iis注册
IIS和.netfw4.0安装顺序是从前到后,如果不小心颠倒了,无所谓. 打开程序-运行-cmd:输入一下命令重新注册IIS C:\WINDOWS\Microsoft.NET\Framework\v4 ...
- C# SortedList类概念和示例
SortedList 类 [C#] 命名空间: System.Collections 表示键/值对的集合,这些键和值按键排序并可按照键和索引访问. SortedList 是 Hashtable 和 A ...
- Natural language style method declaration and usages in programming languages
More descriptive way to declare and use a method in programming languages At present, in most progra ...
- Hibernate中的一级缓存、二级缓存和懒加载(转)
1.为什么使用缓存 hibernate使用缓存减少对数据库的访问次数,从而提升hibernate的执行效率.hibernate中有两种类型的缓存:一级缓存和二级缓存. 2.一级缓存 Hibenate中 ...
- Glide
1.简介 在泰国举行的谷歌开发者论坛上,谷歌为我们介绍了一个名叫 Glide 的图片加载库,作者是bumptech.这个库被广泛的运用在google的开源项目中,包括2014年google I/O大会 ...
- JavaScript indexOf() 方法和 lastIndexOf() 方法
一,定义和用法 indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置. lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索 ...
- javascript数组浅谈1
最近心血来潮要开始玩博客了,刚好也在看数组这块内容,第一篇就只好拿数组开刀了,自己总结的,有什么不对的地方还请批评指正,还有什么没写到的方面也可以提出来我进行完善,谢谢~~ 首先,大概说说数组的基本用 ...
- Android破解之北斗手机定位系统
我想破解GIS相关的安卓程序,于是找到了这个北斗手机定位系统,且不论它是否能定位,定位精度有多高,本文件只进行破解分析. 在模拟器中安装,输入手机号码,点击"卫星定位",它会一级一 ...
- access中根据一个表创建另一个
access中根据一个表创建另一个 SELECT * INTO newTableFROM zD_qlr; SELECT * into mdFROM zd IN 'E:\fz\高阳\大姚\fz\bz\b ...
- ubuntu解决libstdc++.so.6: cannot open shared object file: No such file or directory:问题
解决libstdc++.so.6: cannot open shared object file: No such file or directory:原因在于,在13.10 版本中,ia32_lib ...