#include <bits/stdc++.h>
#define int long long
using namespace std;
vector<array<int, 4>> adj[200001];
vector<vector<array<int, 3>>> adj2[200001];
vector<int> sum[200001];
int DP[200001];
vector<int> DP2[200001];
int cnt[200001];
map<int, int> hm[200001];
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
//freopen("TEST.IN", "r", stdin);
//freopen("TEST.OUT", "w", stdout);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++)
{
int u, v, c, w;
cin >> u >> v >> c >> w;
if (!hm[u].count(c)) hm[u][c] = cnt[u]++;
if (!hm[v].count(c)) hm[v][c] = cnt[v]++;
adj[u].push_back({v, hm[u][c], w, hm[v][c]});
adj[v].push_back({u, hm[v][c], w, hm[u][c]});
}
for (int i = 1; i <= n; i++)
{
DP2[i].assign(cnt[i], (int)1e18);
adj2[i].resize(cnt[i]);
sum[i].assign(cnt[i], 0);
for (const auto& [v, c, w, vc] : adj[i])
{
adj2[i][c].push_back({v, w, vc});
sum[i][c] += w;
}
}
memset(DP, 0x3f, sizeof DP);
DP[1] = 0;
priority_queue<array<int, 3>, vector<array<int, 3>>, greater<>> q;
q.push({DP[1], 1, -1});
while (!q.empty())
{
auto [d, u, pc] = q.top();
q.pop();
if (DP[u] == d && pc == -1)
{
for (int i = 0; i < cnt[u]; i++)
{
int s = sum[u][i];
for (const auto& [v, w, vc] : adj2[u][i])
{
if (DP[v] > DP[u] + min(w, s - w))
{
DP[v] = DP[u] + min(w, s - w);
q.push({DP[v], v, -1});
}
if (DP2[v][vc] > DP[u])
{
DP2[v][vc] = DP[u];
q.push({DP2[v][vc], v, vc});
}
}
}
}
else if (DP2[u][pc] == d && pc != -1)
{
int s = sum[u][pc];
for (const auto& [v, w, vc] : adj2[u][pc])
{
if (DP[v] > DP2[u][pc] + s - w)
{
DP[v] = DP2[u][pc] + s - w;
q.push({DP[v], v, -1});
}
}
}
}
if (DP[n] > (int)1e18) cout << -1;
else cout << DP[n];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
31580 KB |
Output is correct |
2 |
Correct |
7 ms |
31580 KB |
Output is correct |
3 |
Correct |
7 ms |
31580 KB |
Output is correct |
4 |
Correct |
7 ms |
31580 KB |
Output is correct |
5 |
Correct |
7 ms |
31580 KB |
Output is correct |
6 |
Correct |
7 ms |
31576 KB |
Output is correct |
7 |
Correct |
7 ms |
31836 KB |
Output is correct |
8 |
Correct |
7 ms |
31832 KB |
Output is correct |
9 |
Correct |
11 ms |
32348 KB |
Output is correct |
10 |
Correct |
9 ms |
32348 KB |
Output is correct |
11 |
Correct |
8 ms |
32224 KB |
Output is correct |
12 |
Correct |
9 ms |
32092 KB |
Output is correct |
13 |
Correct |
9 ms |
32348 KB |
Output is correct |
14 |
Correct |
9 ms |
32348 KB |
Output is correct |
15 |
Correct |
8 ms |
31836 KB |
Output is correct |
16 |
Correct |
9 ms |
32112 KB |
Output is correct |
17 |
Correct |
9 ms |
32092 KB |
Output is correct |
18 |
Correct |
8 ms |
31780 KB |
Output is correct |
19 |
Correct |
8 ms |
32092 KB |
Output is correct |
20 |
Correct |
10 ms |
32092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
53320 KB |
Output is correct |
2 |
Correct |
56 ms |
41804 KB |
Output is correct |
3 |
Correct |
111 ms |
58840 KB |
Output is correct |
4 |
Correct |
71 ms |
46156 KB |
Output is correct |
5 |
Correct |
451 ms |
95408 KB |
Output is correct |
6 |
Correct |
470 ms |
89344 KB |
Output is correct |
7 |
Correct |
243 ms |
85024 KB |
Output is correct |
8 |
Correct |
334 ms |
78560 KB |
Output is correct |
9 |
Correct |
309 ms |
78572 KB |
Output is correct |
10 |
Correct |
245 ms |
68692 KB |
Output is correct |
11 |
Correct |
115 ms |
59988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
31580 KB |
Output is correct |
2 |
Correct |
7 ms |
31580 KB |
Output is correct |
3 |
Correct |
7 ms |
31580 KB |
Output is correct |
4 |
Correct |
7 ms |
31580 KB |
Output is correct |
5 |
Correct |
7 ms |
31580 KB |
Output is correct |
6 |
Correct |
7 ms |
31576 KB |
Output is correct |
7 |
Correct |
7 ms |
31836 KB |
Output is correct |
8 |
Correct |
7 ms |
31832 KB |
Output is correct |
9 |
Correct |
11 ms |
32348 KB |
Output is correct |
10 |
Correct |
9 ms |
32348 KB |
Output is correct |
11 |
Correct |
8 ms |
32224 KB |
Output is correct |
12 |
Correct |
9 ms |
32092 KB |
Output is correct |
13 |
Correct |
9 ms |
32348 KB |
Output is correct |
14 |
Correct |
9 ms |
32348 KB |
Output is correct |
15 |
Correct |
8 ms |
31836 KB |
Output is correct |
16 |
Correct |
9 ms |
32112 KB |
Output is correct |
17 |
Correct |
9 ms |
32092 KB |
Output is correct |
18 |
Correct |
8 ms |
31780 KB |
Output is correct |
19 |
Correct |
8 ms |
32092 KB |
Output is correct |
20 |
Correct |
10 ms |
32092 KB |
Output is correct |
21 |
Correct |
109 ms |
53320 KB |
Output is correct |
22 |
Correct |
56 ms |
41804 KB |
Output is correct |
23 |
Correct |
111 ms |
58840 KB |
Output is correct |
24 |
Correct |
71 ms |
46156 KB |
Output is correct |
25 |
Correct |
451 ms |
95408 KB |
Output is correct |
26 |
Correct |
470 ms |
89344 KB |
Output is correct |
27 |
Correct |
243 ms |
85024 KB |
Output is correct |
28 |
Correct |
334 ms |
78560 KB |
Output is correct |
29 |
Correct |
309 ms |
78572 KB |
Output is correct |
30 |
Correct |
245 ms |
68692 KB |
Output is correct |
31 |
Correct |
115 ms |
59988 KB |
Output is correct |
32 |
Correct |
82 ms |
58956 KB |
Output is correct |
33 |
Correct |
133 ms |
59400 KB |
Output is correct |
34 |
Correct |
247 ms |
73168 KB |
Output is correct |
35 |
Correct |
182 ms |
65736 KB |
Output is correct |
36 |
Correct |
207 ms |
69688 KB |
Output is correct |
37 |
Correct |
237 ms |
82452 KB |
Output is correct |
38 |
Correct |
246 ms |
95868 KB |
Output is correct |
39 |
Correct |
108 ms |
68760 KB |
Output is correct |
40 |
Correct |
316 ms |
84040 KB |
Output is correct |
41 |
Correct |
339 ms |
84324 KB |
Output is correct |
42 |
Correct |
391 ms |
87232 KB |
Output is correct |
43 |
Correct |
118 ms |
58948 KB |
Output is correct |
44 |
Correct |
191 ms |
77640 KB |
Output is correct |
45 |
Correct |
265 ms |
74580 KB |
Output is correct |
46 |
Correct |
274 ms |
73040 KB |
Output is correct |
47 |
Correct |
224 ms |
75520 KB |
Output is correct |
48 |
Correct |
506 ms |
101288 KB |
Output is correct |