// IamHereForFun //
#include <bits/stdc++.h>
using namespace std;
#define LSOne(X) ((X) & -(X))
const int N = 1e5 + 5;
const int M = 11;
const int L = 12;
const int LG = 25;
const int P = 37;
const long long INF = 1e18 + 5;
const int MOD = 1e9 + 7;
const int nx[] = {0, -1, 0, 1}, ny[] = {-1, 0, 1, 0};
// find shortest path, for every node, calculate minimum path
// problemo: because everytime you change an edge, it affects the next node too =))
// lets say you change a edge, then you should change it so that it doesn't affect the next one =))
// but if you change a edge for another edge to be valid, then you shouldn't go to the node that it's connect too, other wise just use it
// so we use dp[N] as normal, but in your pq, you save a number indicating that the edge that leads to it is changed or not
int n, m;
vector<array<int, 3>> adj[N];
vector<int> id[N];
vector<long long> dp[N];
vector<bool> vis[N];
long long ans = 0;
int gid(int a, int b)
{
return lower_bound(id[a].begin(), id[a].end(), b) - id[a].begin();
}
void solve()
{
cin >> n >> m;
for (int x = 1; x <= n; x++)
{
id[x].push_back(-1);
}
for (int x = 0; x < m; x++)
{
int a, b, c, p;
cin >> a >> b >> c >> p;
adj[a].push_back({b, c, p});
adj[b].push_back({a, c, p});
id[a].push_back(b);
id[b].push_back(a);
}
for(int x = 1; x <= n; x++)
{
sort(id[x].begin(), id[x].end());
vis[x].assign(id[x].size(), 0);
dp[x].assign(id[x].size(), INF);
}
priority_queue<array<long long, 3>> pq;
dp[1][0] = 0;
pq.push({0, -1, 1});
while (!pq.empty())
{
array<long long, 3> arr = pq.top();
pq.pop();
int i = arr[2], l = arr[1];
l = gid(i, l);
if (vis[i][l])
continue;
vis[i][l] = true;
map<int, long long> mp;
for (array<int, 3> a : adj[i])
{
if (a[0] == l)
continue;
mp[a[1]] += a[2];
}
for (array<int, 3> a : adj[i])
{
if (a[0] == l)
continue;
long long val = mp[a[1]] - a[2];
int j = a[0], cid = gid(j, i);
if (dp[j][cid] > dp[i][l] + a[2])
{
dp[j][cid] = dp[i][l] + a[2];
pq.push({-dp[j][cid], i, j});
}
cid = 0;
if (dp[j][cid] > dp[i][l] + val)
{
dp[j][cid] = dp[i][l] + val;
pq.push({-dp[j][cid], -1, j});
}
}
}
ans = INF;
for(long long i : dp[n]) ans = min(ans, i);
if (ans == INF)
{
cout << -1 << "\n";
}
else
{
cout << ans << "\n";
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
for (int x = 1; x <= t; x++)
{
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... |