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>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using ld = long double;
template<typename T1, typename T2> bool chkmin(T1 &x, T2 y) { return y < x ? (x = y, true) : false; }
template<typename T1, typename T2> bool chkmax(T1 &x, T2 y) { return y > x ? (x = y, true) : false; }
void debug_out() { cerr << endl; }
template<typename T1, typename... T2> void debug_out(T1 A, T2... B) { cerr << ' ' << A; debug_out(B...); }
template<typename T> void mdebug_out(T* a, int n) { for (int i = 0; i < n; ++i) cerr << a[i] << ' '; cerr << endl; }
#ifdef DEBUG
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define mdebug(a, n) cerr << #a << ": ", mdebug_out(a, n)
#else
#define debug(...) 1337
#define mdebug(a, n) 1337
#endif
template<typename T> ostream& operator << (ostream& stream, const vector<T> &v) { for (auto x : v) stream << x << ' '; return stream; }
template<typename T1, typename T2> ostream& operator << (ostream& stream, const pair<T1, T2>& p) { return stream << p.first << ' ' << p.second; }
#define int ll
const ll INF64 = 1e18;
const int N = 100005;
int n, m;
struct Edge
{
int from, to, t;
ll c;
Edge() {}
Edge(int _from, int _to, int _t, ll _c): from(_from), to(_to), t(_t), c(_c) {}
};
vector<Edge> G[N];
void add_edge(int a, int b, int t, ll c)
{
G[a].emplace_back(a, b, t, c);
G[b].emplace_back(b, a, t, c);
}
map<pii, int> ctp;
int types[N];
vector<int> rtp[N];
int idxt[2 * N];
vector<vector<Edge>> GT[N];
vector<ll> sumC[N];
vector<ll> dist[N];
signed main()
{
#ifdef DEBUG
freopen("in", "r", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; ++i)
{
int a, b, t, c;
cin >> a >> b >> t >> c;
add_edge(a, b, t, c);
}
for (int v = 1; v <= n; ++v)
{
types[v] = 0;
rtp[v] = {-1};
for (auto& e : G[v])
if (!idxt[e.t])
{
++types[v];
ctp[{v, e.t}] = idxt[e.t] = types[v];
rtp[v].push_back(e.t);
}
GT[v].resize(types[v] + 1);
for (auto e : G[v])
GT[v][idxt[e.t]].push_back(e);
sumC[v].assign(types[v] + 1, 0);
for (auto& e : G[v])
sumC[v][idxt[e.t]] += e.c;
for (auto& e : G[v])
idxt[e.t] = 0;
}
/*for (int v = 1; v <= n; ++v)
{
debug(v);
debug(rtp[v]);
}*/
for (int v = 1; v <= n; ++v)
dist[v].assign(types[v] + 1, INF64);
dist[1][0] = 0;
multiset<pair<ll, pii>> q;
q.insert({0, {1, 0}});
while (!q.empty())
{
ll d = q.begin()->fi;
int v = q.begin()->se.fi;
int ct = q.begin()->se.se;
q.erase(q.begin());
if (d != dist[v][ct]) continue;
if (ct == 0)
{
for (auto& e : G[v])
{
int u = e.to;
int ct = ctp[{v, e.t}];
if (chkmin(dist[u][0], d + min(e.c, sumC[v][ct] - e.c)))
q.insert({dist[u][0], {u, 0}});
int ctu = ctp[{u, e.t}];
if (chkmin(dist[u][ctu], d))
q.insert({dist[u][ctu], {u, ctu}});
}
}
else
{
int t = rtp[v][ct];
for (auto& e : GT[v][ct])
{
if (e.t == t)
{
int u = e.to;
int ctu = ctp[{u, t}];
if (chkmin(dist[u][0], d + sumC[v][ct] - e.c))
q.insert({dist[u][0], {u, 0}});
}
}
}
}
cout << (dist[n][0] >= INF64 ? -1 : dist[n][0]) << '\n';
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:141:25: warning: unused variable 'ctu' [-Wunused-variable]
141 | int ctu = ctp[{u, t}];
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |