NYOJ 323 && HDU 1532 && POJ 1273 Drainage Ditches (網路流之最大流入門),nyoj323

來源:互聯網
上載者:User

NYOJ 323 && HDU 1532 && POJ 1273 Drainage Ditches (網路流之最大流入門),nyoj323

連結:click here

題意:給出n個河流,m個點,以及每個河流的流量,求從1到m點的最大流量。思路:最裸的網路流題目  意思就是求從源點到匯點的最大流。
第一道網路流,一邊看著書上的介紹,一邊敲下代碼:
用的是網路流演算法ford-fulkerson
題目資料量小,鄰接表和鄰接矩陣都可以過

代碼:

#include <ctype.h>  //最大流 入門#include <stdio.h>#include <vector>#include <stdlib.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;const int maxn = 1101;const int inf=0x3f3f3f3f;struct edge {int to,cap,rev;};vector<edge > G[maxn];        //圖的鄰接表表示bool used[maxn];              //訪問標記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});}int dfs(int v,int t,int f){    if(v==t) return f;    used[v]=true;    for(int i=0;i<G[v].size();i++)    {        edge &e=G[v][i];        if(!used[e.to]&&e.cap>0)        {            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 max_flow(int s,int t){    int flow=0;    for(;;)    {        memset(used,0,sizeof(used));        int f=dfs(s,t,inf);        if(f==0) return flow;        flow+=f;    }}int main(){    int n,m,too,capp,revv;    while(~scanf("%d%d",&n,&m))    {        memset(G,0,sizeof(G));        for(int i=0;i<n;i++)        {            scanf("%d%d%d",&too,&capp,&revv);            add_edge(too,capp,revv);        }        printf("%d\n",max_flow(1,m));    }    return 0;}        

用鄰接矩陣BFS實現:

    #include <cstdio>      #include <vector>      #include <iostream>      #include <queue>      #include <cstring>      using namespace std;      const int N = 300;      const int MAX = 0x3f3f3f3f;      int map[N][N];      int flow[N][N];      int a[N],p[N];            int Ford_fulkerson(int s,int t)      {          queue<int> qq;          memset(flow,0,sizeof(flow));          int f=0,u,v;          while(1)          {              memset(a,0,sizeof(a));              a[s]=MAX;              qq.push(s);              while(!qq.empty())              {                  u=qq.front();qq.pop();                  for(v=1;v<=t;v++)                  {                      if(!a[v]&&map[u][v]>flow[u][v])//找到新結點v                      {                          p[v]=u;qq.push(v);//記錄v的父親,並加入FIFO隊列                          a[v]=a[u]<map[u][v]-flow[u][v]?a[u]:map[u][v]-flow[u][v];//a[v]為s-v路徑上的最小流量                      }                  }              }              if(a[t]==0)                  return f;              for(int i=t;i!=s;i=p[i])              {                  flow[i][p[i]]-=a[t];                  flow[p[i]][i]+=a[t];              }              f+=a[t];          }      }      int main()      {          int n,m;          while(~scanf("%d%d",&n,&m))          {              memset(map,0,sizeof(map));              for(int i=0;i<n;i++)              {                  int x,y,z;                  scanf("%d%d%d",&x,&y,&z);                  map[x][y]+=z;              }              printf("%d\n",Ford_fulkerson(1,m));          }      }  


聯繫我們

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