#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i = a; i <= b; i++)
#define FOD(i,a,b) for(int i = a; i >= b; i--)
//#define int long long
#define fi first
#define se second
#define pb push_back
#define ll long long
#define ull unsigned long long
#define db double
#define lcm(a,b) a / __gcd(a, b) * b
#define ii pair<int,int>
#define iii pair<int,pair<int,int>>
#define iv pair<pair<int,int>,pair<int,int>>
#define sq(a) (a) * (a)
#define MASK(i) (1LL << i)
#define task "task"
const int inf = 1e9;
const ll INF = 1e18;
const int N = 1e5 + 5;
const int M = 2e5 + 5;
int n, m;
struct Adj {
int v, c;
ll w1, w2;
bool operator < (const Adj &other) const {return c < other.c;}
};
vector<Adj> g[N], tg[N];
ll sum[M];
struct State {
ll cost;
int u, c;
bool operator < (const State &other) const {return cost > other.cost;}
};
map<int, ll> d[N];
void dijkstra(int sr) {
priority_queue<State> pq;
pq.push({0, sr, 0});
d[sr][0] = 0;
while(pq.size()) {
State H = pq.top();
pq.pop();
if(H.cost != d[H.u][H.c]) continue;
if(H.c == 0) {
for(Adj K : g[H.u]) {
/// thay doi phan con lai
if(d[K.v].count(0) == 0 || d[K.v][0] > H.cost + min(K.w1, K.w2)) {
d[K.v][0] = H.cost + min(K.w1, K.w2);
pq.push({d[K.v][0], K.v, 0});
}
if(d[K.v].count(K.c) == 0 || d[K.v][K.c] > H.cost) {
d[K.v][K.c] = H.cost;
pq.push({d[K.v][K.c], K.v, K.c});
}
}
}
else {
int p = lower_bound(g[H.u].begin(), g[H.u].end(), Adj({0, H.c, 0, 0})) - g[H.u].begin();
FOR(i, p, (int)g[H.u].size() - 1) {
Adj K = g[H.u][i];
if(K.c != H.c) break;
if(d[K.v].count(0) == 0 || d[K.v][0] > H.cost + K.w2) {
d[K.v][0] = H.cost + K.w2;
pq.push({d[K.v][0], K.v, 0});
}
}
}
}
}
void KhoiLe() {
FOR(i, 1, n) {
for(Adj H : tg[i]) {
sum[H.c] += H.w1;
}
for(Adj H : tg[i]) {
g[i].pb({H.v, H.c, H.w1, sum[H.c] - H.w1});
}
for(Adj H : tg[i]) {
sum[H.c] -= H.w1;
}
sort(g[i].begin(), g[i].end());
}
dijkstra(1);
cout << (d[n].count(0) == 0 ? -1 : d[n][0]) << '\n';
}
signed main() {
if(fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
// freopen(task".out", "w", stdout);
}
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int test = 1;
// cin >> test;
while(test--) {
cin >> n >> m;
FOR(i, 1, m) {
int u, v, c, w; cin >> u >> v >> c >> w;
tg[u].pb({v, c, w, 0});
tg[v].pb({u, c, w, 0});
}
KhoiLe();
}
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
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(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |