-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_1238.cpp
More file actions
73 lines (60 loc) · 1.31 KB
/
BOJ_1238.cpp
File metadata and controls
73 lines (60 loc) · 1.31 KB
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
// 21/11/09
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int n, m, x;
vector<pair<int, int>> edge[1001];
vector<vector<int>> cost;
void input()
{
cin >> n >> m >> x;
cost.assign(n+1, vector<int>(n+1, 987654321));
while (m--)
{
int a, b, c;
cin>>a>>b>>c;
edge[a].push_back({b, c});
}
}
int howCost(int node, int target)
{
priority_queue<pair<int, int>> pq;
pq.push({0, node});
while (!pq.empty())
{
int now_cost = -pq.top().first;
int now_node = pq.top().second;
pq.pop();
for (int i=0; i<(int)edge[now_node].size(); i++)
{
int new_cost = now_cost+edge[now_node][i].second;
int new_node = edge[now_node][i].first;
if (new_cost < cost[node][new_node])
{
cost[node][new_node] = new_cost;
pq.push({-new_cost, new_node});
}
}
}
return cost[node][target];
}
int whoMost()
{
priority_queue<int> answer_list;
for (int i=1; i<=n; i++)
{
if (i==x)
continue;
answer_list.push(howCost(x, i) + howCost(i, x));
}
return answer_list.top();
}
int main(void)
{
ios_base::sync_with_stdio(0);
cin.tie(0);
input();
cout << whoMost();
return 0;
}