This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* Author : Mychecksdead */
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef long double ld;
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define PI 3.1415926535
#define pb push_back
#define setp() cout << setprecision(15)
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " is " << x << '\n';
const int N = 1e6+100, M = 1e5+10, F = 2147483646, K = 20;
struct Edge{
int nxt, c, e;
};
int n, m;
ll dist[N];
vector<Edge> g[N];
multiset<int> color[N];
map<int, ll> s[N];
void solve(){
cin >> n >> m;
for(int i = 0; i < m; ++i){
int u, v, c, p; cin >> u >> v >> c >> p;
g[u].pb({v, c, p});
g[v].pb({u, c, p});
color[u].insert(c);
color[v].insert(c);
s[u][c] += p;
s[v][c] += p;
}
for(int i = 1; i <= n; ++i) dist[i] = 1e18;
priority_queue<array<ll, 3>> q;
vector<bool> vis(n + 1);
dist[n] = 0;
q.push({0, n, -1});
while(!q.empty()){
int v = q.top()[1], cc = q.top()[2]; q.pop();
if(vis[v]) continue;
vis[v] = 1;
// cout << v << '\n';
for(auto E: g[v]){
int u = E.nxt, c = E.c; ll e = E.e;
bool ok = 0;
ll val = s[u][c];
if(cc == c) e = 0;
if(e > val - e) ok = 1, e = val - e;
if(dist[v] + e < dist[u]){
dist[u] = dist[v] + e;
q.push({-dist[u], u, ok ? c : -1});
}
}
}
if(dist[1] == 1e18) cout << -1;
else cout << dist[1];
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int T = 1, aa;
// cin >> T;aa=T;
while(T--){
// cout << "Case #" << aa-T << ": ";
solve();
cout << '\n';
}
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:73:16: warning: unused variable 'aa' [-Wunused-variable]
73 | int T = 1, aa;
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |