【網路流第三彈】HDU 3549——Flow Problem(dinic解法)

來源:互聯網
上載者:User

來源:點擊開啟連結

求有向圖的網路流,DINIC模板過,稍作修改即可。

DINIC還是非遞迴版比較好的,省時間。

// 1.設定上限MAXN和MAXN 2.初始化數值src, des, nNode 3.init_adj() & ex_addEdge() 4.dinic()#include <cstdio>#include <iostream>#include <cstring>#include <string>#include <queue>#define init_adj() (eid = 0,memset(p, -1, sizeof (p)))using namespace std;const int MAXN=1000,MAXM=120000,INF = 0x3fffffff;struct node {int v, next,flw;} e[MAXM];int eid, p[MAXN], lv[MAXN],src, des, nNode;void ex_addEdge(int u, int v, int c1, int c2) {    e[eid].v = v;    e[eid].flw = c1;    e[eid].next = p[u];    p[u] = eid++;    e[eid].v = u;    e[eid].flw = c2;    e[eid].next = p[v];    p[v] = eid++;}int bfs() {    static int que[MAXN];    int i, u, v, c, cur, f = 0, r = 1;    memset(lv, 0, sizeof (lv));    lv[src] = 1, *que = src;    while (f < r) {        cur = que[f++], f %= MAXN;        for (i = p[cur]; ~i; i = e[i].next) {            u = cur, v = e[i].v, c = e[i].flw;            if (!lv[v] && c) {                lv[v] = lv[u] + 1;                que[r++] = v, r %= MAXN;            }        }    }    return lv[des];}int dfs() {    static int tp[MAXN], stk[MAXN];    memcpy(tp, p, sizeof (p));    int cur, top = -1, flow = 0, u, v, i, part, ind;    while (1) {        if (top < 0) {            for (i = tp[v = src]; ~i; i = e[i].next)                if (e[i].flw && lv[e[i].v] == 2)                    break;            if (i == -1)                break;            stk[++top] = tp[src] = i;        }        if (e[cur = stk[top]].v != des) {            u = e[cur].v;            for (i = tp[u]; ~i; i = e[i].next)                if (e[i].flw && lv[u] + 1 == lv[e[i].v])                    break;            if (~i)                stk[++top] = tp[u] = i;            else                lv[u] = INF, --top;        } else {            for (part = INF, i = 0; i <= top; i++)                if (e[stk[i]].flw < part)                    part = e[stk[ind = i]].flw;            flow += part;            for (i = 0; i <= top; i++)                e[stk[i]].flw -= part, e[stk[i]^1].flw += part;            top = --ind;        }    }    return flow;}int dinic() {    int ans = 0, t;    while (bfs())        while (t = dfs())            ans += t;    return ans;}int main(){int testcase;int v,w,flow,res,point,road;cin>>testcase;for(int i=1;i<=testcase;i++){init_adj();cin>>point>>road;src=1;des=point;nNode=point;for(int t=1;t<=road;t++){cin>>v>>w>>flow;ex_addEdge(v,w,flow,0);}res=dinic();cout<<"Case "<<i<<": "<<res<<endl;}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.