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;
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 (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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |