#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll INF = 1e18;
constexpr int MAXN = 1e5 + 5;
constexpr int MAXC = 2e5 + 5;
int n, m;
vector<array<int, 3>> graph[MAXN];
ll dist[MAXN], minDist[MAXC], groupSum[MAXC];
bool visited[MAXN];
void dijkstra_with_group_costs()
{
fill(dist + 1, dist + n + 1, INF);
fill(visited + 1, visited + n + 1, false);
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;
dist[1] = 0;
pq.emplace(0, 1);
while (!pq.empty())
{
auto [curDist, u] = pq.top();
pq.pop();
if (visited[u])
continue;
visited[u] = true;
// Clear group data
for (auto &[v, group, cost] : graph[u])
{
groupSum[group] = 0;
minDist[group] = INF;
}
// First pass: gather group info
for (auto &[v, group, cost] : graph[u])
{
groupSum[group] += cost;
minDist[group] = min(minDist[group], dist[v]);
}
// Second pass: try relaxing edges
for (auto &[v, group, cost] : graph[u])
{
ll alt = min({dist[u] + cost,
dist[u] + groupSum[group] - cost,
groupSum[group] + minDist[group] - cost});
if (alt < dist[v])
{
dist[v] = alt;
pq.emplace(alt, v);
}
}
}
printf("%lld\n", (dist[n] == INF ? -1 : dist[n]));
}
int main()
{
scanf("%d %d", &n, &m);
for (int i = 0; i < m; ++i)
{
int u, v, cost, group;
scanf("%d %d %d %d", &u, &v, &cost, &group);
graph[u].push_back({v, group, cost});
graph[v].push_back({u, group, cost});
}
dijkstra_with_group_costs();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:66:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:70:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | scanf("%d %d %d %d", &u, &v, &cost, &group);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |