# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
22647 | 2017-04-30T06:07:00 Z | 나자바봐랑(#989, SCIK, devetude) | Logistical Metropolis (KRIII5_LM) | C++ | 2000 ms | 7088 KB |
#include <stdio.h> #include <vector> #include <queue> using namespace std; class Node { public: int idx; int cost; public: Node(int idx, int cost) { this->idx = idx; this->cost = cost; } }; class Bridge { public: int from; int to; int cost; public: Bridge(int from, int to, int cost) { this->from = from; this->to = to; this->cost = cost; } bool operator<(const Bridge& b) const { return cost < b.cost ? false : true; } }; int find(int* parents, int k) { if (parents[k] == k) { return k; } return parents[k] = find(parents, parents[k]); } bool combine(int* parents, int a, int b) { int aParents = find(parents, a); int bParents = find(parents, b); if (aParents != bParents) { parents[bParents] = aParents; return true; } return false; } int main(void) { int N, M; scanf("%d %d", &N, &M); vector<Node> list[N + 1]; for(int i = 1; i <= N; i++) { int start, end, cost; scanf("%d %d %d", &start, &end, &cost); list[start].push_back(Node(end, cost)); list[end].push_back(Node(start, cost)); } for (int major = 1; major <= N; major++) { int parents[N + 1]; for (int i = 1; i <= N; i++) { parents[i] = i; } int totalCost = 0; for (Node n : list[major]) { totalCost += n.cost; parents[major] = n.idx; } priority_queue<Bridge> priorityQueue; for (int start = 1; start <= N; start++) { if (start != major) { for (Node n : list[start]) { if (n.idx != major) { priorityQueue.push(Bridge(start, n.idx, n.cost)); } } } } unsigned long V = N - list[major].size(); while (V > 1) { Bridge bridge = priorityQueue.top(); priorityQueue.pop(); if (combine(parents, bridge.from, bridge.to)) { totalCost += bridge.cost; V--; } } printf("%d\n", totalCost); } return 0; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Execution timed out | 2000 ms | 1172 KB | Execution timed out |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Execution timed out | 2000 ms | 7088 KB | Execution timed out |
2 | Halted | 0 ms | 0 KB | - |