-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouteChange_UVA11833.cpp
78 lines (62 loc) · 1.71 KB
/
routeChange_UVA11833.cpp
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
#include <bits/stdc++.h>
using namespace std;
ifstream fin("routeChange_UVA11833.in");
ofstream fout("routeChange_UVA11833.out");
const int inf = INT_MAX / 3;
struct segment
{
int u;
int t;
bool operator < (const segment & temp) const
{
return t > temp.t;
}
};
segment _segment(int u, int t)
{
segment temp{u, t}; return temp;
}
int main()
{
while (true)
{
int n = 0, m = 0, cc = 0, kk = 0; fin >> n >> m >> cc >> kk;
if (n + m + cc + kk == 0) break;
vector<vector<int>> a(n, vector<int>(n, inf)),
g(n);
for (int c = 1; c <= m; ++c)
{
int u, v, t; fin >> u >> v >> t;
a[u][v] = t; a[v][u] = a[u][v];
g[u].push_back(v); g[v].push_back(u);
}
int temp = 0;
vector<int> d(cc, 0);
for (int u = cc - 2; u >= 0; --u)
{
temp += a[u][u + 1];
d[u] = temp;
}
vector<int> dp(n, inf);
priority_queue<segment> pq; pq.push(_segment(kk, 0));
while (pq.empty() == false)
{
segment now = pq.top(); pq.pop();
if (now.t >= dp[now.u]) continue;
dp[now.u] = now.t;
if (now.u <= cc - 1)
{
dp[cc - 1] = min(dp[cc - 1], now.t + d[now.u]);
continue;
}
int size = g[now.u].size();
for (int i = 0; i <= size - 1; ++i)
{
int v = g[now.u][i];
pq.push(_segment(v, now.t + a[now.u][v]));
}
}
fout << dp[cc - 1] << '\n';
}
return 0;
}