#include <bits/stdc++.h>
// #include <ext/rope>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
using namespace std;
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define bit(mask, i) ((mask >> i) & 1)
#define el '\n'
#define F first
#define S second
template <class X, class Y> bool maximize(X &x, const Y &y) { return (x < y ? x = y, 1 : 0); }
template <class X, class Y> bool minimize(X &x, const Y &y) { return (x > y ? x = y, 1 : 0); }
const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7;
const int MULTI = 0;
const ld eps = 1e-9;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; // R D L U
const int ddx[4] = {-1, 1, 1, -1}, ddy[4] = {1, 1, -1, -1}; // UR DR DL UL
const char cx[4] = {'R', 'D', 'L', 'U'};
const ll base = 31;
const int nMOD = 2;
const ll mods[] = {(ll)1e9 + 10777, (ll)1e9 + 19777, (ll)1e9 + 3, (ll)1e9 + 3777};
const int maxn = 6e5 + 5;
int n, m;
vector<pair<int, ll>> g[maxn];
int eu[maxn], ev[maxn], ec[maxn], ep[maxn];
ll dist[maxn], s[maxn];
map<pair<int, int>, int> mp;
int total_id;
void add(int u, int c, int p) {
if (mp.find(make_pair(u, c)) == mp.end())
mp[make_pair(u, c)] = ++total_id;
s[mp[make_pair(u, c)]] += p;
}
void dijkstra() {
memset(dist, 0x3f, sizeof(dist));
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
pq.push(make_pair(0, 1)); dist[1] = 0;
while (!pq.empty()) {
auto top = pq.top(); pq.pop();
ll d = top.F;
int u = top.S;
if (dist[u] != d) continue;
for (auto g: g[u]) {
int v = g.F;
ll w = g.S;
if (minimize(dist[v], d + w))
pq.push(make_pair(dist[v], v));
}
}
}
void solve() {
cin >> n >> m;
total_id = n;
for (int i = 1; i <= m; ++i) {
cin >> eu[i] >> ev[i] >> ec[i] >> ep[i];
add(eu[i], ec[i], ep[i]);
add(ev[i], ec[i], ep[i]);
}
for (int i = 1; i <= m; ++i) {
int u = eu[i], v = ev[i];
int x = mp[make_pair(eu[i], ec[i])], y = mp[make_pair(ev[i], ec[i])];
g[u].push_back(make_pair(v, min((ll) ep[i], s[x] - ep[i])));
g[v].push_back(make_pair(u, min((ll) ep[i], s[y] - ep[i])));
g[u].push_back(make_pair(y, 0));
g[v].push_back(make_pair(x, 0));
g[x].push_back(make_pair(v, s[x] - ep[i]));
g[y].push_back(make_pair(u, s[y] - ep[i]));
}
dijkstra();
cout << (dist[n] > 1e17 ? -1 : dist[n]);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (!MULTI) solve();
else {
int test; cin >> test;
while (test--) solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |