티스토리 뷰
https://www.acmicpc.net/problem/18223
백준 소스코드 [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;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 1865 웜홀 (0) | 2020.09.01 |
---|---|
백준 소스코드 [C++] 1261 알고스팟 (0) | 2020.09.01 |
백준 소스코드 [C++] 4485 녹색 옷 입은 애가 젤다지 (0) | 2020.09.01 |
백준 소스코드 [C++] 11779 최소비용 구하기 2 (0) | 2020.09.01 |
백준 소스코드 [C++] 2665 미로만들기 (0) | 2020.09.01 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 최단경로문제
- 벨만포드 시간복잡도
- 벨만포드 알고리즘
- 부스트캠프 6기
- 코딩대회
- WWDC17
- WWDC16
- 컴퓨터 추상화
- 네트워크 플로우
- 최단경로 알고리즘
- 최단경로 문제
- 네트워크 유량
- CPU와 Memory
- rxswift
- mach-o
- Testable
- 에드몬드 카프 알고리즘
- 강한 순환 참조
- 최대 매칭
- WWDC21
- 다익스트라 시간복잡도
- HIG
- observeOn
- IOS
- CompositionalLayout
- MeTal
- State Restoration
- 포드 풀커슨 알고리즘
- test coverage
- WWDC19
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함