백준
백준 소스코드 [C++] 18223 민준이와 마산 그리고 건우
Hani_Levenshtein
2020. 9. 1. 02:00
https://www.acmicpc.net/problem/18223
18223번: 민준이와 마산 그리고 건우
입력의 첫 번째 줄에 정점의 개수 V와 간선의 개수 E, 그리고 건우가 위치한 정점 P가 주어진다. (2 ≤ V ≤ 5,000, 1 ≤ E ≤ 10,000, 1 ≤ P ≤ V) 두 번째 줄부터 E개의 줄에 걸쳐 각 간선의 정보
www.acmicpc.net
백준 소스코드 [C++] 18223 민준이와 마산 그리고 건우
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
using namespace std;
#define MAX 987654321
int v, e, s, d, weight,via;
vector <pair<int, int>> adj[5000];
vector<int> dijkstra(int src) {
priority_queue <pair<int, int> > pq;
vector<int> dist(v, 987654321);
dist[src] = 0;
pq.push({ 0,src });
while (pq.empty() != true) {
int cost = -pq.top().first;
int here = pq.top().second;
pq.pop();
if (dist[here] < cost) continue;
for (int i = 0;i < (int)adj[here].size();i++) {
int there = adj[here][i].first;
int nextdist = cost + adj[here][i].second;
if (dist[there] > nextdist) {
dist[there] = nextdist;
pq.push({ -nextdist,there });
}
}
}
return dist;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> v >> e>>via;
for (int i = 0;i < e;i++) {
cin >> s >> d >> weight;
adj[s - 1].push_back(make_pair(d - 1, weight));
adj[d - 1].push_back(make_pair(s - 1, weight));
}
if (dijkstra(0)[v - 1] == dijkstra(0)[via - 1] + dijkstra(via-1)[v - 1])
cout << "SAVE HIM";
else cout << "GOOD BYE";
return 0;
}