Skip to content

nar789/networkflow

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

1 Commit
ย 
ย 
ย 
ย 

Repository files navigation

Networkflow Algorithm

this is networkflow algorithm.

#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
 
using namespace std;
 
const int INF =(1<<30);
const int MAX_V = 100;
int V;
 
//capacity[u][v]=u์—์„œ v๋กœ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋Š” ์šฉ๋Ÿ‰
//flow[u][v]=u์—์„œ v๋กœ ํ˜๋Ÿฌ๊ฐ€๋Š” ์œ ๋Ÿ‰ (๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์ธ ๊ฒฝ์šฐ ์Œ์ˆ˜)
int capacity[MAX_V][MAX_V], flow[MAX_V][MAX_V];
 
//flow[][]๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ  ์ด ์œ ๋Ÿ‰์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
int networkflow(int source, int sink){
    //flow๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™” ํ•œ๋‹ค.
    memset(flow, 0, sizeof(flow));
    int totalFlow=0;
    while(true){
        //๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰์œผ๋กœ ์ฆ๊ฐ€ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š”๋‹ค.
        vector<int> parent(MAX_V, -1);
        queue<int>q;
        parent[source]=source;
        q.push(source);
        while(!q.empty() && parent[sink]==-1){
            int here=q.front();
            q.pop();
            for(int there=0; there<V; ++there)
                if(capacity[here][there] - flow[here][there] > 0 &&
                    parent[there]==-1){
                        q.push(there);
                        parent[there]=here;
                }
        }
        //์ฆ๊ฐ€ ๊ฒฝ๋กœ๊ฐ€ ์—†์œผ๋ฉด ์ข…๋ฃŒํ•œ๋‹ค.
        if(parent[sink]==-1) break;
        //์ฆ๊ฐ€ ๊ฒฝ๋กœ๋ฅผ ํ†ตํ•ด ์œ ๋Ÿ‰์„ ์–ผ๋งˆ๋‚˜ ๋ณด๋‚ผ์ง€ ๊ฒฐ์ •ํ•œ๋‹ค.
        int amount = INF;
        for(int p=sink; p!=source; p=parent[p])
            amount=min(capacity[parent[p]][p] - flow[parent[p]][p], amount);
 
        //์ฆ๊ฐ€ ๊ฒฝ๋กœ๋ฅผ ํ†ตํ•ด ์œ ๋Ÿ‰์„ ๋ณด๋‚ธ๋‹ค.
        for(int p=sink; p!=source; p=parent[p]){
            flow[parent[p]][p]+=amount;
            flow[p][parent[p]]-=amount;
        }
 
        totalFlow+=amount;
    }
 
    return totalFlow;
}

About

networkflow algorithm

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages