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;
#define ll long long
#define ar array
const int mxN=1e5;
const ll INF=1e18;
int n, m;
ll dp1[mxN]; // no changed edges leading to this node
map<int, ll> dp2[mxN]; // must use this color
map<int, vector<ar<int, 2>>> adj[mxN];
map<int, ll> mp[mxN];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i=0; i<m; ++i) {
int u, v, c, w;
cin >> u >> v >> c >> w, --u, --v;
adj[u][c].push_back({v, w});
adj[v][c].push_back({u, w});
mp[u][c]+=w;
mp[v][c]+=w;
}
for (int i=0; i<n; ++i) {
dp1[i]=INF;
for (auto& x : adj[i])
dp2[i][x.first]=INF;
}
dp1[0]=0;
priority_queue<ar<ll, 3>, vector<ar<ll, 3>>, greater<ar<ll, 3>>> pq;
pq.push({0, 0, -1});
while(pq.size()) {
ll d=pq.top()[0];
int u=pq.top()[1], t=pq.top()[2];
pq.pop();
if (t==-1&&d^dp1[u]||~t&&d^dp2[u][t])
continue;
if (t==-1) {
for (auto& x : adj[u]) {
int c=x.first;
for (ar<int, 2> v : x.second) {
ll cur=min(d+mp[u][c]-v[1], d+v[1]);
if (cur<dp1[v[0]])
pq.push({dp1[v[0]]=cur, v[0], -1});
// change this edge and force this color
if (d<dp2[v[0]][c])
pq.push({dp2[v[0]][c]=d, v[0], c});
}
}
} else {
for (ar<int, 2> v : adj[u][t]) {
ll cur=d+mp[u][t]-v[1];
if (cur<dp1[v[0]])
pq.push({dp1[v[0]]=cur, v[0], -1});
}
}
}
cout << (dp1[n-1]^INF?dp1[n-1]:-1);
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:39:12: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
39 | if (t==-1&&d^dp1[u]||~t&&d^dp2[u][t])
| ~~~~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |