백준(BOJ) 1238 : 파티
BOJ
2020. 6. 8. 17:53
https://www.acmicpc.net/problem/1238
1238번: 파티
문제 N개의 숫자로 구분된 각각의 마을에 한 명의 학생이 살고 있다. 어느 날 이 N명의 학생이 X (1 ≤ X ≤ N)번 마을에 모여서 파티를 벌이기로 했다. 이 마을 사이에는 총 M개의 단방향 도로들이
www.acmicpc.net
단방향 그래프가 주어졌을 때 도착점까지 왔다가 돌아올 수 있는 최단 거리중 최댓값을 구하는 문제.
다익스트라 알고리즘을 이용하여 각 정점들에 대해 도착점 -> 정점들의 거리를 일단 구해준다.
그 후에 각 정점들에 대해 다익스트라 알고리즘을 사용하여 정점 -> 도착점까지의 거리를 구한 뒤
앞에서 구한 도착점 -> 정점 까지의 거리를 더해준 것들 중 최댓값을 구해주면 된다.
아래는 소스코드이다.
더보기
#include <bits/stdc++.h>
#define endl '\n'
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define cont continue
#define rep(i, n) for(int i = 0 ; i < (n) ; i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll INF = 987654321;
int T;
vector< vector<pair<int, int> > > adj(1001);
int dist[1001] = {};
int dist2[1001] = {};
void foo(int start) {
priority_queue< pair<int, int> > pq;
pq.push(mp(start, 0));
dist[start] = 0;
while(!pq.empty()) {
int here = pq.top().first;
int DIST = -pq.top().second;
pq.pop();
if(DIST > dist[here]) continue;
for(int i = 0 ; i < adj[here].size() ; i++) {
int nxt = adj[here][i].first;
int cost = adj[here][i].second;
if(dist[nxt] > dist[here] + cost) {
dist[nxt] = dist[here] + cost;
pq.push(mp(nxt, -(dist[here] + cost)));
}
}
}
}
int main() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
int N, M, X;
cin >> N >> M >> X;
fill(dist, dist + 1001, INF);
fill(dist2, dist2 + 1001, INF);
for(int i = 0 ; i < M ; i++) {
int a, b, c;
cin >> a >> b >> c;
adj[a].pb(mp(b, c));
}
foo(X);
for(int i = 1 ; i <= N ; i++) {
dist2[i] = dist[i];
}
int ans = 0;
for(int i = 1 ; i <= N ; i++) {
fill(dist, dist + 1001, INF);
foo(i);
ans = max(dist2[i] + dist[X], ans);
}
cout << ans << endl;
}
'BOJ' 카테고리의 다른 글
백준(BOJ) 9660 : 돌 게임 6 (0) | 2020.06.18 |
---|---|
백준(BOJ) 1005 : ACM Craft (0) | 2020.06.16 |
백준(BOJ) 1799 : 비숍 (0) | 2020.06.16 |
백준(BOJ) 1766 : 문제집 (0) | 2020.06.16 |
백준(BOJ) 5397 : 키로거 (0) | 2020.06.06 |