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>
using namespace std;
using ll = long long;
const int maxn = 1e5 + 1;
vector <array <int, 3>> adj[maxn];
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b, c, p;
cin >> a >> b >> c >> p;
adj[a].push_back({b, c, p});
adj[b].push_back({a, c, p});
}
vector <vector <ll>> dis(n+1, vector <ll> (m+1, 1e18));
fill(dis[1].begin(), dis[1].end(), 0);
priority_queue <array <ll, 4>> pq;
pq.push({0, 0, 0, 1});
vector <vector <bool>> processed(n+1, vector <bool> (m+1));
ll ans = 1e18;
while (!pq.empty()) {
int s = pq.top()[3];
int col = pq.top()[1];
int p = pq.top()[2];
pq.pop();
if (processed[s][col])
continue;
processed[s][col] = true;
if (s == n)
ans = min(ans, dis[s][col]);
vector <int> cnt(m+1);
for (auto i : adj[s]) {
if (i[0] == p)
cnt[col]++;
else
cnt[i[1]]++;
}
for (auto i : adj[s]) {
for (int j = 1; j <= m; j++) {
if (cnt[j] == 1 && dis[i[0]][j] > dis[s][col]) {
dis[i[0]][j] = dis[s][col];
pq.push({-dis[i[0]][j], j, p, i[0]});
} else if (cnt[j] == 0 && dis[i[0]][j] > dis[s][col] + i[2]) {
dis[i[0]][j] = dis[s][col] + i[2];
pq.push({-dis[i[0]][j], j, p, i[0]});
}
}
}
}
if (ans == 1e18)
ans = -1;
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |