zoukankan      html  css  js  c++  java
  • Drainage Ditches USACO 4.2 (最大流,BFS)

    算法导论求最大流这一节还没看过,下午看了下,参照了大牛的代码模仿写了Ford-Fulkerson+BFS过了...

      1 /*
      2 
      3 ID: hubiao cave
      4 
      5 PROG: ditch
      6 
      7 LANG: C++
      8 
      9 */
     10 
     11 
     12 
     13 
     14 #include<iostream>
     15 
     16 #include<fstream>
     17 
     18 #include<cstring>
     19 #include<queue>
     20 #include<deque>
     21 
     22 using namespace std;
     23 
     24 
     25 
     26 int E[201][201];
     27 int m,n;
     28 bool visited[201];
     29 int prenumber;
     30 queue<pair<int,int> > intqueue;
     31 deque<pair<int,int> >intde;
     32 
     33 void BFS();
     34 void PreBFS();
     35 int main()
     36 
     37 {
     38 
     39 
     40     ifstream fin("ditch.in");
     41 
     42     ofstream fout("ditch.out");
     43 
     44     fin>>n>>m;
     45     for(int i=1;i<=n;i++)
     46     {
     47         int x,y,z;
     48         fin>>x>>y>>z;
     49         E[x][y]+=z;
     50     }
     51     int ans=0;
     52     for(;;)
     53     {
     54         PreBFS();
     55         BFS();
     56         if(prenumber==0)
     57             break;
     58         int min=2111111111;
     59         
     60         int pre=intde.back().second;
     61         do
     62         {
     63             while(intde.back().second!=pre)
     64                 intde.pop_back();
     65             pair<int,int>cur=intde.back();
     66             intde.pop_back();
     67             if(E[cur.first][cur.second]<min&&cur.first!=0)
     68                 min=E[cur.first][cur.second];
     69             if(cur.first!=0)
     70             intde.push_front(cur);
     71             pre=cur.first;
     72         }while(pre!=0);
     73         
     74         ans+=min;
     75         while(!intde.empty())
     76         {
     77             pair<int,int> pii=intde.front();
     78             intde.pop_front();
     79             E[pii.first][pii.second]-=min;
     80             E[pii.second][pii.first]+=min;
     81         }
     82     }
     83 
     84     fout<<ans<<endl;
     85     return 0;
     86 
     87 
     88 }
     89 
     90 void BFS()
     91 {
     92     intqueue.push(make_pair(0,1));
     93     while(!intqueue.empty())
     94     {
     95         pair<int,int> cur=intqueue.front();    
     96         if(visited[cur.second])
     97         {
     98             intqueue.pop();
     99             continue;
    100         }
    101         if(cur.second==m)
    102         {
    103             prenumber=cur.first;
    104             intde.push_back(cur);
    105             return;
    106         }
    107         visited[cur.second]=true;
    108         intde.push_back(cur);
    109         intqueue.pop();
    110         for(int i=1;i<=m;i++)
    111         {
    112             if(E[cur.second][i]&&!visited[i])
    113             {
    114                 intqueue.push(make_pair(cur.second,i));
    115             }
    116         }
    117     }
    118 }
    119 
    120 void PreBFS()
    121 {
    122     while(!intqueue.empty())
    123     {
    124         intqueue.pop();
    125     }
    126     intde.clear();
    127     prenumber=0;
    128     memset(visited,0,sizeof(visited));
    129 
    130 }
  • 相关阅读:
    下载文件
    利用 js 获取地址栏参数
    子组件向父组件传值
    cordova 插件 调用iOS社交化分享(ShareSDK:微信QQ分享)
    cordova 企业应用打包Archive的时候报 "#import <Cordova file not found"
    企业应用打包的时候 修改ipa包的bundle identifier
    Mac下利用safari调试 Cordova的WebApp
    Mac下利用Cordova打包 iOS App以及出现的问题
    数据库设计流程
    Javascript 获取dom的宽度 随笔一
  • 原文地址:https://www.cnblogs.com/cavehubiao/p/3418643.html
Copyright © 2011-2022 走看看