처음 도전해본 MCMF문제.


편도 최소시간은 다익스트라로 하면 되지만 왕복 최소시간은 다익스트라*2가 통하지 않는 경우가 있다.


MCMF를 설명하기에는 아직 아는게 너무 부족하다. 연습 숙련이 답인듯.


<코드>

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
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef pair<intint> pii;
 
struct E {
    int t, c, co;
    E *rev;
    E(int t, int c, int co) : t(t), c(c), co(co) {}
};
 
vector<vector<*>> g;
 
void aE(int u, int v, int c, int co) {
    E *ori = new E(v, c, co);
    E *rev = new E(u, 0-co);
    ori->rev = rev;
    rev->rev = ori;
    g[u].push_back(ori);
    g[v].push_back(rev);
}
 
int n, m, s, k;
int d[1003], INF = 2e9;
pii f[1003];
bool ch[1003];
 
int main() {
    scanf("%d%d"&n, &m);
    k = n + 1;
    g.resize(n + 2);
    while (m--) {
        int a, b, c;
        scanf("%d%d%d"&a, &b, &c);
        aE(a, b, 1, c);
        aE(b, a, 1, c);
    }
    aE(s, 120);
    aE(n, k, 20);
    int res = 0;
    while (1) {
        memset(f, -1sizeof(f));
        memset(ch, falsesizeof(ch));
        fill(d, d + n + 2, INF);
        queue<int> q;
        d[s] = 0;
        q.push(s);
        while (q.size()) {
            int x = q.front();
            q.pop();
            ch[x] = false;
            for (int i = 0; i < g[x].size(); i++) {
                auto nx = g[x][i];
                if (nx->> 0 && d[nx->t] > d[x] + nx->co) {
                    d[nx->t] = d[x] + nx->co;
                    f[nx->t] = { x,i };
                    if (!ch[nx->t]) {
                        ch[nx->t] = 1;
                        q.push(nx->t);
                    }
                }
            }
        }
        if (f[k].first == -1break;
        int x = k;
        for (; x != s; x = f[x].first) {
            res += g[f[x].first][f[x].second]->co;
            g[f[x].first][f[x].second]->-= 1;
            g[f[x].first][f[x].second]->rev->+= 1;
        }
    }
    printf("%d\n", res);
 
    return 0;
}
cs


'BOJ' 카테고리의 다른 글

3056 007  (0) 2017.09.06
1733 등번호  (0) 2017.09.06
5588 별자리 찾기  (0) 2017.09.06
3621 족보  (0) 2017.09.06
3780 네트워크 연결  (0) 2017.09.06

+ Recent posts