zoj 2314 reactor cooling(無源匯有上下界可行流)__網路流

來源:互聯網
上載者:User

初次接觸,https://www.cnblogs.com/liu-runda/p/6262832.html,通過這個部落格學習的建圖方式。建好圖後套板子即可。

#include <bits/stdc++.h>using namespace std;const int MAXN = 300;const int INF = 0x7fffffff;struct edge{    edge(){}    edge(int _to, int _cap, int _rev, int _id)    :to(_to),cap(_cap),rev(_rev),id(_id){}    int to,cap,rev,id;};vector<edge> G[MAXN];int level[MAXN];int iter[MAXN];void addedge(int from, int to, int cap, int id){    G[from].push_back(edge(to, cap, G[to].size(), 0));    G[to].push_back(edge(from, 0, G[from].size()-1, id));}//先分層,然後在分層圖上dfsvoid bfs(int s){    memset(level, -1, sizeof(level));    queue<int> que;    level[s] = 0;    que.push(s);    while(!que.empty())    {        int v = que.front();        que.pop();        for(int i = 0; i < G[v].size(); ++i)        {            edge& e = G[v][i];            if(e.cap > 0 && level[e.to] < 0)            {                level[e.to] = level[v] + 1;                que.push(e.to);            }        }    }}int dfs(int v, int t, int f){    if(v == t) return f;    for(int &i = iter[v]; i < G[v].size(); ++i)    {        edge &e = G[v][i];        if(e.cap > 0 && level[v] < level[e.to])        {            int d = dfs(e.to, t, min(f, e.cap));            if(d > 0)            {                e.cap -= d;                G[e.to][e.rev].cap += d;                return d;            }        }    }    return 0;}int maxFlow(int s, int t){    int flow = 0;    while(true)    {        bfs(s);        if(level[t] < 0) return flow;        memset(iter,0,sizeof(iter));        int f;        while((f = dfs(s,t,INF)) > 0)            flow += f;    }}int low[100000];int totFlow[MAXN];int res[100000];int main(){    int T,n,m,u,v,s,t,up;    scanf("%d",&T);    while(T--)    {        memset(totFlow,0,sizeof(totFlow));        memset(res,0,sizeof(res));        scanf("%d %d",&n,&m);        s = 0, t = n+1;        for(int i = 1; i <= m; ++i)        {            scanf("%d %d %d %d",&u,&v,&low[i],&up);            addedge(u,v,up-low[i],i);            totFlow[u] -= low[i];            totFlow[v] += low[i];        }        int sum = 0;        for(int i = 1; i <= n; ++i)        {            if(totFlow[i] < 0)                addedge(i,t,-totFlow[i],0);            else            {                sum += totFlow[i];                addedge(s,i,totFlow[i],0);            }        }        if(maxFlow(s,t) == sum)        {            puts("YES");            for(int u = 1; u <= n; ++u)            {                for(int i = 0; i < G[u].size(); ++i)                {                    int v = G[u][i].to;                    if(v == t) continue;                    if(G[u][i].id)                        res[G[u][i].id] += (low[G[u][i].id] + G[u][i].cap);                }            }            for(int i = 1; i <= m; ++i)                printf("%d\n",res[i]);        }        else            puts("NO");        for(int i = 0; i <= n+1; ++i)            G[i].clear();    }    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.