#include <bits/stdc++.h>
#define int long long
using namespace std;
vector<array<int, 4>> adj[100001];
vector<vector<array<int, 3>>> adj2[100001];
vector<int> sum[100001];
int DP[100001];
vector<int> DP2[100001];
int cnt[200001];
map<int, int> hm[100001];
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;
for (int i = 1; i < cnt[1]; i++) DP2[1][i] = 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)
{
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 |
4 ms |
16732 KB |
Output is correct |
2 |
Correct |
4 ms |
16732 KB |
Output is correct |
3 |
Correct |
4 ms |
16732 KB |
Output is correct |
4 |
Correct |
4 ms |
16732 KB |
Output is correct |
5 |
Correct |
4 ms |
16732 KB |
Output is correct |
6 |
Correct |
4 ms |
16732 KB |
Output is correct |
7 |
Correct |
4 ms |
16988 KB |
Output is correct |
8 |
Correct |
5 ms |
16984 KB |
Output is correct |
9 |
Correct |
6 ms |
17504 KB |
Output is correct |
10 |
Correct |
6 ms |
17500 KB |
Output is correct |
11 |
Correct |
5 ms |
17244 KB |
Output is correct |
12 |
Correct |
5 ms |
17244 KB |
Output is correct |
13 |
Correct |
6 ms |
17244 KB |
Output is correct |
14 |
Correct |
6 ms |
17244 KB |
Output is correct |
15 |
Correct |
5 ms |
16988 KB |
Output is correct |
16 |
Correct |
5 ms |
17244 KB |
Output is correct |
17 |
Correct |
5 ms |
17244 KB |
Output is correct |
18 |
Correct |
4 ms |
16988 KB |
Output is correct |
19 |
Correct |
7 ms |
17244 KB |
Output is correct |
20 |
Correct |
8 ms |
17268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
38284 KB |
Output is correct |
2 |
Correct |
48 ms |
26960 KB |
Output is correct |
3 |
Correct |
102 ms |
43976 KB |
Output is correct |
4 |
Correct |
64 ms |
31308 KB |
Output is correct |
5 |
Correct |
448 ms |
80640 KB |
Output is correct |
6 |
Correct |
454 ms |
78240 KB |
Output is correct |
7 |
Correct |
239 ms |
73328 KB |
Output is correct |
8 |
Correct |
304 ms |
67664 KB |
Output is correct |
9 |
Runtime error |
227 ms |
133132 KB |
Execution killed with signal 11 |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16732 KB |
Output is correct |
2 |
Correct |
4 ms |
16732 KB |
Output is correct |
3 |
Correct |
4 ms |
16732 KB |
Output is correct |
4 |
Correct |
4 ms |
16732 KB |
Output is correct |
5 |
Correct |
4 ms |
16732 KB |
Output is correct |
6 |
Correct |
4 ms |
16732 KB |
Output is correct |
7 |
Correct |
4 ms |
16988 KB |
Output is correct |
8 |
Correct |
5 ms |
16984 KB |
Output is correct |
9 |
Correct |
6 ms |
17504 KB |
Output is correct |
10 |
Correct |
6 ms |
17500 KB |
Output is correct |
11 |
Correct |
5 ms |
17244 KB |
Output is correct |
12 |
Correct |
5 ms |
17244 KB |
Output is correct |
13 |
Correct |
6 ms |
17244 KB |
Output is correct |
14 |
Correct |
6 ms |
17244 KB |
Output is correct |
15 |
Correct |
5 ms |
16988 KB |
Output is correct |
16 |
Correct |
5 ms |
17244 KB |
Output is correct |
17 |
Correct |
5 ms |
17244 KB |
Output is correct |
18 |
Correct |
4 ms |
16988 KB |
Output is correct |
19 |
Correct |
7 ms |
17244 KB |
Output is correct |
20 |
Correct |
8 ms |
17268 KB |
Output is correct |
21 |
Correct |
98 ms |
38284 KB |
Output is correct |
22 |
Correct |
48 ms |
26960 KB |
Output is correct |
23 |
Correct |
102 ms |
43976 KB |
Output is correct |
24 |
Correct |
64 ms |
31308 KB |
Output is correct |
25 |
Correct |
448 ms |
80640 KB |
Output is correct |
26 |
Correct |
454 ms |
78240 KB |
Output is correct |
27 |
Correct |
239 ms |
73328 KB |
Output is correct |
28 |
Correct |
304 ms |
67664 KB |
Output is correct |
29 |
Runtime error |
227 ms |
133132 KB |
Execution killed with signal 11 |
30 |
Halted |
0 ms |
0 KB |
- |