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; ll e;
};
int n, m;
ll dist[N][2];
vector<Edge> g[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});
s[u][c] += p;
s[v][c] += p;
}
for(int i = 1; i <= n; ++i) dist[i][0] = dist[i][1] = 1e18;
priority_queue<array<ll, 3>> q;
vector<bool> vis[2]; vis[0].resize(n+1), vis[1].resize(n+1);
dist[n][0] = 0;
q.push({0, n, -1});
while(!q.empty()){
int v = q.top()[1], cc = q.top()[2];
ll D = -q.top()[0];
q.pop();
if(vis[cc == -1][v]) continue;
vis[cc == -1][v] = 1;
// cout << v << '\n';
for(auto E: g[v]){
int u = E.nxt, c = E.c; ll e = E.e;
ll val = s[u][c];
if(cc == c) e = 0;
if(D + e < dist[u][0]){
dist[u][0] = D + e;
q.push({-dist[u][0], u, -1});
}
if(e > 0 && D + val - e < dist[u][1]){
dist[u][1] = D + val - e;
q.push({-dist[u][1], u, c});
}
}
}
if(min(dist[1][0], dist[1][1]) == 1e18) cout << -1;
else cout << min(dist[1][0], dist[1][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:75:16: warning: unused variable 'aa' [-Wunused-variable]
75 | 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... |