hdu 3549 Flow Problem(最大流)
来源:互联网 发布:数学学术论文知乎 编辑:程序博客网 时间:2024/05/02 01:04
最大流模板题
EK
#include <stdio.h>#include <string.h>#include <algorithm>#include <queue>#include <iostream>using namespace std;#define tt printf("ewgaergasergs\n");const int N = 10005;struct node{ int nxt, w, v;}e[N];int head[100], re[100], pre[100];int cnt, n, m;queue<int> q;void init(){ cnt = 0; memset( head, -1, sizeof( head ));}void add( int u, int v, int w){ e[cnt].v = v; e[cnt].w = w; e[cnt].nxt = head[u]; head[u] = cnt++; e[cnt].v = u; e[cnt].w = 0; e[cnt].nxt = head[v]; head[v] = cnt++;}int EK(){ while( !q.empty() ) q.pop(); memset( pre, -1, sizeof( pre )); q.push(1); bool OK = 0; while( !q.empty() ) { int u = q.front(); q.pop(); for( int i = head[u]; ~i; i = e[i].nxt ) { int to = e[i].v; if( pre[to] == -1 && e[i].w > 0 ) { pre[to] = u; re[to] = i; if( to == n ) { OK = 1; break; } else q.push(to); } } if( OK ) break; } if( !OK ) return 0; int minflow = 99999999; int now = n; //printf("%d\n", pre[1]); while( 1 ) { minflow = min( minflow, e[re[now]].w); now = pre[now]; if( now == 1 ) break; } now = n; while( 1 ) { e[re[now]].w -= minflow; e[re[now]^1].w += minflow; now = pre[now]; if( now == 1 ) break; } return minflow;}int main(){ int tot, cas = 1; scanf("%d\n", &tot); for( ; cas <= tot; cas++ ) { init(); scanf("%d%d", &n, &m); int u, v, w; while(m--) { scanf("%d%d%d", &u, &v, &w); add(u, v, w); } int tmp, ans = 0; while( tmp = EK() ) { ans += tmp; } printf("Case %d: %d\n", cas, ans); } return 0;}/*514 51 2 401 4 202 4 202 3 303 4 10*/
SAP:
#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define lson l, mid, rt << 1#define rson mid + 1, r, rt << 1 | 1#define ls rt << 1#define rs rt << 1 | 1#define pi acos(-1.0)#define eps 1e-8typedef long long ll;const int inf = 0x3f3f3f3f;const int N = 2000;const int E = 10005;struct node{int nxt, v, cap, w;}e[E];int n, m, nn;int st, ed;int cnt;int head[N];int dep[N];int cur[N];int gap[N];int pre[N];void init(){cnt = 0;memset( head, -1, sizeof( head ) );st = 1, ed = n, nn = n + 1;}void add( int u, int v, int w, int rw = 0 ){e[cnt].v = v;e[cnt].cap = w;e[cnt].w = 0;e[cnt].nxt = head[u];head[u] = cnt++;e[cnt].v = u;e[cnt].cap = rw;e[cnt].w = 0;e[cnt].nxt = head[v];head[v] = cnt++;}int isap(){memset( gap, 0, sizeof( gap ) );memset( dep, 0, sizeof( dep ) );memcpy( cur, head, sizeof head );int u = st;pre[u] = -1;gap[0] = nn;int flow = 0;while( dep[st] < nn ){if( u == ed ){int minn = inf, tmp;for( int i = pre[u]; ~i; i = pre[e[i^1].v] ){if( minn > e[i].cap - e[i].w ){minn = e[i].cap - e[i].w;}}for( int i = pre[u]; ~i; i = pre[e[i^1].v] ){e[i].w += minn;e[i^1].w -= minn;}u = st;flow += minn;}bool f = 0;int v;for( int i = cur[u]; ~i; i = e[i].nxt ){v = e[i].v;if( e[i].cap - e[i].w && dep[v]+1 == dep[u] ){f = 1;cur[u] = pre[v] = i;break;}}if( f ){u = v;continue;}int minn = nn;for( int i = head[u]; ~i; i = e[i].nxt ){if( e[i].cap - e[i].w && dep[e[i].v] < minn ){cur[u] = i;minn = dep[e[i].v];}}gap[dep[u]]--;if( !gap[dep[u]] )return flow;dep[u] = minn + 1;gap[dep[u]]++;if( u != st )u = e[pre[u]^1].v;}return flow;}int main(){int tot;scanf("%d", &tot);for( int ca = 1; ca <= tot; ++ca ){scanf("%d%d", &n, &m);init();int u, v, w;for( int i = 1; i <= m; ++i ){scanf("%d%d%d", &u, &v, &w);add( u, v, w );}int ans = isap();printf("Case %d: ", ca);printf("%d\n", ans);}}
0 0
- HDU-3549 Flow Problem【最大流】
- hdu 3549 Flow Problem【最大流】
- hdu 3549 Flow Problem 最大流
- hdu 3549 Flow Problem (最大流)
- hdu - 3549 - Flow Problem(最大流)
- Hdu 3549 Flow Problem - 最大流
- HDU 3549 Flow Problem 最大网络流
- hdu 3549 最大网络流 Flow Problem
- hdu 3549 Flow Problem(最大流)
- HDU 3549 Flow Problem (最大流)
- HDU 3549--Flow Problem【最大流】
- HDU 3549--Flow Problem 【最大流 && dinic】
- HDU 3549 Flow Problem【最大流】
- HDU 3549 Flow Problem(最大流)
- hdu 3549 Flow Problem【Dinic最大流】
- HDU 3549 Flow Problem(最大流)
- HDU 3549 Flow Problem (最大流)
- Hdu 3549 Flow Problem(最大流)
- 打造博客的品牌效应
- 将大写字母转化成小写字母
- No package 'glibmm-2.4' found ( When install Mango)
- 第三方开始大力开发艰苦拉萨地方阿斯蒂芬阿萨德
- 解决C++中宏定义导致的名字污染
- hdu 3549 Flow Problem(最大流)
- gruntfile
- 银行的利润到底有多高?
- Asp.net判断图片存在的并获取图片
- 蓝桥杯试题集 基础练习 特殊回文数
- java的Future和FutureTask
- 7.ios之xib的使用
- [IOS]你用过原生的 UIRefreshControl 吗?
- Linux账号管理