#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 5;
const int M = 2e5 + 5;
const ll INF = 1e18;
struct Edge
{
int a, b, c;
};
int n, m;
vector<Edge> adj[N];
ll dp[N], col[M], sum[M];
bool vis[N];
int main()
{
scanf("%d %d", &n, &m);
for (int x = 0; x < m; x++)
{
int a, b, c, p;
scanf("%d %d %d %d", &a, &b, &c, &p);
adj[a].push_back({b, c, p});
adj[b].push_back({a, c, p});
}
for (int x = 1; x <= n; x++)
{
dp[x] = INF;
vis[x] = 0;
}
priority_queue<pair<ll, int>> pq;
pq.push({0, 1});
dp[1] = 0;
while (!pq.empty())
{
int i = pq.top().second;
pq.pop();
if (vis[i])
continue;
vis[i] = true;
for (Edge &a : adj[i])
{
sum[a.b] = 0;
col[a.b] = INF;
}
for (Edge &a : adj[i])
{
sum[a.b] += a.c;
col[a.b] = min(col[a.b], dp[a.a]);
}
for (Edge &a : adj[i])
{
ll val = min({dp[i] + a.c, dp[i] + sum[a.b] - a.c, sum[a.b] + col[a.b] - a.c});
int j = a.a;
if (val < dp[j])
{
dp[j] = val;
pq.push({-dp[j], j});
}
}
}
if (dp[n] == INF)
printf("-1\n");
else
printf("%lld\n", dp[n]);
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:21:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:25:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | scanf("%d %d %d %d", &a, &b, &c, &p);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |