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>
#include<fstream>
using namespace std;
ifstream fin("WINTER.inp");
ofstream fout("WINTER.out");
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const ld PI = 3.14159265359;
const ll mod = 1e9 + 7;
const int mxn = 1e5, mxm = 2e5;
struct th{
int v, color, cost, id;
};
struct ch{
int s; ll d; int last;
};
struct cmp{
bool operator()(const ch &a, const ch &b){
return(a.d > b.d);
}
};
int n, m;
vt<th>adj[mxn + 1];
map<int, ll>d[mxn + 1];
ll sm[mxm + 1];
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
forr(i, 0, m){
int a, b, c, p; cin >> a >> b >> c >> p;
adj[a].pb({b, c, p, i}); adj[b].pb({a, c, p, i});
}
priority_queue<ch, vt<ch>, cmp>pq;
d[1][-1] = 0; pq.push({1, d[1][-1], -1});
while(!pq.empty()){
auto [u, dd, last] = pq.top(); pq.pop();
if(d[u][last] != dd)continue;
for(auto [v, color, cost, id]: adj[u]){
if(id == last)continue;
sm[color] += cost;
}
for(auto [v, color, cost, id]: adj[u]){
if(id == last)continue;
if(d[v].find(-1) == d[v].end() || d[v][-1] > dd + sm[color] - cost){
d[v][-1] = dd + sm[color] - cost;
pq.push({v, d[v][-1], -1});
}if(d[v].find(id) == d[v].end() || d[v][id] > dd + cost){
d[v][id] = dd + cost;
pq.push({v, d[v][id], id});
}
}
for(auto [v, color, cost, id]: adj[u]){
sm[color] = 0;
}
}
ll res = 1e18;
for(auto i: d[n]){
res = min(res, i.se);
}
if(res == 1e18){
cout << -1;
}else{
cout << res;
}
return(0);
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:49:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
49 | auto [u, dd, last] = pq.top(); pq.pop();
| ^
Main.cpp:51:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
51 | for(auto [v, color, cost, id]: adj[u]){
| ^
Main.cpp:55:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
55 | for(auto [v, color, cost, id]: adj[u]){
| ^
Main.cpp:66:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
66 | for(auto [v, color, cost, id]: adj[u]){
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |