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 int long long
using namespace std;
const int linf = 1e18;
const int mxN = 1e5 + 10;
const int mxM = 2e5 + 10;
int n, m;
map<int, vector<pair<int, int>>> adj[mxN];
map<int, int> sum[mxN], d[mxN];
int col[mxM], cost[mxM];
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= m; i ++){
int u, v;
cin >> u >> v >> col[i] >> cost[i];
adj[u][col[i]].push_back({v, i});
adj[v][col[i]].push_back({u, i});
d[u][col[i]] = d[v][col[i]] = linf;
sum[u][col[i]] += cost[i];
sum[v][col[i]] += cost[i];
}
for(int i = 1; i <= n; i ++) d[i][0] = linf;
priority_queue<array<int, 3>, vector<array<int, 3>>, greater<array<int, 3>>> pq;
pq.push({0, 1, 0});
d[1][0] = 0;
while(!pq.empty()){
int u = pq.top()[1];
int D = pq.top()[0];
int k = pq.top()[2];
pq.pop();
int C = col[k];
if(D != d[u][C]) continue ;
if(k){
for(auto v : adj[u][C]){
if(v.first == k) continue ;
int nCost = D + sum[u][C] - cost[v.second];
if(nCost < d[v.first][0]){
d[v.first][0] = nCost;
pq.push({nCost, v.first, 0});
}
}
} else {
for(auto x : adj[u]){
int c = x.first;
for(auto v : x.second){
int nCost = D + min(sum[u][c] - cost[v.second], cost[v.second]);
if(nCost < d[v.first][0]){
d[v.first][0] = nCost;
pq.push({nCost, v.first, 0});
}
if(D < d[v.first][c]){
d[v.first][c] = D;
pq.push({D, v.first, v.second});
}
}
}
}
}
if(d[n][0] == linf) d[n][0] = -1;
cout << d[n][0] << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |