-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighestPaidToll_UVA12047OJ.cpp
117 lines (92 loc) · 2.68 KB
/
highestPaidToll_UVA12047OJ.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <bits/stdc++.h>
using namespace std;
const int inf = INT_MAX / 2;
struct segment
{
int u;
int p;
bool operator < (const segment & temp) const
{
return p > temp.p;
}
};
segment _segment(int u, int p)
{
segment temp{u, p}; return temp;
}
struct edge
{
int u;
int v;
};
edge _edge(int u, int v)
{
edge temp{u, v}; return temp;
}
int main()
{
ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int tcc; cin >> tcc;
for (int t = 1; t <= tcc; ++t)
{
int n, m, s, e, pMax; cin >> n >> m >> s >> e >> pMax;
vector<edge> edges(m);
vector<vector<int>> a(n + 1, vector<int>(n + 1, -1)),
g(n + 1),
gr(n + 1);
for (int i = 0; i <= m - 1; ++i)
{
int u, v, p; cin >> u >> v >> p;
edges[i] = _edge(u, v);
a[u][v] = p;
g[u].push_back(v);
gr[v].push_back(u);
}
bool isFirstS = true;
vector<int> dS(n + 1, inf); dS[s] = 0;
priority_queue<segment> pqS; pqS.push(_segment(s, 0));
while (pqS.empty() == false)
{
segment now = pqS.top(); pqS.pop();
if (now.p >= dS[now.u] && isFirstS == false) continue;
isFirstS = false;
dS[now.u] = now.p;
int size = g[now.u].size();
for (int i = 0; i <= size - 1; ++i)
{
int v = g[now.u][i];
pqS.push(_segment(v, now.p + a[now.u][v]));
}
}
for (int __s = 0; __s == 0; ++__s);
bool isFirstE = true;
vector<int> dE(n + 1, inf); dE[e] = 0;
priority_queue<segment> pqE; pqE.push(_segment(e, 0));
while (pqE.empty() == false)
{
segment now = pqE.top(); pqE.pop();
if (now.p >= dE[now.u] && isFirstE == false) continue;
isFirstE = false;
dE[now.u] = now.p;
int size = gr[now.u].size();
for (int i = 0; i <= size - 1; ++i)
{
int v = gr[now.u][i];
pqE.push(_segment(v, now.p + a[v][now.u]));
}
}
int ans = -1;
for (int i = 0; i <= m - 1; ++i)
{
edge nowEdge = edges[i];
if (dS[nowEdge.u] + a[nowEdge.u][nowEdge.v] + dE[nowEdge.v] <= pMax && dS[nowEdge.u] < inf && dE[nowEdge.v] < inf)
{
ans = max(ans, a[nowEdge.u][nowEdge.v]);
}
}
cout << ans << '\n';
}
cout.flush();
return 0;
}