ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 사탕 줍는 로봇 - 15892
    Algorithm/BOJ 2020. 7. 6. 15:53
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    //15892 - 사탕 줍는 로봇
     
    #include <iostream>
    #include <vector>
    #include <queue>
    using namespace std;
     
    int N, M;
    typedef struct edge {
        int dest;
        int capa;
        struct edge* rev;
     
        edge(int d, int c) : dest(d), capa(c) {}
    }Edge;
     
    vector<vector<Edge*>> adj;
    int work[301];
    int level[301];
     
    void init() {
        cin >> N >> M;
        adj = vector<vector<Edge*>>(N+1);
        int x, y, c;
        for(int i = 0; i<M; ++i) {
            cin >> x >> y >> c;
            Edge* u = new Edge(y, c);
            Edge* v = new Edge(x, c);
            u->rev = v;
            v->rev = u;
            adj[x].push_back(u);
            adj[y].push_back(v);
        }
    }
     
    bool bfs() {
        fill(level, level+N+1-1);
        queue<int> Q;
        Q.push(1);
        level[1= 0;
     
        while(!Q.empty()) {
            int here = Q.front();
            Q.pop();
     
            for(int i = 0; i<adj[here].size(); ++i) {
                Edge* e = adj[here][i];
                int there = e->dest;
                int cap = e->capa;
     
                if(level[there] == -1 && cap > 0) {
                    level[there] = level[here]+1;
                    Q.push(there);
                }
            }
        }
        return level[N] != -1;
    }
     
    int dfs(int here, int fl) {
     
        if(here == N) return fl;
     
        for(int& i = work[here]; i<adj[here].size(); i++) {
            Edge* e = adj[here][i];
            Edge* re = e->rev;
            int there = e->dest;
            int cap = e->capa;
     
            if(level[there] == level[here] + 1 && cap > 0) {
                int df = dfs(there, min(fl, cap));
     
                if(df > 0) {
                    e->capa -= df;
                    re->capa += df;
                    return df;
                }
            }
        }
        return 0;
    }
     
    int main() {
     
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        init();
        int ans = 0;
        while(bfs()) {
            
            fill(work, work+N+10);
     
            while(1) {
                int df = dfs(111111);
                if(!df) break;
     
                ans += df;
            }
        }
        cout << ans;    
    }
    cs

     

     

     

     

     

    colorscripter.com/info#e"

     

    정보 - Color Scripter

    Simple & Flexible Syntax HighLighter

    colorscripter.com

     

     

     

     

     

    사탕 줍는 로봇

    최대 유량을 구하는 문제입니다. 디닉알고리즘을 사용해서 해결했습니다!

     

     

     

     

    'Algorithm > BOJ' 카테고리의 다른 글

    369 - 17614  (0) 2020.07.19
    비트베리 - 17374  (0) 2020.07.13
    세 용액 - 2473  (0) 2020.04.16
    소가 길을 건너간 이유 6 - 14466  (0) 2020.04.01
    네트워크 연결 - 3780  (0) 2020.04.01

    댓글

Designed by Tistory.