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 ss second
#define ff first
typedef long long ll;
#define int ll
int mod = 1e9+7;
int inf = 1e18;
struct edge{
int to, c, p;
};
map<int, vector<edge>> graph[100001];
int dp[100001];
map<int, int> dp2[100001], psum[100001];
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// ifstream cin ("shortcut.in");
// ofstream cout ("shortcut.out");
int n, m;
cin >> n >> m;
for(int i = 0; i < m; ++i){
int u, v, c, p;
cin >> u >> v >> c >> p;
graph[u][c].push_back({v,c,p});
graph[v][c].push_back({u,c,p});
psum[u][c] += p;
psum[v][c] += p;
}
memset(dp, 0x3f, sizeof dp);
dp[1] = 0;
using fud = tuple<int,int,int>;
priority_queue<fud> pq;
pq.push({0,1,0});
while(!pq.empty()){
auto[cost,node,c] = pq.top(); pq.pop();
if(c){
if(dp2[node][c]!=-cost) continue;
for(edge &i : graph[node][c]){
int case1 = psum[node][c] - i.p;
if(case1 - cost < dp[i.to]){
dp[i.to] = case1 - cost;
pq.push({-dp[i.to], i.to, 0});
}
}
}else{
if(dp[node]!=-cost) continue;
for(auto &i : graph[node]){
for(edge j : i.ss){
int case1 = psum[node][j.c] - j.p - cost;
if(case1<dp[j.to]){
dp[j.to] = case1;
pq.push({-dp[j.to], j.to, 0});
}
int case2 = j.p - cost;
if(case2<dp[j.to]){
dp[j.to] = case2;
pq.push({dp[j.to], j.to, 0});
}
int case3 = -cost;
if(!dp2[j.to].count(j.c)||case3<dp2[j.to][j.c]){
dp2[j.to][j.c] = case3;
pq.push({-dp2[j.to][j.c],j.to, j.c});
}
}
}
}
}
cout << (dp[n]>inf?-1:dp[n]) << '\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... |