티스토리 뷰

알고리즘/BOJ

[백준] 10678 Meeting Time

세진짱 2018. 6. 29. 19:06


Bessie와 Elsie가 1에서 출발해 N에서 만나려고한다

간선은 M개가 주어진다(간선은 낮은숫자 ->높으숫자만 가능)

근데 둘이 간선을 지나갈 때의 시간이 다르다

이 때 M에서 만날 수 있는 최소의 시간을 구하는 문제다

N이 16밖에 안되기 때문에 bfs로 모든 경로를 다 구했다

그리고 N까지 가는 시간 중 가장 작은수를 출력하면 답이다!


소스코드

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
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
using namespace std;
int n, m;
void bfs(vector<vector<pair<intint>>>& Graph, set<int>&S) {
    queue < pair<intint>> q;
    q.push({ 0,0 });
    while (!q.empty()) {
        int here = q.front().first;
        int cost = q.front().second;
        q.pop();
 
        for (int i = 0; i < Graph[here].size(); i++) {
            int next = Graph[here][i].first;
            int ncost = Graph[here][i].second;
            if (next == n - 1) S.insert(cost + ncost);
            q.push({ next,cost+ncost });
        }
    }
}
int main() {
    scanf(" %d %d"&n, &m);
    vector < vector<pair<intint>>> aGraph(n), bGraph(n);
    for (int i = 0; i < m; i++) {
        int u, v, a, b; scanf(" %d %d %d %d"&u, &v, &a, &b);
        u--; v--;
        aGraph[u].push_back({ v,a });
        bGraph[u].push_back({ v,b });
    }
    set<int> A, B;
    bfs(aGraph, A);
    bfs(bGraph, B);
    set<int>::iterator it;
    for (it = A.begin(); it != A.end(); it++) {
        if (B.count(*it)) {
            printf("%d\n"*it); return 0;
        }
    }
    puts("IMPOSSIBLE");
}
cs

'알고리즘 > BOJ' 카테고리의 다른 글

[백준] 2411 아이템먹기  (0) 2018.07.01
[백준] 10747 Censoring  (0) 2018.06.30
[백준] 10677 It's All About the Base  (0) 2018.06.29
[백준] 10748 Cow Hopscotch  (0) 2018.06.29
[백준] 10750 Censoring  (0) 2018.06.28
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/02   »
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
글 보관함