《網路流學習筆記03&&POJ1459 Power Network》

來源:互聯網
上載者:User

標籤:網路流 最大流

題目連結:click here

題意:一個電力網路有n個點,有np個發電站,nc個消耗點,其餘的為中轉站。m條電纜,中轉站既不發電也不耗電。每條電纜都有一個最大容量。
思路:設定一個超級源點和一個超級匯點,將所有的源點和匯點分別放進去,Dinic 演算法實現。

注意括弧的處理。

代碼:

#include <math.h>#include <queue>#include <deque>#include <vector>#include <stack>#include <stdio.h>#include <ctype.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <algorithm>using namespace std;#define Max(a,b) a>b?a:b#define Min(a,b) a>b?b:a#define mem(a,b) memset(a,b,sizeof(a))int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};const double eps = 1e-6;const double Pi = acos(-1.0);const int maxn = 505;const int inf=0x3f3f3f3f;struct edge{    int to, cap, rev;};vector <edge> G[maxn];int level[maxn];      //頂點到源點的距離編號int iter[maxn];      //當前弧,在其之前的邊不用再考慮int n,np,nc,m,i,j,u,v,z;void add_edge(int from, int to, int cap){    G[from].push_back((edge)    {        to, cap, G[to].size()    });    G[to].push_back((edge)    {        from, 0, G[from].size()-1    });}//bfs用來計算從源點出發所有點的距離編號void 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);            }        }    }}//通過DFS尋找當前的最短的增廣路int dfs(int v, int t, int f){    if (v == t) return f;    for (int &i = iter[v]; i < G[v].size(); i++)  //這裡用引用,巧妙地修改了iter數組    {        edge &e = G[v][i];        if (e.cap > 0 && level[v] < level[e.to])  //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;}void input(){    char str[100];    int a, b, c;    int s = n;    int t = n + 1;    for (int i = 0; i < m; i++)    {        scanf("%[^(]", str);        scanf("(%d,%d)%d", &a, &b, &c);        add_edge(a, b, c);    }    for (int i = 0; i <np; i++)    {        scanf("%[^(]", str);        scanf("(%d)%d", &a, &c);        add_edge(s, a, c);    }    for (int i = 0; i < nc; i++)    {        scanf("%[^(]", str);        scanf("(%d)%d", &a, &c);        add_edge(a, t, c);    }}int max_flow(int s, int t){    int flow = 0;    for(;;)    {        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 main(){    while (~scanf("%d%d%d%d", &n, &np, &nc, &m))    {        char str;        for (i = 0; i < maxn; i++) G[i].clear();        input();        printf("%d\n", max_flow(n, n+1));    }    return 0;}


《網路流學習筆記03&&POJ1459 Power Network》

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.