Submission #106239

# Submission time Handle Problem Language Result Execution time Memory
106239 2019-04-17T15:57:52 Z eriksuenderhauf Cities (BOI16_cities) C++11
64 / 100
1767 ms 42044 KB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define enl printf("\n")
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pli pair<long long, int>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vli vector<pli>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const ll INF = 1e16 + 7;
const int MAXN = 2e5 + 5;
const double eps = 1e-9;
int ind[5];
ll dp[5][MAXN], dp2[5][5][MAXN];
vli adj[MAXN];
vi a;

int main() {
    memset(dp, 0x3f, sizeof dp);
    int n, k, m;
    scanf("%d %d %d", &n, &k, &m);
    if (k == 1) return !printf("0\n");
    for (int i = 0; i < k; i++) {
        ni(ind[i]);
        ind[i]--;
        a.pb(i);
    }
    for (int i = 0; i < m; i++) {
        int u, v, w;
        scanf("%d %d %d", &u, &v, &w);
        u--, v--;
        adj[u].pb({w,v});
        adj[v].pb({w,u});
    }
    for (int i = 0; i < k; i++) {
        priority_queue<pli,vli,greater<pli>> pq;
        pq.push({0,ind[i]});
        dp[i][ind[i]] = 0;
        while (!pq.empty()) {
            ll d; int u;
            tie(d, u) = pq.top(); pq.pop();
            if (dp[i][u] < d) continue;
            for (pli v: adj[u]) {
                if (dp[i][v.se] <= d + v.fi) continue;
                dp[i][v.se] = d + v.fi;
                pq.push({d + v.fi, v.se});
            }
        }
    }
    for (int i = 0; i < k; i++) {
        for (int j = i + 1; j < k; j++) {
            priority_queue<pli,vli,greater<pli>> pq;
            for (int l = 0; l < n; l++) {
                pq.push({dp[i][l]+dp[j][l], l});
                dp2[i][j][l] = dp[i][l]+dp[j][l];
            }
            while (!pq.empty()) {
                ll d; int u;
                tie(d, u) = pq.top(); pq.pop();
                if (dp2[i][j][u] < d) continue;
                for (pli v: adj[u]) {
                    if (dp2[i][j][v.se] <= d + v.fi) continue;
                    dp2[i][j][v.se] = d + v.fi;
                    pq.push({d + v.fi, v.se});
                }
            }
        }
    }
    ll ans = INF;
    switch(k) {
        case 2: {
            ans = dp[0][1];
            break;
        }
        case 3: {
            for (int i = 0; i < n; i++)
                ans = min(ans, dp[0][i]+dp[1][i]+dp[2][i]);
            break;
        }
        case 4: {
            do {
                if (a[0]>a[1]||a[2]>a[3]) continue;
                for (int i = 0; i < n; i++)
                    ans = min(ans, dp2[a[0]][a[1]][i]+dp2[a[2]][a[3]][i]);
            } while (next_permutation(a.begin(), a.end()));
            break;
        }
        case 5: {
            do {
                if (a[0]>a[1]||a[2]>a[3]) continue;
                for (int i = 0; i < n; i++)
                    ans = min(ans, dp2[a[0]][a[1]][i]+dp2[a[2]][a[3]][i]+dp[a[4]][i]);
            } while (next_permutation(a.begin(), a.end()));
            break;
        }
    }
    prl(ans);
}

Compilation message

cities.cpp: In function 'int main()':
cities.cpp:42:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d %d", &n, &k, &m);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
cities.cpp:6:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 #define ni(n) scanf("%d", &(n))
               ~~~~~^~~~~~~~~~~~
cities.cpp:45:9: note: in expansion of macro 'ni'
         ni(ind[i]);
         ^~
cities.cpp:51:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %d", &u, &v, &w);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 12928 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 747 ms 36272 KB Output is correct
2 Correct 788 ms 35828 KB Output is correct
3 Correct 432 ms 26148 KB Output is correct
4 Correct 127 ms 25332 KB Output is correct
5 Incorrect 491 ms 32532 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 19 ms 13184 KB Output is correct
2 Correct 18 ms 13184 KB Output is correct
3 Correct 17 ms 13056 KB Output is correct
4 Correct 15 ms 13056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1152 ms 38556 KB Output is correct
2 Correct 1107 ms 38112 KB Output is correct
3 Correct 744 ms 28840 KB Output is correct
4 Correct 681 ms 33640 KB Output is correct
5 Correct 233 ms 26876 KB Output is correct
6 Correct 112 ms 27484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1710 ms 42044 KB Output is correct
2 Correct 1723 ms 41716 KB Output is correct
3 Correct 1767 ms 41408 KB Output is correct
4 Correct 1197 ms 31920 KB Output is correct
5 Correct 853 ms 34928 KB Output is correct
6 Correct 262 ms 27324 KB Output is correct
7 Correct 115 ms 27384 KB Output is correct