백준 16118번 달빛 여우
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 | #include <iostream> #include <vector> #include <queue> #include <algorithm> #include <cstring> //memset using namespace std; const int MAX = 4000 + 2; typedef pair<int, int> pii; typedef pair<long long, int> pli; typedef long long ll; const ll INF = 4000LL * 100000 * 10 + 1; int N, M; ll dist[MAX]; ll Foxdist[MAX][2]; vector<pli > adj[MAX]; bool visited[MAX]; void dijkstra() { fill(dist, dist + MAX, INF); priority_queue<pli, vector<pli>, greater<pli> > pq; pq.push({ 0,1 }); dist[1] = 0; while (!pq.empty()) { ll cost = pq.top().first; int here = pq.top().second; pq.pop(); if (dist[here] != cost) continue; for (auto &p : adj[here]) { int there = p.second; ll new_cost = p.first + cost; if (dist[there] > new_cost) { dist[there] = new_cost; pq.push({ new_cost,there }); } } } } void dijkstra2() { fill(&Foxdist[0][0], &Foxdist[MAX - 1][1] + 1, INF); Foxdist[1][0] = 0; priority_queue<pair<pli, int >, vector<pair< pli, int> >, greater<pair<pli, int > > > pq; pq.push({ { 0,1 }, 0 }); while (!pq.empty()) { ll cost = pq.top().first.first; int here = pq.top().first.second; bool mult = pq.top().second; pq.pop(); if (cost > Foxdist[here][mult]) continue; if (mult) { for (auto &there : adj[here]) { ll newCost = cost + there.first * 2; if (Foxdist[there.second][0] > newCost) { pq.push({ {newCost, there.second},0 }); Foxdist[there.second][0] = newCost; } } } else { for (auto &there : adj[here]) { ll newCost = cost + there.first / 2; if (Foxdist[there.second][1] > newCost) { pq.push({ {newCost, there.second},1 }); Foxdist[there.second][1] = newCost; } } } } } int main(void) { ios_base::sync_with_stdio(0); cin.tie(0); cin >> N >> M; for (int i = 0; i < M; i++) { int a, b; ll d; cin >> a >> b >> d; adj[a].push_back({ d*2,b }); adj[b].push_back({ d*2,a }); } dijkstra(); dijkstra2(); int cnt = 0; for (int i = 2; i <= N; i++) { if (dist[i] < min(Foxdist[i][0], Foxdist[i][1])) cnt++; } cout << cnt << endl; return 0; } | cs |
출처:https://www.acmicpc.net/problem/16118
INF값을 long long으로 잡지 않고 int로 해서 런타임 에러가 났었네요.
cost가 long long까지 가는 것을 주의해주어야 합니다.
그리고 멍청하게 계속 우선순위 큐를 쓰는데 cost값을 앞으로 보내주지 않고 풀었네요 ㅋㅋㅋ
정말 주의해야겠습니다.
'알고리즘 > BAEKJOON' 카테고리의 다른 글
| 백준 2042번 구간 합 구하기 (0) | 2019.05.22 |
|---|---|
| 백준 15357번 Portal (0) | 2019.05.18 |
| 백준 9466번 텀 프로젝트 (0) | 2019.05.18 |
| 백준 5719번 거의 최단 경로 (0) | 2019.05.16 |
| 백준 16211번 백채원 (0) | 2019.05.15 |