#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; i--)
#define pa pair<ll, ll>
#define fi first
#define se second
#define bit(mask, j) ((mask >> j) & 1)
#define t_test int t;cin >> t;while(t--)
const ll mod = 1e9 + 7;
const ll INF = 1e18;
inline void adm(ll &x){if(x>=mod)x%=mod;else if(x<0)x+=mod;}
//--------------------------------------------------------------------
const ll N = 1e5 + 10;
ll n, m;
struct edge {
ll v, c, p;
};
struct state {
ll cost, u, c;
bool operator < (const state & a) const {
return cost > a.cost;
}
};
vector<edge> g[N];
ll d[N];
unordered_map<ll, ll> d2[N], clr[N];
void DJK() {
priority_queue<state> q;
memset(d, 0x3f, sizeof d);
d[1] = 0;
q.push({0, 1, 0});
while(!q.empty()) {
ll u = q.top().u, cost = q.top().cost, c = q.top().c;
q.pop();
if(c != 0) {
if(d2[u][c] != cost) continue;
for(auto e : g[u]) {
ll v = e.v, _c = e.c, p = e.p;
if(_c != c) continue;
ll val = cost + clr[u][_c] - p;
if(d[v] > val) {
d[v] = val;
q.push({d[v], v, 0});
}
}
}
else {
if(cost != d[u]) continue;
if(u == n) {
cout << cost;
exit(0);
}
for(auto e : g[u]) {
ll v = e.v, _c = e.c, p = e.p;
ll val = cost + clr[u][_c] - p;
if(d[v] > val) {
d[v] = val;
q.push({d[v], v, 0});
}
val = cost + p;
if(d[v] > val) {
d[v] = val;
q.push({d[v], v, 0});
}
val = cost;
if(d2[v].find(_c) == d2[v].end() || d2[v][_c] > val) {
d2[v][_c] = val;
q.push({d2[v][_c], v, _c});
}
}
}
}
}
void hbmt() {
cin >> n >> m;
FOR(i, 1, m) {
ll u, v, c, p;
cin >> u >> v >> c >> p;
g[u].push_back({v, c, p});
g[v].push_back({u, c, p});
clr[u][c] += p;
clr[v][c] += p;
}
DJK();
cout << -1;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
if(fopen("hbmt.inp", "r")) {
freopen("hbmt.inp", "r", stdin);
freopen("hbmt.out", "w", stdout);
}
// t_test
hbmt();
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:96:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | freopen("hbmt.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:97:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | freopen("hbmt.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |