Submission #971173

# Submission time Handle Problem Language Result Execution time Memory
971173 2024-04-28T05:51:57 Z htphong0909 Robot (JOI21_ho_t4) C++17
0 / 100
35 ms 64084 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

vector<array<int, 4>> adj[200001];
vector<vector<array<int, 3>>> adj2[200001];
vector<int> sum[200001];
int DP[200001];
vector<int> DP2[200001];
int cnt[200001];
map<int, int> hm[200001];

int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    freopen("TEST.IN", "r", stdin);
    freopen("TEST.OUT", "w", stdout);
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        int u, v, c, w;
        cin >> u >> v >> c >> w;
        if (!hm[u].count(c)) hm[u][c] = cnt[u]++;
        if (!hm[v].count(c)) hm[v][c] = cnt[v]++;
        adj[u].push_back({v, hm[u][c], w, hm[v][c]});
        adj[v].push_back({u, hm[v][c], w, hm[u][c]});
    }
    for (int i = 1; i <= n; i++)
    {
        DP2[i].assign(cnt[i], (int)1e18);
        adj2[i].resize(cnt[i]);
        sum[i].assign(cnt[i], 0);
        for (const auto& [v, c, w, vc] : adj[i])
        {
            adj2[i][c].push_back({v, w, vc});
            sum[i][c] += w;
        }
    }
    memset(DP, 0x3f, sizeof DP);
    DP[1] = 0;
    priority_queue<array<int, 3>, vector<array<int, 3>>, greater<>> q;
    q.push({DP[1], 1, -1});
    while (!q.empty())
    {
        auto [d, u, pc] = q.top();
        q.pop();
        if (DP[u] == d && pc == -1)
        {
            for (int i = 0; i < cnt[u]; i++)
            {
                int s = sum[u][i];
                for (const auto& [v, w, vc] : adj2[u][i])
                {
                    if (DP[v] > DP[u] + min(w, s - w))
                    {
                        DP[v] = DP[u] + min(w, s - w);
                        q.push({DP[v], v, -1});
                    }
                    if (DP2[v][vc] > DP[u])
                    {
                        DP2[v][vc] = DP[u];
                        q.push({DP2[v][vc], v, vc});
                    }
                }
            }
        }
        else if (DP2[u][pc] == d && pc != -1)
        {
            int s = sum[u][pc];
            for (const auto& [v, w, vc] : adj2[u][pc])
            {
                if (DP[v] > DP2[u][pc] + s - w)
                {
                    DP[v] = DP2[u][pc] + s - w;
                    q.push({DP[v], v, -1});
                }
            }
        }
    }
    if (DP[n] > (int)1e18) cout << -1;
    else cout << DP[n];
    return 0;
}

Compilation message

Main.cpp: In function 'int32_t main()':
Main.cpp:17:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |     freopen("TEST.IN", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:18:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |     freopen("TEST.OUT", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 31 ms 64084 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 35 ms 64080 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 31 ms 64084 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -