Submission #106245

# Submission time Handle Problem Language Result Execution time Memory
106245 2019-04-17T16:07:12 Z eriksuenderhauf Cities (BOI16_cities) C++11
100 / 100
1461 ms 37816 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 = 1e18 + 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][ind[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]||a[0]>a[2]) 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]||a[0]>a[2]) 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 Correct 14 ms 12928 KB Output is correct
2 Correct 13 ms 12920 KB Output is correct
3 Correct 13 ms 12972 KB Output is correct
4 Correct 13 ms 12928 KB Output is correct
5 Correct 12 ms 12928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 684 ms 32008 KB Output is correct
2 Correct 723 ms 31484 KB Output is correct
3 Correct 438 ms 23988 KB Output is correct
4 Correct 136 ms 21916 KB Output is correct
5 Correct 481 ms 28308 KB Output is correct
6 Correct 109 ms 25336 KB Output is correct
7 Correct 16 ms 13184 KB Output is correct
8 Correct 17 ms 13056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 13056 KB Output is correct
2 Correct 17 ms 13184 KB Output is correct
3 Correct 16 ms 13056 KB Output is correct
4 Correct 16 ms 13056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1149 ms 34544 KB Output is correct
2 Correct 990 ms 33900 KB Output is correct
3 Correct 706 ms 26540 KB Output is correct
4 Correct 613 ms 29376 KB Output is correct
5 Correct 253 ms 23076 KB Output is correct
6 Correct 127 ms 24076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1461 ms 37816 KB Output is correct
2 Correct 1379 ms 37636 KB Output is correct
3 Correct 1438 ms 37168 KB Output is correct
4 Correct 1040 ms 29536 KB Output is correct
5 Correct 774 ms 30624 KB Output is correct
6 Correct 292 ms 23524 KB Output is correct
7 Correct 138 ms 24056 KB Output is correct