다익스트라 문제다.


시작 도시는 추가비용이 0

1번째 도시는 t

2번째 도시는 2t

...

이렇게 증가한다.


결국 n개의 도시가 있다면


t + 2t + 3t + ... + (n-1)*t의 추가비용이 든다.


그리고 도시를 점령하는 순간 현재 뻗을 수 있는 모든 도로에 대해 비용이 증가되는 것이므로


미리 다 더해놓고 다익스트라를 돌리는 것과 같다.


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
#include <stdio.h>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
 
typedef pair<intint> pii;
 
int main() {
    int n, m, t, ans, d[10001], u, v, w;
    vector<pii> adj[10001];
    memset(d, -1sizeof(d));
    scanf("%d%d%d"&n, &m, &t);
    while (m--) {
        scanf("%d%d%d"&u, &v, &w);
        adj[u].push_back({ v,w });
        adj[v].push_back({ u,w });
    }
    priority_queue<pii> q;
    q.push({ 0,1 });
    ans = t*(n - 2)*(n - 1/ 2;
    while (q.size()) {
        int c = -q.top().first;
        int x = q.top().second;
        q.pop();
        if (d[x] != -1continue;
        d[x] = c;
        ans += c;
        for (auto i : adj[x]) {
            if (d[i.first] != -1continue;
            q.push({ -i.second, i.first });
        }
    }
    printf("%d", ans);
    return 0;
}
cs


'BOJ' 카테고리의 다른 글

15312 이름 궁합  (0) 2018.02.17
1371 가장 많은 글자  (0) 2018.02.17
14949 외계 미생물  (0) 2018.02.17
2855 흥미로운 수열  (0) 2017.12.14
14948 군대탈출하기  (0) 2017.12.14

+ Recent posts