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
const int N = 100005;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int n, m;
vector<array<int, 3>> adj[N];
map<int, vector<int>> cadj[N];
ll dp1[N]; map<int, ll> dp2[N];
int main(){
// freopen("a.in", "r", stdin);
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for(int i = 0; i<m; ++i){
int u, v, c, p;
cin >> u >> v >> c >> p;
adj[u].push_back({v, c, p});
adj[v].push_back({u, c, p});
cadj[u][c].push_back(v);
cadj[v][c].push_back(u);
}
memset(dp1, INF, sizeof dp1);
dp1[1] = 0;
priority_queue<array<ll, 3>> pq;
pq.push({0, 1, 0});
while(pq.size()){
auto [val, v, c] = pq.top();
pq.pop();
if(c){
int u = (cadj[v][c][0]==v?cadj[v][c][1]:cadj[v][c][0]);
if(val<dp1[u])
dp1[u] = val, pq.push({val, u, 0});
} else{
for(auto [u, c, p] : adj[v]){
if(!dp2[u].count(c) || val+p<dp2[u][c])
dp2[u][c] = val+p, pq.push({dp2[u][c], u, c});
if(val+p<dp1[u])
dp1[u] = val+p, pq.push({dp1[u], u, 0});
if((int)cadj[v][c].size()==1 && val<dp1[u])
dp1[u] = val, pq.push({dp1[u], u, 0});
}
}
}
cout << (dp1[n]==INF?-1:dp1[n]) << '\n';
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:25:17: warning: overflow in conversion from 'long long int' to 'int' changes value from '4557430888798830399' to '1061109567' [-Woverflow]
25 | memset(dp1, INF, sizeof dp1);
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |