#include <bits/stdc++.h>
//#include "includeall.h"
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define input(x) scanf("%lld", &x);
#define input2(x, y) scanf("%lld%lld", &x, &y);
#define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a);
#define print(x, y) printf("%lld%c", x, y);
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define all(x) x.begin(), x.end()
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
#define FOR(i, x, n) for (ll i =x; i<=n; ++i)
#define RFOR(i, x, n) for (ll i =x; i>=n; --i)
using namespace std;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
typedef long double ld;
typedef pair<ld, ll> pd;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
typedef pair<pi, pi> piii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll const INF = 1e18;
ll n, m, a, b, c, d;
unordered_map<ll, ll> ps[100005];
unordered_map<ll, vector<pi> > adj[100005];
unordered_map<ll, ll> dist[100005];
int main()
{
cin >> n >> m;
for (ll i=1; i<=n; ++i) dist[i][0] = INF;
for (ll i=0; i<m; ++i)
{
cin >> a >> b >> c >> d;
ps[a][c] += d, ps[b][c] += d;
adj[a][c].pb(mp(b, d));
adj[b][c].pb(mp(a, d));
dist[a][c] = dist[b][c] = INF;
}
dist[1][0] = 0;
priority_queue<pii, vector<pii>, greater<pii> > pq;
pq.push(mp(0, mp(1, 0)));
while (pq.size())
{
ll now = pq.top().s.f, c = pq.top().s.s, dis = pq.top().f;
pq.pop();
if (dist[now][c] < dis) continue;
if (c==0)
{
for (auto & x: adj[now]) for (auto & u: x.s)
{
if (dist[u.f][x.f] > dis)
{
dist[u.f][x.f] = dis;
pq.push(mp(dis, mp(u.f, x.f)));
}
if (dist[u.f][0] > dis + min(u.s, ps[now][x.f] - u.s))
{
dist[u.f][0] = dis + min(u.s, ps[now][x.f] - u.s);
pq.push(mp(dist[u.f][0], mp(u.f, 0)));
}
}
}
else
{
for (auto & u: adj[now][c])
{
if (dist[u.f][c] > dis + ps[now][c] - u.s)
{
dist[u.f][c] = dis + ps[now][c] - u.s;
pq.push(mp(dist[u.f][c], mp(u.f, 0)));
}
}
}
}
ll ans = dist[n][0];
if (ans == INF) cout << -1 << endl;
else cout << ans << endl;
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... |