Submission #1075433

# Submission time Handle Problem Language Result Execution time Memory
1075433 2024-08-26T06:01:32 Z vjudge1 Cities (BOI16_cities) C++17
14 / 100
1945 ms 44744 KB
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
// #define double long double
 
#define endl '\n'
#define fastIO ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define setmin(x, y) x = min((x), (y))
#define setmax(x, y) x = max((x), (y))
#define sqr(x) ((x) * (x))
#define fi first
#define se second
#define all(x) x.begin(), x.end()
 
mt19937 hdp(chrono::high_resolution_clock::now().time_since_epoch().count());
int rand(int l, int r){return l + ((hdp() % (r - l + 1)) + r - l + 1) % (r - l + 1);}

const int NM = 1e5 + 5;
const int inf = 1e18 + 5;

vector<pair<int, int>> g[NM];
int n, k, m, dp[NM][32], imp[5];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;

void solve()
{
    cin >> n >> k >> m;
    for (int i = 0; i < k; i++)
        cin >> imp[i];
    while (m--)
    {
        int u, v, w;
        cin >> u >> v >> w;
        g[u].emplace_back(v, w);
        g[v].emplace_back(u, w);
    }
    for (int msk = 1; msk < (1 << k); msk++)
    {
        for (int i = 1; i <= n; i++)
            dp[i][msk] = inf;
        if (__builtin_popcount(msk) == 1)
            dp[imp[__lg(msk)]][msk] = 0;
        for (int u = 1; u <= n; u++)
            for (int smsk = (msk - 1) & msk; smsk; smsk = (smsk - 1) & msk)
                dp[u][msk] = dp[u][smsk] + dp[u][msk ^ smsk];
        for (int u = 1; u <= n; u++)
            pq.push({dp[u][msk], u});
        while (!pq.empty())
        {
            auto t = pq.top();
            pq.pop();
            if (dp[t.se][msk] != t.fi)
                continue;
            for (auto v : g[t.se])
                if (dp[v.fi][msk] > t.fi + v.se)
                {
                    dp[v.fi][msk] = t.fi + v.se;
                    pq.push({dp[v.fi][msk], v.fi});
                }
        }
    }
    int res = inf;
    for (int i = 1; i <= n; i++)
        setmin(res, dp[i][(1 << k) - 1]);
    cout << res;
}

signed main()
{
    fastIO
    if (fopen("in.txt", "r")) 
    {
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    }
    int tc = 1;
    // cin >> tc;
    // pre();
    while (tc--)
        solve();
}

Compilation message

cities.cpp: In function 'int main()':
cities.cpp:75:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |         freopen("in.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
cities.cpp:76:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |         freopen("out.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2648 KB Output is correct
2 Correct 2 ms 2648 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2824 KB Output is correct
5 Incorrect 1 ms 2652 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 470 ms 44520 KB Output is correct
2 Correct 481 ms 43920 KB Output is correct
3 Correct 300 ms 36548 KB Output is correct
4 Correct 43 ms 14172 KB Output is correct
5 Correct 243 ms 44488 KB Output is correct
6 Correct 39 ms 14012 KB Output is correct
7 Correct 4 ms 3084 KB Output is correct
8 Correct 3 ms 3160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 3164 KB Output is correct
2 Incorrect 5 ms 3092 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1000 ms 44744 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1841 ms 44512 KB Output is correct
2 Incorrect 1945 ms 44632 KB Output isn't correct
3 Halted 0 ms 0 KB -