# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
26259 | 2017-06-28T15:29:15 Z | sgtlaugh | Cities (BOI16_cities) | C++14 | 3023 ms | 43876 KB |
#include <stdio.h> #include <bits/stdtr1c++.h> #define MAXK 5 #define MAXN 100010 #define clr(ar) memset(ar, 0, sizeof(ar)) #define read() freopen("lol.txt", "r", stdin) #define dbg(x) cout << #x << " = " << x << endl #define ran(a, b) ((((rand() << 15) ^ rand()) % ((b) - (a) + 1)) + (a)) using namespace std; const long long INF = 1LL << 60; bitset <MAXN> visited; int n, k, m, special[MAXK]; long long dis[1 << MAXK][MAXN]; vector <pair<int, int> > graph[MAXN]; void dijkstra(long long dis[MAXN]){ int i, j, v, w; priority_queue <pair<long long, int> > PQ; for (i = 1; i <= n; i++){ visited[i] = 0; if (dis[i] < INF) PQ.push(make_pair(-dis[i], i)); } while (!PQ.empty()){ pair <long long, int> cur = PQ.top(); PQ.pop(); if (-cur.first == dis[cur.second]){ i = cur.second, visited[i] = 1; for (j = 0; j < graph[i].size(); j++){ v = graph[i][j].first, w = graph[i][j].second; if (dis[v] > dis[i] + w){ dis[v] = dis[i] + w; if (!visited[v]) PQ.push(make_pair(-dis[v], v)); } } } } } long long minimumSteinerTree(){ int i, j, mask, submask; for (mask = 1; mask < (1 << k); mask++){ for (i = 1; i <= n; i++) dis[mask][i] = INF; if (__builtin_popcount(mask) == 1){ dis[mask][special[31 - __builtin_clz(mask)]] = 0; } else{ for (i = 1; i <= n; i++){ for (submask = mask - 1; submask >= (submask ^ mask); submask = (submask - 1) & mask){ dis[mask][i] = min(dis[mask][i], dis[submask][i] + dis[submask ^ mask][i]); } } } dijkstra(dis[mask]); } return *std::min_element(dis[mask - 1] + 1, dis[mask - 1] + 1 + n); } int main(){ int i, j, u, v, w; while (scanf("%d %d %d", &n, &k, &m) != EOF){ for (i = 0; i < MAXN; i++) graph[i].clear(); for (i = 0; i < k; i++) scanf("%d", &special[i]); for (i = 0; i < m; i++){ scanf("%d %d %d", &u, &v, &w); graph[u].push_back(make_pair(v, w)); graph[v].push_back(make_pair(u, w)); } printf("%lld\n", minimumSteinerTree()); } return 0; }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 29380 KB | Output is correct |
2 | Correct | 0 ms | 29380 KB | Output is correct |
3 | Correct | 0 ms | 29380 KB | Output is correct |
4 | Correct | 3 ms | 29380 KB | Output is correct |
5 | Correct | 3 ms | 29380 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 779 ms | 43876 KB | Output is correct |
2 | Correct | 719 ms | 43808 KB | Output is correct |
3 | Correct | 359 ms | 36676 KB | Output is correct |
4 | Correct | 116 ms | 34048 KB | Output is correct |
5 | Correct | 273 ms | 41828 KB | Output is correct |
6 | Correct | 133 ms | 34068 KB | Output is correct |
7 | Correct | 6 ms | 29512 KB | Output is correct |
8 | Correct | 0 ms | 29512 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 6 ms | 29512 KB | Output is correct |
2 | Correct | 3 ms | 29512 KB | Output is correct |
3 | Correct | 3 ms | 29520 KB | Output is correct |
4 | Correct | 3 ms | 29512 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 1553 ms | 43868 KB | Output is correct |
2 | Correct | 1449 ms | 43664 KB | Output is correct |
3 | Correct | 739 ms | 36680 KB | Output is correct |
4 | Correct | 809 ms | 39284 KB | Output is correct |
5 | Correct | 253 ms | 35656 KB | Output is correct |
6 | Correct | 153 ms | 35672 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 3023 ms | 43872 KB | Output is correct |
2 | Correct | 2669 ms | 43864 KB | Output is correct |
3 | Correct | 2923 ms | 43712 KB | Output is correct |
4 | Correct | 2193 ms | 36680 KB | Output is correct |
5 | Correct | 1559 ms | 39284 KB | Output is correct |
6 | Correct | 373 ms | 35640 KB | Output is correct |
7 | Correct | 129 ms | 35536 KB | Output is correct |