HDU 3549 Flow Problem

来源:互联网 发布:ubuntu 14.04配置jdk 编辑:程序博客网 时间:2024/05/03 02:07


第一次AC的网络流类型

几乎没有任何装饰的网络流题目,注意有重边存在,记得反向边

代码风格很诡异,请尽情BS我。但是,如果你编译通不过,请改用GCC 3.4.2或者更高版本(GCC3.4.2是Dev-CPP 4.9.9.2用的),me的ACM实践中不考虑对VC的兼容性。

多路增广Dinic见

http://poj.org/showmessage?message_id=129060


有过长的行,经过了语义概括的改写

#include "iostream"#include "cstdio"#include "cstring"using namespace std;struct node { int v, w, c, next, b; } e[1000010];struct G{int h[10100];int r[10100];int pre[10100];int lvl[10100];int q[10100];int map[1000][1000];}g;int h, t, top;int N, M;// dinic(multi-way augment): http://poj.org/showmessage?message_id=129060#define $EACH_EDGE(u, v, limit) \p=g.h[u], v = e[p].v; \p!=-1 && limit; \p=e[p].next, v = e[p].v#define $COND_UV_LEVEL_CAP_LIMIT(with_level, with_cap, with_limit, action) \if (g.lvl[v] with_level && e[p].c with_cap && (with_limit)) \{action;}int dinic(int st, int ed){int ans = 0, flow;while (true){for (int i=st; i<=ed; i++) g.lvl[i]=-1;for (g.lvl[g.q[h=1]=st]=0, t=0; t<h; ) {for (int u = g.q[++t], $EACH_EDGE(u, v, true) ) {$COND_UV_LEVEL_CAP_LIMIT(==-1, !=0, true, g.lvl[g.q[++h]=v]=g.lvl[u]+1);}}if (g.lvl[ed] == -1) return ans;struct dfs { static int self(int u, int flow, int ed) {if (u==ed) return flow;int w=0, t;for (int $EACH_EDGE(u, v, w<flow)) $COND_UV_LEVEL_CAP_LIMIT(==g.lvl[u]+1,    >0,                  (t=self(v, min(e[p].c, flow-w), ed)),{e[p].c-=t;  w+=t; e[e[p].b].c+=t;} ) if (!w) g.lvl[u]=-1;return w;}};while (flow=dfs::self(st, 0xFFFFF, ed)) ans+=flow; }}int main(){int T; int tot;scanf("%d", &T);int ncase = 0;while (T--){scanf("%d%d", &N, &M);tot = 0;memset(g.h,  -1, sizeof(g.h));memset(g.map, 0, sizeof(g.map));for (int i=0, u,v,c; i<M; i++)scanf("%d%d%d", &u, &v, &c), g.map[u][v] += c;for (int i=1; i<=N; i++)for (int j=i; j<=N; j++)if (g.map[i][j] || g.map[j][i]){e[tot]=(node){j, 0, g.map[i][j],g.h[i]}, g.h[i]=tot, tot++;e[tot]=(node){i, 0, g.map[j][i],g.h[j]}, g.h[j]=tot, tot++;e[g.h[i]].b=g.h[j];e[g.h[j]].b=g.h[i];}printf("Case %d: %d\n", ++ncase, dinic(1, N));}}


原创粉丝点击