青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

USACO 4.2.2 第一道網絡流····

Posted on 2010-03-26 13:04 rikisand 閱讀(453) 評論(0)  編輯 收藏 引用 所屬分類: C/C++ 、USACO

Drainage Ditches
Hal Burch

Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.

Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network. Note however, that there can be more than one ditch between two intersections.

Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.

PROGRAM NAME: ditch
INPUT FORMAT

Line 1:
Two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream.

Line 2..N+1:
Each of N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.

SAMPLE INPUT (file ditch.in)
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
OUTPUT FORMAT

One line with a single integer, the maximum rate at which water may emptied from the pond.

SAMPLE OUTPUT (file ditch.out)
50
最基本的網絡流
   1:  #include<iostream>
   2:  #include<fstream>
   3:  #include<string>
   4:  #include<vector>
   5:  #include<map>
   6:  #include<algorithm>
   7:  #include<sstream>
   8:  #include <cstring>
   9:  #include <queue>
  10:  using namespace std;
  11:  const int MAXN = 220;
  12:  const int infi = 0x7FFFFFFF;
  13:   int capacity[MAXN][MAXN], prenode[MAXN], flow[MAXN];
  14:   queue<int> mq; 
  15:   
  16:  int start, end, N;
  17:  void init(){
  18:      freopen("ditch.in","r",stdin);
  19:      //freopen("e:\\usaco\\ditch.in","r",stdin);
  20:      start = 1;  
  21:      scanf("%d %d",&N,&end); int c, s, t;
  22:      memset(capacity,0,sizeof(capacity));
  23:      for(int i=0;i<N;i++)
  24:      {
  25:          scanf("%d %d %d",&c,&s,&t);
  26:          capacity[c][s] += t; //兩個節點間不只有一條路
  27:      } 
  28:  }
  29:  int bfs(){//尋找增廣路徑
  30:      while(!mq.empty()) mq.pop(); 
  31:      mq.push(start);  //源節點入隊
  32:      //memset(flow,0,sizeof(flow));
  33:      memset(prenode,-1,sizeof(prenode)); //重置前向節點
  34:      prenode[start] = 0; flow[start]=infi; //源節點流量無限大
  35:      while(!mq.empty()){
  36:          int cur = mq.front(); 
  37:          mq.pop();
  38:          if(cur == end) break; //到達匯點結束路徑 
  39:          for(int i=1;i<=end;i++){ 
  40:              if(prenode[i]==-1 && capacity[cur][i]) //訪問當前節點所有未訪問的相鄰節點,更新flow
  41:              {
  42:                  prenode[i] = cur;
  43:                  flow[i] = (flow[cur]<capacity[cur][i]?flow[cur]:capacity[cur][i]);
  44:                  mq.push(i);
  45:              }
  46:          }
  47:      }
  48:      if(prenode[end]==-1)  //如果未找到增廣路徑返回-1
  49:          return -1;
  50:      return flow[end];
  51:  }
  52:  int Edmonds_Karp(){
  53:      int total = 0, pathcapacity;//pathcapacity 路徑增加量
  54:      while((pathcapacity = bfs()) != -1){//可以找到增廣路徑時候進行循環
  55:          int cur = end;    //從匯點開始增加逆向節點
  56:          while( cur != start ){
  57:              int t = prenode[cur] ;
  58:              capacity[t][cur] -= pathcapacity;
  59:              capacity[cur][t] += pathcapacity;
  60:              cur = t;
  61:          }
  62:          total += pathcapacity;//max_flow
  63:      }
  64:      return total;
  65:  }
  66:  void output(){
  67:      freopen("ditch.out","w",stdout);
  68:      //freopen("c:\\usaco\\ditch.out","w",stdout);
  69:      cout<<Edmonds_Karp()<<endl;
  70:  } 
  71:     int main()
  72:  {
  73:      init();  
  74:      output();
  75:      return 0;
  76:  }

標程:使用貪心法,尋找一條增廣路徑的時候不斷尋找cap最大的,未被訪問的節點mloc;然后更新跟mloc相鄰的節點flow以

及prenode信息.最后當運行到end時候,更新路徑節點capacity,同時增加max_flow.重復上述過程直到找不到增廣路徑

   1:  #include <stdio.h>
   2:  #include <string.h>
   3:   
   4:  #define MAXI 200
   5:   
   6:  /* total drain amount between intersection points */
   7:  int drain[MAXI][MAXI];
   8:  int nint; /* number of intersection points */
   9:   
  10:  int cap[MAXI]; /* amount of flow that can get to each node */
  11:  int vis[MAXI]; /* has this node been visited by Dijkstra's yet? */
  12:  int src[MAXI]; /* the previous node on the path from the source to here */
  13:   
  14:  int augment(void)
  15:   { /* run a Dijkstra's varient to find maximum augmenting path */
  16:    int lv;
  17:    int mloc, max;
  18:    int t;
  19:   
  20:    memset(cap, 0, sizeof(cap));
  21:    memset(vis, 0, sizeof(vis));
  22:   
  23:    cap[0] = 2000000000;
  24:    while (1)
  25:     {
  26:      /* find maximum unvisited node */
  27:      max = 0;
  28:      mloc = -1;
  29:      for (lv = 0; lv < nint; lv++)
  30:        if (!vis[lv] && cap[lv] > max)
  31:         {
  32:          max = cap[lv];
  33:      mloc = lv;
  34:         }
  35:      if (mloc == -1) return 0;
  36:      if (mloc == nint-1) break; /* max is the goal, we're done */
  37:   
  38:      vis[mloc] = -1; /* mark as visited */
  39:   
  40:      /* update neighbors, if going through this node improves the
  41:         capacity */
  42:      for (lv = 0; lv < nint; lv++)
  43:        if (drain[mloc][lv] > cap[lv] && max > cap[lv])
  44:         {
  45:          cap[lv] = drain[mloc][lv];
  46:      if (cap[lv] > max) cap[lv] = max;
  47:      src[lv] = mloc;
  48:         }
  49:     }
  50:    max = cap[nint-1];
  51:   
  52:    /* augment path, starting at end */
  53:    for (lv = nint-1; lv > 0; lv = src[lv])
  54:     {
  55:      t = src[lv];
  56:      drain[t][lv] -= max;
  57:      drain[lv][t] += max;
  58:     }
  59:    return max;
  60:   }
  61:   
  62:  int main(int argc, char **argv)
  63:   {
  64:    FILE *fout, *fin;
  65:    int lv;
  66:    int num;
  67:    int p1, p2, c;
  68:   
  69:    if ((fin = fopen("ditch.in", "r")) == NULL)
  70:     {
  71:      perror ("fopen fin");
  72:      exit(1);
  73:     }
  74:    if ((fout = fopen("ditch.out", "w")) == NULL)
  75:     {
  76:      perror ("fopen fout");
  77:      exit(1);
  78:     }
  79:   
  80:    fscanf (fin, "%d %d", &num, &nint);
  81:    while (num--)
  82:     {
  83:      fscanf (fin, "%d %d %d", &p1, &p2, &c);
  84:      p1--;
  85:      p2--;
  86:      drain[p1][p2] += c; /* note += handles two ditches between same points */
  87:     }
  88:   
  89:    /* max flow algorithm: augment while you can */
  90:    c = 0;
  91:    while ((p1 = augment()))
  92:      c += p1;
  93:    fprintf (fout, "%d\n", c);
  94:    return 0;
  95:   }

 

 

 

 

 

 

 

 

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美午夜精品久久久久久人妖| 亚洲免费成人av电影| 久热国产精品视频| 亚洲成人资源| 亚洲免费精品| 午夜精品久久久久| 久久久五月婷婷| 欧美日韩国产三级| 国产日韩免费| 136国产福利精品导航| 一本到12不卡视频在线dvd| 午夜在线播放视频欧美| 久久亚洲欧美| 9国产精品视频| 久久亚洲综合色一区二区三区| 欧美日本国产在线| 国产综合自拍| 亚洲一区二区三区免费在线观看| 久久久久久久一区二区| 亚洲国产美女| 久久精品国产精品亚洲综合| 欧美日本精品在线| 一区久久精品| 欧美一区二区三区的| 亚洲第一页中文字幕| 午夜日本精品| 欧美午夜一区二区福利视频| 在线日本欧美| 久久久久99| 这里只有精品视频| 欧美福利一区| 亚洲高清不卡在线观看| 欧美在线观看一区| 洋洋av久久久久久久一区| 巨乳诱惑日韩免费av| 国产日韩精品视频一区| 亚洲图色在线| 亚洲精品日韩一| 欧美大片国产精品| 亚洲大片一区二区三区| 久久嫩草精品久久久久| 亚洲一区二区少妇| 国产精品国产三级国产普通话三级 | 久久午夜色播影院免费高清| 中文一区二区| 欧美少妇一区| 亚洲在线观看视频网站| 亚洲人精品午夜| 欧美国产91| 日韩视频中文字幕| 亚洲精品在线电影| 欧美片第1页综合| 亚洲伦理网站| 亚洲国产欧美一区二区三区久久| 老鸭窝91久久精品色噜噜导演| 国产一区二区三区的电影| 久久精品99无色码中文字幕| 亚洲欧美激情诱惑| 国产视频久久久久| 久久久久久伊人| 久久久久综合| 最新亚洲一区| 亚洲日本无吗高清不卡| 欧美日韩国产美| 亚洲欧美中文另类| 欧美在线播放| 亚洲激情二区| 亚洲麻豆av| 国产美女精品在线| 久久亚洲不卡| 欧美高清视频| 亚洲欧美激情精品一区二区| 午夜精品一区二区三区四区| 极品少妇一区二区三区| 亚洲国产精品视频一区| 欧美午夜精品久久久久久超碰| 欧美在线视频播放| 免费观看一区| 欧美一区永久视频免费观看| 久久人人97超碰精品888 | 欧美激情一区二区| 亚洲性xxxx| 久久久无码精品亚洲日韩按摩| 亚洲精品影院| 午夜精品久久久久久久| 亚洲国产小视频在线观看| 夜夜爽av福利精品导航 | 欧美精品日韩www.p站| 香蕉成人伊视频在线观看| 久久免费视频这里只有精品| 亚洲视频在线观看三级| 久久久久久亚洲精品杨幂换脸| 妖精成人www高清在线观看| 亚洲欧美日韩国产精品| 日韩亚洲欧美成人| 久久av一区二区| 在线中文字幕一区| 久久综合色影院| 欧美一区二区三区日韩| 欧美国产日韩亚洲一区| 欧美在线观看一区| 欧美日韩亚洲一区二区三区四区| 老司机午夜精品视频| 国产精品红桃| 亚洲国语精品自产拍在线观看| 国产日韩欧美亚洲| 日韩视频中午一区| 亚洲精品久久久久久久久| 欧美一区二区三区播放老司机 | 在线观看欧美黄色| 亚洲影视中文字幕| 亚洲一区二区三区在线视频| 免费成人高清| 免费成人毛片| 国产一区二区三区四区在线观看| 国产精品99久久久久久久女警| 亚洲乱码精品一二三四区日韩在线 | 亚洲尤物视频在线| 一区二区不卡在线视频 午夜欧美不卡在 | 美日韩丰满少妇在线观看| 欧美一区二区三区四区高清| 欧美日韩一区二区三区视频| 亚洲国产欧美日韩精品| 亚洲高清网站| 久久亚洲春色中文字幕| 久久午夜电影网| 国产精品羞羞答答| 亚洲欧美另类综合偷拍| 亚洲一区二区三区精品视频| 欧美日韩二区三区| 亚洲日本电影| 99精品欧美一区| 欧美日韩一区二区在线观看视频| 亚洲裸体俱乐部裸体舞表演av| 一区二区三区不卡视频在线观看| 欧美经典一区二区| 99视频+国产日韩欧美| 亚洲视频在线看| 国产精品色在线| 欧美一区二区三区免费观看 | 亚洲国产女人aaa毛片在线| 久久免费的精品国产v∧| 久久综合亚州| 最新国产乱人伦偷精品免费网站 | 欧美日韩一视频区二区| 一区二区免费看| 午夜精品久久久久久久久| 国产精品一区二区三区四区五区| 亚洲免费影院| 久久亚洲一区二区| 亚洲精品乱码久久久久久久久 | 欧美日韩蜜桃| 午夜精品美女久久久久av福利| 久久精品国产第一区二区三区最新章节 | 136国产福利精品导航| 欧美精品aa| 午夜精品久久久久| 欧美高清一区| 亚洲欧美国产不卡| 国内精品久久久久影院色 | 欧美主播一区二区三区| 欧美成人中文字幕| 亚洲欧美电影在线观看| 激情视频亚洲| 国产精品久久久久秋霞鲁丝| 久久精品二区三区| 一区二区激情视频| 欧美成人免费小视频| 亚洲综合视频网| 在线欧美日韩国产| 国产精品日韩一区| 欧美岛国在线观看| 亚洲特级片在线| 欧美激情一区二区三区高清视频| 午夜亚洲福利| 一区二区三区欧美视频| 一区二区三区在线免费观看| 国产精品啊啊啊| 免费美女久久99| 久久不射中文字幕| 一本色道综合亚洲| 亚洲国产精品美女| 老司机aⅴ在线精品导航| 亚洲小说春色综合另类电影| 91久久精品一区| 韩国成人精品a∨在线观看| 欧美日韩亚洲高清| 欧美不卡激情三级在线观看| 久久激情五月婷婷| 亚洲欧美一区二区视频| 一区二区不卡在线视频 午夜欧美不卡在| 欧美99在线视频观看| 久久久久国产成人精品亚洲午夜| 亚洲尤物视频在线| 这里是久久伊人| 日韩视频在线一区二区三区| 91久久国产自产拍夜夜嗨| 影音先锋在线一区| 狠狠噜噜久久| 韩国久久久久|