HihoCoder #1369 : 网络流一·Ford-Fulkerson算法
来源:互联网 发布:华云数据集团有限公司 编辑:程序博客网 时间:2024/05/01 02:44
1.题面
http://hihocoder.com/problemset/problem/1369
2.题意
给你一张图,求从1到n的最大流
3.思路
使用Dinic算法
4.代码
/***************************************************************** > File Name: cpp_acm.cpp > Author: Uncle_Sugar > Mail: uncle_sugar@qq.com > Created Time: Sun 02 Oct 2016 13:36:50 CST*****************************************************************/# include <cstdio># include <cstring># include <cctype># include <cmath># include <cstdlib># include <climits># include <iostream># include <iomanip># include <set># include <map># include <vector># include <stack># include <queue># include <algorithm>using namespace std;# define rep(i,a,b) for (i=a;i<=b;i++)# define rrep(i,a,b) for (i=b;i>=a;i--)# define mset(aim, val) memset((aim), (val), sizeof(aim))template<class T>void PrintArray(T* first,T* last,char delim=' '){ for (;first!=last;first++) cout << *first << (first+1==last?'\n':delim);}/*1.see the size of the input data before you select your algorithm 2.cin&cout is not recommended in ACM/ICPC3.pay attention to the size you defined, for instance the size of edge is double the size of vertex*/const int debug = 1;//# const int size = 10 + ; const int INF = INT_MAX>>1;typedef long long ll;const int MAXN = 100 + 100000;const int MAXM = 100 + 200000;struct Edge{int to, nxt, f;};struct Dinic_MaxFlow{int head[MAXN], tot;Edge edge[MAXM];int level[MAXN];void init(){tot = 0; mset(head, -1);}void addedge(int from, int to, int f){edge[tot].to = to; edge[tot].f = f;edge[tot].nxt = head[from]; head[from] = tot++;edge[tot].to = from; edge[tot].f = 0;edge[tot].nxt = head[to]; head[to] = tot++;}bool bfs(int s, int t){static queue<int> que;while (!que.empty()) que.pop();mset(level, 0);level[s] = 1;que.push(s);while (!que.empty()){int c = que.front();que.pop();if (c == t) return true;for (int e = head[c]; ~e; e = edge[e].nxt){int to = edge[e].to, f = edge[e].f;//# cout << "to = " << to << endl;if (!level[to] && f){level[to] = level[c] + 1;que.push(to);}}}return false;}int dfs(int u, int t, int sup){if (u == t) return sup;int ret = 0;for (int e = head[u]; ~e; e = edge[e].nxt){int to = edge[e].to, f = edge[e].f;//# cout << "to = " << to << endl;if (level[to] == level[u] + 1 && f){int mi = min(sup - ret, f);int tf = dfs(to, t, mi);edge[e].f -= tf;edge[e^1].f += tf;ret += tf;if (ret == sup)return ret;}}return ret;}int Dinic(int s, int t){int ret = 0;while (bfs(s, t)) ret += dfs(s, t, INF);return ret;}}dinic;int main(){/*std::ios::sync_with_stdio(false);cin.tie(0);*/int n, m;while (~scanf("%d%d", &n, &m)){dinic.init();for (int i = 0; i < m; i++){int a, b, c;scanf("%d%d%d", &a, &b, &c);dinic.addedge(a, b, c);//# dinic.addedge(b, a, c);}printf("%d\n", dinic.Dinic(1, n));//# cout << dinic.Dinic(1, n) << endl;}return 0;}
0 0
- HihoCoder #1369 : 网络流一·Ford-Fulkerson算法
- hihocoder #1369 : 网络流一·Ford-Fulkerson算法
- hihocoder 1369: 网络流一·Ford-Fulkerson算法
- hihocoder#1369 : 网络流一·Ford-Fulkerson算法
- hihoCoder 题目1 : 网络流一·Ford-Fulkerson算法
- 网络流一·Ford-Fulkerson算法
- hihoCoder 1369 网络流一·Ford-Fulkerson算法 (网络流学习#1 记录)
- hihoCoder-第115周-网络流一·Ford-Fulkerson算法
- hihocode#1369 : 网络流一·Ford-Fulkerson算法(EK)
- hihocoder1369 网络流一·Ford-Fulkerson算法
- 网络流--ford-fulkerson算法
- 网络流Ford—Fulkerson算法
- 网络流问题Ford-Fulkerson算法详解
- 最大网络流的Ford-Fulkerson 算法
- 增广路算法 Ford-Fulkerson算法 网络最大流问题
- 网络流Ford-fulkerson算法及dinic算法
- 网络流 (Ford-Fulkerson)
- 最大流 Ford-Fulkerson算法
- java日期类型互转
- Google的AI翻译正在接近人类的水平:
- C++ Primer Plus(再谈重载-一个矢量类)
- 范式说明和数据库脏读、幻读、不可重复读
- 关于ext用到了自己定义的插件时间(精确到秒)一些注意的地方
- HihoCoder #1369 : 网络流一·Ford-Fulkerson算法
- GLM Intro - Matrix Transform Example
- codeforces 722C (并查集)
- android_07_表格布局
- 安卓混淆
- ROS导航-----slam_gmapping使用Log数据创建地图
- HDU 5514 Frogs(容斥原理 gcd)
- hdu2688 Rotate(树状数组)
- Java的自学之路-return与break的差别