hdu 3549 Flow Problem
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=3549
Flow Problem
Description
Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph.
Input
The first line of input contains an integer T, denoting the number of test cases.
For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 <= M <= 1000)
Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)
Output
For each test cases, you should output the maximum flow from source 1 to sink N.
Sample Input
2
3 2
1 2 1
2 3 1
3 3
1 2 1
2 3 1
1 3 1
Sample Output
Case 1: 1
Case 2: 2
裸的最大流dinic,测模板。。
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using std::min;
using std::find;
using std::sort;
using std::pair;
using std::queue;
using std::vector;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 1100;
const int INF = 0x3f3f3f3f;
struct Dinic {
struct edge { int to, cap, next, rev; }G[N << 2];
int s, t, tot, level[N], ite[N], head[N];
inline void init() {
tot = 0, cls(head, -1);
}
inline void add_edge(int u, int v, int cap) {
G[tot] = (edge){ v, cap, head[u], tot + 1 }; head[u] = tot++;
G[tot] = (edge){ u, 0, head[v], tot - 1 }; head[v] = tot++;
}
inline void built(int n, int m) {
int u, v, f;
s = 1, t = n;
while(m--) {
scanf("%d %d %d", &u, &v, &f);
add_edge(u, v, f);
}
}
inline void bfs(int s) {
cls(level, -1);
queue<int> q;
q.push(s);
level[s] = 0;
while(!q.empty()) {
int u = q.front(); q.pop();
for(int i = head[u]; ~i; i = G[i].next) {
edge &e = G[i];
if(e.cap > 0 && level[e.to] < 0) {
level[e.to] = level[u] + 1;
q.push(e.to);
}
}
}
}
inline int dfs(int u, int t, int f) {
if(u == t) return f;
for(int &i = ite[u]; ~i; i = G[i].next) {
edge &e = G[i];
if(e.cap > 0 && level[u] < level[e.to]) {
int d = dfs(e.to, t, min(e.cap, f));
if(d > 0) {
e.cap -= d;
G[e.rev].cap += d;
return d;
}
}
}
return 0;
}
inline int max_flow() {
int flow = 0;
while(true) {
bfs(s);
if(level[t] < 0) break;
int f;
rep(i, t) ite[i] = head[i];
while((f = dfs(s, t, INF)) > 0) {
flow += f;
}
}
return flow;
}
inline void solve(int n, int m) {
static int k = 1;
init(), built(n, m);
printf("Case %d: %d\n", k++, max_flow());
}
}go;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int t, n, m;
scanf("%d", &t);
while(t--) {
scanf("%d %d", &n, &m);
go.solve(n, m);
}
return 0;
}
hdu 3549 Flow Problem的更多相关文章
- HDU 3549 Flow Problem(最大流)
HDU 3549 Flow Problem(最大流) Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...
- 网络流 HDU 3549 Flow Problem
网络流 HDU 3549 Flow Problem 题目:pid=3549">http://acm.hdu.edu.cn/showproblem.php?pid=3549 用增广路算法 ...
- hdu 3549 Flow Problem【最大流增广路入门模板题】
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Time Limit: 5000/5000 MS (Java/Others ...
- hdu 3549 Flow Problem Edmonds_Karp算法求解最大流
Flow Problem 题意:N个顶点M条边,(2 <= N <= 15, 0 <= M <= 1000)问从1到N的最大流量为多少? 分析:直接使用Edmonds_Karp ...
- HDU 3549 Flow Problem 网络流(最大流) FF EK
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Tot ...
- hdu 3549 Flow Problem (网络最大流)
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tota ...
- HDU 3549 Flow Problem (最大流ISAP)
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tota ...
- hdu 3549 Flow Problem (Dinic)
Flow ProblemTime Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total ...
- hdu 3549 Flow Problem 最大流问题 (模板题)
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tota ...
随机推荐
- 【测试】DG的主切备,备切主
1.首先要应用日志,保持主备库一致: 备库:SBDB@SYS> recover managed standby database using current logfile disconnect ...
- Web 项目 中读取专用配置文件
在 web 开发中,有时我们要为 业务逻辑处理 配置专用的 配置文件,也就是 xml 文件,这样可以极大的方便维护工作,但是读取 专用的配置文件还需要自己写一个方法,在这里,我封装了一个公用 的方法: ...
- linux使用flock文件锁解决crontab冲突问题
* * * * * flock -xn /dev/shm/redis.lock -c "/usr/local/bin/redis-server" 可以用flock命令,配合使用rs ...
- ORA-00933: SQL command not properly ended
今天写了一个小的SQL语句类似下面的这句: UPDATE A SET ID=B.ID FROM A,B WHERE A.NAME=B.NAME 在执行时居然报了“ORA-00933: SQL comm ...
- Winserver2008R2 .netframework4.5 asp.netmvc 访问出现的是文件列表。
Winserver2008R2 .netframework4.5 asp.netmvc 访问出现的是文件列表,服务器需要安装如下的补丁,才可正常访问. http://www.microsoft.com ...
- UIBezierPath IOS贝塞尔曲线
//记录 贝塞尔曲线使用 //根据一个矩形画曲线 + (UIBezierPath *)bezierPathWithRect:(CGRect)rect //根据矩形框的内切圆画曲线 + (UIBezi ...
- 《Unix/Linux日志分析与流量监控》书稿完成
<Unix/Linux日志分析与流量监控>书稿完成 近日,历时3年创作的75万字书稿已完成,本书紧紧围绕网络安全的主题,对各种Unix/Linux系统及网络服务日志进行了全面系统的讲解,从 ...
- 【Python】django模型models的外键关联使用
Python 2.7.10,django 1.8.6 外键关联:http://www.bubuko.com/infodetail-618303.html 字段属性:http://www.cnblogs ...
- Linux:ssh远程执行命令并自动退出
ssh命令格式: [root@localhost ~]# ssh --helpusage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c ...
- JPA 批量新增
1. 在实现类 增加 EntityManager 注入 private EntityManager em; @PersistenceContext(name = "EntityManager ...