Submission #1252012

#TimeUsernameProblemLanguageResultExecution timeMemory
1252012badge881Robot (JOI21_ho_t4)C++20
0 / 100
28 ms6472 KiB
#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;
}

Compilation message (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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...