The experience of getting started with network streaming

1. Describe the network flow problem in one sentence

  Directed graphs have multiple edges, and each edge has a capacity limit. Specify the start and end points, and find the maximum capacity from the start point to the end point.

2. Describe the basic network algorithm in one sentence

  Residual network : Consider directed edges and consume forward capacity if forward traffic is passed. At the same time increase reverse traffic.
  Augmented path : Considering a directed graph, if a path can be found that can go from the beginning to the end under the current traffic state, and the minimum capacity of the path is greater than zero, it is considered that this path can be selected as the answer. Max Flow : Update the answer into the residual network each time an " augmenting path " is found. Until a certain augmentation path is not found after completing the search .
  

3. Describe the pit encountered in one sentence

  After first seeing the concept of augmented path , the network flow is regarded as an undirected graph model.

Fourth, a list indicates that the basic algorithm of network flow can pass the problem

  http://hihocoder.com/problemset/problem/1369  

  http://acm.hdu.edu.cn/showproblem.php?pid=3549  

  http://poj.org/problem?id=1273

Five, almost universal code:

#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include<queue>
#include<string.h>
#include<math.h>

using namespace std;

#define ll long long
#define pp pair<ll,ll>
#define vecp vector<pp>
#define veci vector<int>

const ll MAXN=500+233;
const ll INF = 50000000+23333;
 

class edge
{
    public :
        ll a,b,c;
        edge(ll a,ll b,ll c)
        {
            this->a=a;
            this->b=b;
            this->c=c;
        }
        edge(){}
};

Case G [MAXN];
vector<edge> path;
ll vis[MAXN];
ll n,m;


void add_edge(ll a,ll b,ll c)
{
    // path.push_back(make)
    path.push_back(edge(a,b,c));
    G[a].push_back(path.size()-1);
    path.push_back(edge(b,a,0));
    G[b].push_back(path.size()-1);
    
}
ll dfs(ll now,ll minn)
{
    if(vis[now]||minn <= 0 )return 0;
    vis[now]=1;
    if(now == n)return minn;
    ll len = G[now].size();
    
    for(ll i=0;i<len;++i)
    {
        ll pos = G[now][i];
        ll tar = path[pos].b;
        ll mm = min(path[pos].c,minn);
        ll res = dfs(tar,mm);
        if(res)
        {
            path[pos].c -= res;
            path[pos^1].c += res;
            return res;
        }
    }return 0;
}
ll cases=0;

void init()
{
    cases++;
    for(ll i=0;i<=n;++i)G[i].clear();
    path.clear();
    memset(vis,0,sizeof(vis));
    
    for(ll i=0;i<m;++i)
    {
        ll a,b,c;
        cin>>a>>b>>c;
        add_edge(a,b,c);
    }
    
    ll res = dfs(1,INF);
    ll ans = res;
    while(res)
    {
        memset(vis,0,sizeof(vis));
        res = dfs(1,INF);
        years += res;
    }
    cout<<ans<<endl;
    // prllf("Case %d: %d\n",cases,ans);
}


intmain ()
{
    
    cin.sync_with_stdio(false);
    
    ll t=1 ;
    while(cin>>m>>n)init();
    // cin>>t;
    // for(ll i=0;i<t;++i)
    // {
        // cin>>m>>n;
        // init();
    // }
    // while(cin>>n>>m)init();
    
    
    return 0;
}

 

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324889023&siteId=291194637