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 pii pair<int, int>
#define fi first
#define se second
#define int long long
using namespace std;
const int N = 100013, M = 400013;
int a[M], b[M], c[M], p[M];
vector<int> G[N];
int d[M][2];
bool vis[M][2];
unordered_map<int, int> Map[N];
int32_t main(){
int n, m;
cin >> n >> m;
a[0] = 0, b[0] = 1, c[0] = 0, p[0] = 0;
for(int i = 1; i <= m; i++){
cin >> a[i] >> b[i] >> c[i] >> p[i];
G[a[i]].push_back(i);
a[m + i] = b[i], b[m + i] = a[i], c[m + i] = c[i], p[m + i] = p[i];
G[a[m + i]].push_back(m + i);
}
priority_queue<pair<int, pii>, vector<pair<int, pii>>, greater<pair<int, pii>>> pq;
pq.push({0, {0, 0}});
vis[0][0] = true;
for(int i = 1; i <= m * 2; i++){
d[i][0] = d[i][1] = (1LL << 60);
}
for(int i = 1; i <= n; i++){
for(auto j : G[i]){
Map[i][c[j]] += p[j];
}
}
while(pq.size()){
pair<int, pii> t = pq.top(); pq.pop();
int dis = t.fi, eid = t.se.fi, r = t.se.se;
vis[eid][r] = true;
if(d[eid][r] < dis) continue;
int u = a[eid], v = b[eid];
for(auto i : G[v]){
if(r){
int cost_1 = dis + p[i];
int cost_2 = dis + min(Map[v][c[i]] - p[i] - (c[i] == c[eid] && (eid - i) % m != 0 ? p[eid] : 0), p[i]);
if(cost_1 < d[i][1]){
d[i][1] = cost_1;
pq.push({d[i][1], {i, 1}});
}
if(cost_2 < d[i][0]){
d[i][0] = cost_2;
pq.push({d[i][0], {i, 0}});
}
}else{
int cost_1 = dis + p[i];
int cost_2 = dis + min(Map[v][c[i]] - p[i], p[i]);
if(cost_1 < d[i][1]){
d[i][1] = cost_1;
pq.push({d[i][1], {i, 1}});
}
if(cost_2 < d[i][0]){
d[i][0] = cost_2;
pq.push({d[i][0], {i, 0}});
}
}
}
}
int ans = INT64_MAX;
for(int i = 0; i <= m * 2; i++){
if(b[i] == n){
ans = min(ans, d[i][0]);
ans = min(ans, d[i][1]);
}
}
cout << (ans >= (1LL << 60) ? -1 : ans) << endl;
}
Compilation message (stderr)
Main.cpp: In function 'int32_t main()':
Main.cpp:42:7: warning: unused variable 'u' [-Wunused-variable]
42 | int u = a[eid], v = b[eid];
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |