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 pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
const ll inf = 1e17;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
vector<vector<array<int, 3>>> v(n);
for(int i = 0; i < m; i++){
int a, b, c, p; cin >> a >> b >> c >> p; a--, b--;
v[a].pb({b, c, p});
v[b].pb({a, c, p});
}
for(int i = 0; i < n; i++){
sort(v[i].begin(), v[i].end());
}
vector<vector<vector<ll>>> dis(n);
vector<vector<vector<bool>>> bl(n);
for(int i = 0; i < n; i++){
dis[i].assign(v[i].size(), vector<ll>(2, inf));
bl[i].assign(v[i].size(), vector<bool>(2, 0));
}
priority_queue<pair<ll, array<int, 3>>> pq;
pq.push({0, {0, -1, 0}});
while(!pq.empty()){
auto [nd, e, ch] = pq.top().sc; pq.pop();
array<int, 3> cur = {nd, 0, 0};
if(e == -1){
unordered_map<int, ll> mp;
for(auto [x, c, p]: v[nd]) mp[c]+=p;
for(auto [x, c, p]: v[nd]){
auto it = lower_bound(v[x].begin(), v[x].end(), cur);
int in = it-v[x].begin();
if(p < dis[x][in][1]){
dis[x][in][1] = p;
pq.push({-dis[x][in][1], {x, in, 1}});
}
if(mp[c]-p < dis[x][in][0]){
dis[x][in][0] = mp[c]-p;
pq.push({-dis[x][in][0], {x, in, 0}});
}
}
continue;
}
if(bl[nd][e][ch]) continue;
bl[nd][e][ch] = 1;
unordered_map<int, ll> mp;
for(auto [x, c, p]: v[nd]){
if(x != v[nd][e][0] || !ch) mp[c]+=p;
}
for(auto [x, c, p]: v[nd]){
auto it = lower_bound(v[x].begin(), v[x].end(), cur);
int in = it-v[x].begin();
if(dis[nd][e][ch]+p < dis[x][in][1]){
dis[x][in][1] = dis[nd][e][ch]+p;
pq.push({-dis[x][in][1], {x, in, 1}});
}
if(dis[nd][e][ch]+mp[c]-p < dis[x][in][0]){
dis[x][in][0] = dis[nd][e][ch]+mp[c]-p;
pq.push({-dis[x][in][0], {x, in, 0}});
}
}
}
ll ans = inf;
for(int i = 0; i < v[n-1].size(); i++) ans = min(ans, dis[n-1][i][0]), ans = min(ans, dis[n-1][i][1]);
if(ans >= inf) ans = -1;
cout << ans << "\n";
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:74:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for(int i = 0; i < v[n-1].size(); i++) ans = min(ans, dis[n-1][i][0]), ans = min(ans, dis[n-1][i][1]);
| ~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |