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 task "robot1"
#define ull unsigned long long
#define ll long long
#define double long double
#define For(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define ForD(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define endl '\n'
#define maxN 1000005
#define fi first
#define se second
#define pb push_back
#define mask(i) (1ll << (i))
#define bit(mask, i) (mask >> i & 1)
#define onBIT(mask, i) (mask | (1ll << (i)))
#define offBIT(mask, i) (mask &~ (1ll << (i)))
#define cntbit __builtin_popcountll
#define logg(x) 31-__builtin_clz(x)
#define Blog(n) (63 - __builtin_clzll(n))
#define sqrt sqrtl
#define ii pair<ll,ll>
#define vi vector<ll>
#define vpi vector<ii>
#define vii vector<vector<ll>>
#define reset(c, x) memset(c, x, sizeof(c))
const int base = 311;
const ll mod = 1e9+7;
const ll oo = 1e18;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int block = 320;
const double pi = acos(-1);
template <class X, class Y> bool maximize(X &a, const Y &b){
if(a < b) return a = b, true;
return false;
}
template <class X, class Y> bool minimize(X &a, const Y &b){
if(a > b) return a = b, true;
return false;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <class T> T rand(T l, T h) { return uniform_int_distribution <T> (l, h) (rng); }
template <class T> T rand(T h) { return uniform_int_distribution <T> (0, h - 1) (rng); }
ll fixmod(ll xxx){ if (xxx<0) return (xxx+mod*mod)%mod;return xxx%mod;}
ll sqr(ll xxx){return (xxx*xxx);}
ll pw(ll xxx,ll kkk) {if (kkk==0) return 1;ll tmp=pw(xxx,kkk/2);if (kkk%2) return fixmod(fixmod(tmp*tmp)*xxx);return fixmod(tmp*tmp);}
ll n, m;
vector<pair<ll, ii>> g[maxN];
vector<ii> ke[maxN];
ll sum[maxN];
bool vis[maxN];
ll d[maxN];
ll cur;
void dij(ll s)
{
For(i, 1, cur) d[i] = oo;
priority_queue<ii, vpi, greater<ii>> pq;
pq.push({0, 1});
while (pq.size())
{
ii x = pq.top();
pq.pop();
if (vis[x.se]) continue;
vis[x.se] = 1;
for (auto xx : ke[x.se])
{
if (d[xx.fi] <= x.fi + xx.se) continue;
d[xx.fi] = x.fi + xx.se;
pq.push({d[xx.fi], xx.fi});
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
For(i, 1, m)
{
ll x, y, c, p;
cin >> x >> y >> c >> p;
g[x].pb({c, {y, p}});
g[y].pb({c, {x, p}});
ke[x].pb({y, p});
ke[y].pb({x, p});
}
cur = n;
For(i, 1, n) sort(g[i].begin(), g[i].end());
For(i, 1, n)
{
for (auto x : g[i]) sum[x.fi] += x.se.se;
ll last = -oo;
for (auto x : g[i])
{
if (x.fi != last)
{
++ cur;
last = x.fi;
ke[i].pb({cur, 0});
}
ke[cur].pb({x.se.fi, sum[x.fi] - x.se.se});
ke[x.se.fi].pb({cur, sum[x.fi] - x.se.se});
}
for (auto x : g[i]) sum[x.fi] -= x.se.se;
}
dij(1);
if (d[n] == oo) cout << - 1;
else cout << d[n];
cerr << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << " s.\n";
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... |