This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
KAMUI!
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
▓▓▓▓▓▓▓▓▓▓▓▓▓██████████▓▓▓▓▓▓▓▓▓▓▓▓▓▓
▓▓▓▓▓▓▓▓▓▓███▓▓▓▓▓▓▓▓▓▓█████▓▓▓▓▓▓▓▓▓
▓▓▓▓▓▓▓███▓▒▒░▒▒▒▒▒░░░▒▒▒▓▓███▓▓▓▓▓▓▓
▓▓▓▓▓▓█▓▒▒▒▓▓████████▓▒▒░▒▒▒▓██▓▓▓▓▓▓
▓▓▓▓██▒▓████████████████▓░▒▒▒▒▓██▓▓▓▓
▓▓▓██▓███████▓▒░░░░░░░▒███▒░░▒▒▒██▓▓▓
▓▓█████████▓░░░░░░░░░░░░░██▓▓██████▓▓
▓▓█▒▓███████████▓▓▒▒▒▓▓██████████▓█▓▓
▓██▒▒▒███████████████████████████▓▓█▓
▓█▓▒▒░░████████▒░░░░▓███████████▓░▒█▓
▓█▒▒▒░██░▒████░░░░░░█████░░████▓░░▒█▓
▓█▒▒▒▒██░░░██▓░░░░░░░███▒░░████▒▒▒▒█▓
▓█▓▒▒▒██▒░░░▓█▓░░░░░▓█▓░░░▓███▓▒▒░▓█▓
▓█▓▒▒▒███░░░░████████▓░░░░░████▒▒▒▓█▓
▓▓█▒▒░▓███░░░▒███████▒░░░▒███▓▒▒▒▒█▓▓
▓▓██▒▒░████▒░░███████░░░▓███▓░▒▒▒██▓▓
▓▓▓██▒▒▒█████▓░░██████▒▓███▓▒░▒▒██▓▓▓
▓▓▓▓██▓▒░▓██████████████▓▒░▒▒▒▓██▓▓▓▓
▓▓▓▓▓▓██▓░▒▓█████████▒░░▒▒▒▒▓██▓▓▓▓▓▓
▓▓▓▓▓▓▓███▓▒▒▓██████▓░▒▒▒▓▓███▓▓▓▓▓▓▓
▓▓▓▓▓▓▓▓▓▓▓███▓▓▓▓███▓▓▓████▓▓▓▓▓▓▓▓▓
▓▓▓▓▓▓▓▓▓▓▓▓▓███████████▓▓▓▓▓▓▓▓▓▓▓▓▓
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
*/
#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
#include <random>
#include <array>
using namespace std;
template<typename T1, typename T2> inline void chkmin(T1 &a, T2 b) {if (a > b) a = b;}
template<typename T1, typename T2> inline void chkmax(T1 &a, T2 b) {if (a < b) a = b;}
#define files(FILENAME) read(FILENAME); write(FILENAME)
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define all(c) (c).begin(), (c).end()
#define sz(c) (int)(c).size()
#define left left228
#define right right228
#define y1 y1228
#define mp make_pair
#define pb push_back
#define y2 y2228
#define rank rank228
using ll = long long;
using ld = long double;
const string FILENAME = "input";
const int MAXN = 200229;
const ll LLINF = 3e18;
int N, M;
map<int, vector<pair<int, int> > > edge[MAXN];
int color[MAXN];
int cost[MAXN];
map<int, ll> sum[MAXN];
//you can only save it if its color is unique or its color is twice and you just used one of them & paid.
ll dist[MAXN];
map<int, ll> special[MAXN];
priority_queue<array<ll, 3>, vector<array<ll, 3> >, greater<array<ll, 3> > > pq;
ll ans;
void solve() {
cin >> N >> M;
for (int i = 0; i < M; i++) {
int u, v, c, d;
cin >> u >> v >> c >> d; u--; v--;
edge[u][c].pb({v, i});
edge[v][c].pb({u, i});
sum[u][c] += d;
sum[v][c] += d;
color[i] = c;
cost[i] = d;
}
for (int u = 0; u < N; u++)
{
for (auto &p : edge[u])
{
sort(all(p.second), [&](pair<int, int> a, pair<int, int> b)
{
return cost[a.second] > cost[b.second];
});
}
}
fill(dist, dist + N, LLINF);
dist[0] = 0;
pq.push({0, 0, -1});
while(!pq.empty())
{
ll d = pq.top()[0]; int u = pq.top()[1], c = pq.top()[2];
pq.pop();
if (c != -1)
{
if (d != special[u][c]) continue;
int cl = color[c];
int it = 0;
for (auto e : edge[u][cl])
{
int v = e.first, eid = e.second; ll dis = cost[eid];
ll nd = d + min(dis, sum[u][cl] - dis - cost[c]);
if (dist[v] > nd)
{
dist[v] = nd;
pq.push({nd, v, -1});
}
it++;
if (it > 3) break;
}
}
else
{
if (d != dist[u]) continue;
for (auto p : edge[u])
{
int cl = p.first;
for (auto e : p.second)
{
int v = e.first, eid = e.second; ll dis = cost[eid];
ll nd = d + min(dis, sum[u][cl] - dis);
if (dist[v] > nd)
{
dist[v] = nd;
pq.push({nd, v, -1});
}
nd = d + dis;
if ((special[v].find(eid) == special[v].end() || special[v][eid] > nd))
{
special[v][eid] = nd;
pq.push({nd, v, eid});
}
}
}
}
}
ans = dist[N - 1];
if (ans >= LLINF) ans = -1;
cout << ans << '\n';
}
//21:50
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// read(FILENAME);
int t;
t = 1;
// cin >> t;
while (t--) {
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... |