#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l); i>=(r); --i)
#define fore(x, v) for(auto &x : v)
#define all(x) (x).begin(), (x).end()
#define siz(x) (int)(x).size()
#define pow2(x) (1ll << (x))
#define on(mask, x) ((mask >> x & 1) == 1)
#define off(mask, x) ((mask >> x & 1) == 0)
#define mp make_pair
#define vec vector
#define ar array
#define fi first
#define se second
#define pii pair<int, int>
#define out(x) cout << #x << " " << x << "\n"
template<class T> bool ckmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> bool ckmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; }
void setIO() {
ios::sync_with_stdio(0);
cin.tie(0);
#define task "input"
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
}
const ll LINF = 1e18+18;
const int INF = 1e9+9;
const int N = 2e5+5;
int n, m;
struct Edge {
int u, v, c, p;
Edge (int _u=0, int _v=0, int _c=0, int _p=0) : u(_u), v(_v), c(_c), p(_p) {}
int other(int x) { return x^u^v; }
};
vector<Edge> edges;
vector<int> adj[N];
map<int, ll> color[N];
int main() {
setIO();
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int u, v, c, p;
cin >> u >> v >> c >> p;
adj[u].push_back(siz(edges));
adj[v].push_back(siz(edges));
edges.emplace_back(u, v, c, p);
color[u][c] += p;
color[v][c] += p;
}
vector<ll> dist(n+1, LINF);
priority_queue<pair<ll, int>> pq;
dist[1] = 0;
pq.push({0, 1});
while (!pq.empty()) {
auto top = pq.top();
pq.pop();
if (-top.fi != dist[top.se]) continue;
int u = top.se;
// cout << "Node " << u << " Dist " << dist[u] << "\n";
for (int i : adj[u]) {
int v = edges[i].other(u);
ll cost = min(color[u][edges[i].c]-edges[i].p, (color[u][edges[i].c] > edges[i].p ? edges[i].p : 0ll));
if (dist[v] > dist[u]+cost) {
dist[v] = dist[u]+cost;
pq.push({-dist[v], v});
}
}
}
cout << (dist[n] == LINF ? -1 : dist[n]) << "\n";
}
Compilation message (stderr)
Main.cpp: In function 'void setIO()':
Main.cpp:31:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:32:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | freopen(task".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... |