-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8dd8140
commit 71aba47
Showing
6 changed files
with
318 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
ifstream fin("highestPaidToll_UVA12047.in"); | ||
ofstream fout("highestPaidToll_UVA12047.out"); | ||
|
||
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() | ||
{ | ||
int tcc; fin >> tcc; | ||
for (int t = 1; t <= tcc; ++t) | ||
{ | ||
int n, m, s, e, pMax; fin >> 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; fin >> 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) | ||
{ | ||
ans = max(ans, a[nowEdge.u][nowEdge.v]); | ||
} | ||
} | ||
|
||
fout << ans << '\n'; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
1 | ||
2 3 1 2 14 | ||
1 2 4 | ||
1 2 1 | ||
2 1 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) | ||
{ | ||
ans = max(ans, a[nowEdge.u][nowEdge.v]); | ||
} | ||
} | ||
|
||
cout << ans << '\n'; | ||
} | ||
|
||
cout.flush(); | ||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
ifstream fin("highestPaidToll_UVA12047.in"); | ||
ofstream fout("highestPaidToll_UVA12047.out"); | ||
|
||
const int inf = INT_MAX / 2; | ||
|
||
struct segment | ||
{ | ||
int u; | ||
int curPMax; | ||
int curPTotal; | ||
|
||
bool operator < (const segment & temp) const | ||
{ | ||
if (curPMax != temp.curPMax) return curPMax < temp.curPMax; | ||
else return curPTotal > temp.curPTotal; | ||
} | ||
}; | ||
segment _segment(int u, int curPMax, int curPTotal) | ||
{ | ||
segment temp{u, curPMax, curPTotal}; return temp; | ||
} | ||
|
||
int main() | ||
{ | ||
int tcc; fin >> tcc; | ||
for (int t = 1; t <= tcc; ++t) | ||
{ | ||
int n, m, s, e, pMax; fin >> n >> m >> s >> e >> pMax; | ||
|
||
vector<vector<int>> a(n + 1, vector<int>(n + 1, -1)), | ||
g(n + 1); | ||
for (int c = 1; c <= m; ++c) | ||
{ | ||
int u, v, p; fin >> u >> v >> p; | ||
a[u][v] = p; | ||
g[u].push_back(v); | ||
} | ||
|
||
if (t == 9) | ||
{ | ||
for (int __s = 0; __s == 0; ++__s); | ||
} | ||
|
||
bool isFirst = true; | ||
int ans = -1; | ||
vector<vector<int>> dp(n + 1, vector<int>(pMax + 1, -1)); dp[s][0] = 0; | ||
priority_queue<segment> pq; pq.push(_segment(s, 0, 0)); | ||
while (pq.empty() == false) | ||
{ | ||
segment now = pq.top(); pq.pop(); | ||
|
||
if (now.curPMax <= dp[now.u][now.curPTotal] && isFirst == false) continue; | ||
isFirst = false; | ||
dp[now.u][now.curPTotal] = now.curPMax; | ||
|
||
if (now.u == e) ans = max(ans, now.curPMax); | ||
|
||
int size = g[now.u].size(); | ||
for (int i = 0; i <= size - 1; ++i) | ||
{ | ||
int v = g[now.u][i]; | ||
|
||
if (now.curPTotal + a[now.u][v] <= pMax) | ||
{ | ||
pq.push(_segment(v, max(now.curPMax, a[now.u][v]), now.curPTotal + a[now.u][v])); | ||
} | ||
} | ||
} | ||
|
||
fout << ans << '\n'; | ||
} | ||
|
||
return 0; | ||
} |