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 change of edges leading to this node
vector<ll> dp2[mxN]; // changed the edge leading into this node
vector<ar<int, 4>> 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;
int s1=adj[u].size(), s2=adj[v].size();
adj[u].push_back({v, c, w, s2});
adj[v].push_back({u, c, w, s1});
mp[u][c]+=w;
mp[v][c]+=w;
}
if (adj[n-1].empty()) {
cout << -1;
return 0;
}
for (int i=0; i<n; ++i) {
dp1[i]=INF;
dp2[i].assign(adj[i].size(), 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)
mp[u][adj[u][t][1]]-=adj[u][t][2];
for (ar<int, 4> v : adj[u]) {
ll cur=d+mp[u][v[1]]-v[2];
if (cur<dp1[v[0]])
pq.push({dp1[v[0]]=cur, v[0], -1});
cur=d+v[2];
if (cur<dp2[v[0]][v[3]])
pq.push({dp2[v[0]][v[3]]=cur, v[0], v[3]});
}
if (~t)
mp[u][adj[u][t][1]]+=adj[u][t][2];
}
ll ans=min(dp1[n-1], *min_element(dp2[n-1].begin(), dp2[n-1].end()));
cout << (ans==INF?-1:ans);
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:43:12: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
43 | 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... |