// IamHereForFun //
#include <bits/stdc++.h>
using namespace std;
#define LSOne(X) ((X) & -(X))
#define int long long
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;
long long dp[N];
vector<array<int, 3>> adj[N];
bool vis[N];
void solve()
{
cin >> n >> m;
for (int x = 1; x <= n; x++)
{
dp[x] = INF;
}
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});
}
priority_queue<array<long long, 3>> pq;
dp[1] = 0;
pq.push({0, -1, 1});
while (!pq.empty())
{
array<long long, 3> arr = pq.top();
pq.pop();
int i = arr[2];
if(vis[i]) continue;
vis[i] = true;
map<int, long long> mp;
for(array<int, 3> a : adj[i])
{
if(a[0] == arr[1]) continue;
mp[a[1]] += a[2];
}
for(array<int, 3> a : adj[i])
{
if(a[0] == arr[1]) continue;
long long val = mp[a[1]] - a[2];
int j = a[0];
if(dp[j] > dp[i] + a[2])
{
dp[j] = dp[i] + a[2];
pq.push({-dp[j], i, j});
}
if(dp[j] > dp[i] + val)
{
dp[j] = dp[i] + val;
pq.push({-dp[j], -1, j});
}
}
}
if(dp[n] == INF)
{
cout << -1 << "\n";
}
else
{
cout << dp[n] << "\n";
}
}
signed main()
{
freopen("CP.INP", "r", stdin);
freopen("CP.OUT", "w", stdout);
// freopen("art2.in", "r", stdin);
// freopen("art2.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
for (int x = 1; x <= t; x++)
{
solve();
}
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:89:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | freopen("CP.INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:90:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | freopen("CP.OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |