#include <bits/stdc++.h>
#define int long long
using namespace std;
vector<array<int, 4>> adj[100001];
int DP[100001];
int sum[200001];
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);
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();
for (int i = 0; i < cnt[u]; i++) sum[i] = 0;
for (const array<int, 4>& cur : adj[u]) sum[cur[1]] += cur[2];
for (const auto& [v, c, w, vc] : adj[u])
{
bool k = false;
int s = sum[c];
if (DP2[u][c] == d && pc == c && DP[v] > DP2[u][c] + s - w)
{
DP[v] = DP2[u][c] + s - w;
k = true;
}
if (DP[v] > DP[u] + min(w, s - w))
{
DP[v] = DP[u] + min(w, s - w);
k = true;
}
if (k) q.push({DP[v], v, -1});
if (DP2[v][vc] > DP[u])
{
DP2[v][vc] = DP[u];
q.push({DP2[v][vc], v, vc});
}
}
}
if (DP[n] > (int)1e18) cout << -1;
else cout << DP[n];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13660 KB |
Output is correct |
2 |
Correct |
3 ms |
13660 KB |
Output is correct |
3 |
Correct |
3 ms |
13656 KB |
Output is correct |
4 |
Correct |
3 ms |
13660 KB |
Output is correct |
5 |
Correct |
3 ms |
13660 KB |
Output is correct |
6 |
Correct |
3 ms |
13660 KB |
Output is correct |
7 |
Correct |
4 ms |
13916 KB |
Output is correct |
8 |
Correct |
3 ms |
13732 KB |
Output is correct |
9 |
Correct |
12 ms |
14172 KB |
Output is correct |
10 |
Correct |
9 ms |
14172 KB |
Output is correct |
11 |
Correct |
4 ms |
13916 KB |
Output is correct |
12 |
Correct |
4 ms |
13916 KB |
Output is correct |
13 |
Correct |
4 ms |
13988 KB |
Output is correct |
14 |
Correct |
4 ms |
13916 KB |
Output is correct |
15 |
Correct |
4 ms |
13916 KB |
Output is correct |
16 |
Correct |
5 ms |
13852 KB |
Output is correct |
17 |
Correct |
4 ms |
13916 KB |
Output is correct |
18 |
Correct |
4 ms |
13660 KB |
Output is correct |
19 |
Correct |
4 ms |
13916 KB |
Output is correct |
20 |
Correct |
4 ms |
13916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
27524 KB |
Output is correct |
2 |
Correct |
47 ms |
20424 KB |
Output is correct |
3 |
Correct |
114 ms |
32564 KB |
Output is correct |
4 |
Correct |
61 ms |
23528 KB |
Output is correct |
5 |
Execution timed out |
3090 ms |
54964 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13660 KB |
Output is correct |
2 |
Correct |
3 ms |
13660 KB |
Output is correct |
3 |
Correct |
3 ms |
13656 KB |
Output is correct |
4 |
Correct |
3 ms |
13660 KB |
Output is correct |
5 |
Correct |
3 ms |
13660 KB |
Output is correct |
6 |
Correct |
3 ms |
13660 KB |
Output is correct |
7 |
Correct |
4 ms |
13916 KB |
Output is correct |
8 |
Correct |
3 ms |
13732 KB |
Output is correct |
9 |
Correct |
12 ms |
14172 KB |
Output is correct |
10 |
Correct |
9 ms |
14172 KB |
Output is correct |
11 |
Correct |
4 ms |
13916 KB |
Output is correct |
12 |
Correct |
4 ms |
13916 KB |
Output is correct |
13 |
Correct |
4 ms |
13988 KB |
Output is correct |
14 |
Correct |
4 ms |
13916 KB |
Output is correct |
15 |
Correct |
4 ms |
13916 KB |
Output is correct |
16 |
Correct |
5 ms |
13852 KB |
Output is correct |
17 |
Correct |
4 ms |
13916 KB |
Output is correct |
18 |
Correct |
4 ms |
13660 KB |
Output is correct |
19 |
Correct |
4 ms |
13916 KB |
Output is correct |
20 |
Correct |
4 ms |
13916 KB |
Output is correct |
21 |
Correct |
97 ms |
27524 KB |
Output is correct |
22 |
Correct |
47 ms |
20424 KB |
Output is correct |
23 |
Correct |
114 ms |
32564 KB |
Output is correct |
24 |
Correct |
61 ms |
23528 KB |
Output is correct |
25 |
Execution timed out |
3090 ms |
54964 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |