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>
#define taskname "A"
using namespace std;
void World_Final();
void Solve();
int main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
if (fopen(taskname".in", "r")) {
freopen(taskname".in", "r", stdin);
freopen(taskname".out", "w", stdout);
}
World_Final();
}
void World_Final(){
int Tests = 1;
//cin >> Tests;
for (int i = 1; i <= Tests; i ++) {
//cout << i << "\n";
Solve();
}
}
const int MAXN = 1e5 + 10;
const int64_t INF = 1e18;
int N, M;
struct Edge{ int to, c, p; };
vector<Edge> G[MAXN]; map<int, int64_t> sum[MAXN], f[MAXN];
map<int, vector<pair<int, int>>> G1[MAXN];
void Solve(){
cin >> N >> M; for (int i = 1; i <= M; i ++) {
int u, v, c, p; cin >> u >> v >> c >> p;
G[u].push_back({v, c, p});
G[v].push_back({u, c, p});
G1[u][c].push_back({v, p});
G1[v][c].push_back({u, p});
sum[u][c] += p;
sum[v][c] += p;
} f[1][0] = 0;
priority_queue<pair<int64_t, pair<int, int>>> pq; pq.push(make_pair(0, make_pair(1, 0)));
while (!pq.empty()) {
int64_t d = pq.top().first;
int u, prevc; tie(u, prevc) = pq.top().second; pq.pop();
if (-d != f[u][prevc]) continue;
if (prevc) {
for (auto [v, p] : G1[u][prevc]) {
/// Have to flip others because haven't flipped previous edge with same
/// color c
if (f[v].find(0) == f[v].end() || f[v][0] > f[u][prevc] + sum[u][prevc] - p) {
f[v][0] = f[u][prevc] + sum[u][prevc] - p;
pq.push(make_pair(-f[v][0], make_pair(v, 0)));
}
}
} else {
for (auto [v, c, p] : G[u]) {
/// Flip this edge
if (f[v].find(0) == f[v].end() || f[v][0] > f[u][0] + p) {
f[v][0] = f[u][0] + p;
pq.push(make_pair(-f[v][0], make_pair(v, 0)));
}
/// Flip others
if (f[v].find(0) == f[v].end() || f[v][0] > f[u][0] + sum[u][c] - p) {
f[v][0] = f[u][0] + sum[u][c] - p;
pq.push(make_pair(-f[v][0], make_pair(v, 0)));
}
/// Save to flip with latter edge
if (f[v].find(c) == f[v].end() || f[v][c] > f[u][0]) {
f[v][c] = f[u][0];
pq.push(make_pair(-f[v][c], make_pair(v, c)));
}
}
}
}
if (f[N].find(0) == f[N].end()) cout << -1;
else cout << f[N][0];
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:11:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
11 | freopen(taskname".in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:12:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | freopen(taskname".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... |