This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
using namespace std;
map<int, vector<pair<int, int>>> adj[100001];
unordered_map<int, int> D[100001];
unordered_map<int, int> sum[100001];
map<int, pair<int, int>> edge[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;
map<int, vector<pair<int, int>>> test;
for (int i = 0; i < m; i++)
{
int u, v, c, w;
cin >> u >> v >> c >> w;
adj[u][c].emplace_back(v, w);
adj[v][c].emplace_back(u, w);
sum[u][c] += w;
sum[v][c] += w;
edge[u][v] = {c, w};
edge[v][u] = {c, w};
}
for (int i = 1; i <= n; i++) edge[i][0] = {0, 0};
D[1][0] = 1;
priority_queue<array<int, 3>, vector<array<int, 3>>, greater<>> q;
q.push({1, 1, 0});
while (!q.empty())
{
int u = q.top()[1];
int t = q.top()[2];
int pc, pw;
tie(pc, pw) = edge[u][t];
q.pop();
for (const auto& [c, l] : adj[u])
{
int sz = (int)l.size();
int s = sum[u][c];
if (pc == c)
{
sz--;
s -= pw;
}
for (const auto& [v, w] : l)
{
if (v == t) continue;
if (D[u][t] + w < D[v][u] || !D[v][u])
{
D[v][u] = D[u][t] + w;
q.push({D[v][u], v, u});
}
if (sz == 1 && (D[u][t] < D[v][u] || !D[v][u]))
{
D[v][u] = D[u][t];
q.push({D[v][u], v, u});
}
/*if (sz != 1 && (D[u][t] + s - w < D[v][0] || !D[v][0]))
{
cerr << c << " " << pc << " " << t << " " << sum[u][c] << " " << s << endl;
D[v][0] = D[u][t] + s - w;
q.push({D[v][0], v, 0});
}*/
}
}
}
int ans = (int)1e18;
for (const auto& [t, dis] : D[n]) ans = min(ans, dis);
if (ans == (int)1e18) cout << -1;
else cout << ans - 1;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |