#include <bits/stdc++.h>
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
#pragma GCC optimize("Ofast,unroll-loops")
using namespace std;
#define endl '\n'
#define ll long long
#define all(x) x.begin(), x.end()
map<int, vector<tuple<int, int, int>>> edg[100001];
unordered_map<int, ll> sum[100001], dp2[100001];
ll dp[100001];
int n, m;
void solve() {
priority_queue<tuple<ll, int, int>> pq;
pq.push({0, 1, 0});
while (pq.size() > 0) {
ll dist;
int place, color;
tie(dist, place, color) = pq.top();
dist *= -1;
pq.pop();
if (color) {
if (dist != dp2[place][color]) continue;
for (auto [to, color, cost] : edg[place][color]) {
ll newCost = dist + sum[place][color] - cost;
if (newCost < dp[to]) {
dp[to] = newCost;
pq.push({-dp[to], to, 0});
}
}
}
else {
if (dist != dp[place]) continue;
for (int i = 0; i < edg[place].size(); i++) {
for (auto [to, color, cost] : edg[place][i]) {
ll newCost = dist + min((ll) cost, sum[place][color] - (ll) cost);
if (newCost < dp[to]) {
dp[to] = newCost;
pq.push({-dp[to], to, 0});
}
if (!dp2[to].count(color) || dist < dp2[to][color]) {
dp2[to][color] = dist;
pq.push({-dp2[to][color], to, color});
}
}
}
}
}
cout << ((dp[n] == 1e18) ? -1 : dp[n]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 2; i <= n; i++) dp[i] = 1e18;
for (int i = 0; i < m; i++) {
int from, to, color, cost;
cin >> from >> to >> color >> cost;
edg[from][color].push_back({to, color, cost});
edg[to][color].push_back({from, color, cost});
sum[from][color] += cost;
sum[to][color] += cost;
}
solve();
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:43:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::map<int, std::vector<std::tuple<int, int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for (int i = 0; i < edg[place].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
15956 KB |
Output is correct |
2 |
Correct |
10 ms |
15860 KB |
Output is correct |
3 |
Correct |
10 ms |
16088 KB |
Output is correct |
4 |
Correct |
18 ms |
15928 KB |
Output is correct |
5 |
Correct |
14 ms |
16372 KB |
Output is correct |
6 |
Correct |
9 ms |
15992 KB |
Output is correct |
7 |
Correct |
19 ms |
20300 KB |
Output is correct |
8 |
Correct |
11 ms |
17492 KB |
Output is correct |
9 |
Correct |
130 ms |
93468 KB |
Output is correct |
10 |
Correct |
143 ms |
103396 KB |
Output is correct |
11 |
Correct |
256 ms |
160504 KB |
Output is correct |
12 |
Correct |
40 ms |
28228 KB |
Output is correct |
13 |
Correct |
72 ms |
56128 KB |
Output is correct |
14 |
Correct |
107 ms |
75652 KB |
Output is correct |
15 |
Correct |
41 ms |
34256 KB |
Output is correct |
16 |
Correct |
119 ms |
79220 KB |
Output is correct |
17 |
Correct |
91 ms |
69228 KB |
Output is correct |
18 |
Correct |
9 ms |
16692 KB |
Output is correct |
19 |
Correct |
34 ms |
31616 KB |
Output is correct |
20 |
Correct |
173 ms |
101820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3124 ms |
2008292 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
15956 KB |
Output is correct |
2 |
Correct |
10 ms |
15860 KB |
Output is correct |
3 |
Correct |
10 ms |
16088 KB |
Output is correct |
4 |
Correct |
18 ms |
15928 KB |
Output is correct |
5 |
Correct |
14 ms |
16372 KB |
Output is correct |
6 |
Correct |
9 ms |
15992 KB |
Output is correct |
7 |
Correct |
19 ms |
20300 KB |
Output is correct |
8 |
Correct |
11 ms |
17492 KB |
Output is correct |
9 |
Correct |
130 ms |
93468 KB |
Output is correct |
10 |
Correct |
143 ms |
103396 KB |
Output is correct |
11 |
Correct |
256 ms |
160504 KB |
Output is correct |
12 |
Correct |
40 ms |
28228 KB |
Output is correct |
13 |
Correct |
72 ms |
56128 KB |
Output is correct |
14 |
Correct |
107 ms |
75652 KB |
Output is correct |
15 |
Correct |
41 ms |
34256 KB |
Output is correct |
16 |
Correct |
119 ms |
79220 KB |
Output is correct |
17 |
Correct |
91 ms |
69228 KB |
Output is correct |
18 |
Correct |
9 ms |
16692 KB |
Output is correct |
19 |
Correct |
34 ms |
31616 KB |
Output is correct |
20 |
Correct |
173 ms |
101820 KB |
Output is correct |
21 |
Execution timed out |
3124 ms |
2008292 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |