티스토리 뷰
20010번: 악덕 영주 혜유
FT온라인 게임에서 치열한 경쟁을 통해 영주가 된 혜유는 퀘스트를 받았다. 퀘스트의 내용은 자신이 관리하고 있는 마을 사이에 교역로를 건설하여 마을 간 교류를 활성화시키는 것이다. 이때,
www.acmicpc.net
백준 소스코드 [C++] 20010 악덕 영주 혜유
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <sstream>
#include <cstdlib>
#define all(v) v.begin(), v.end()
#define pii pair<int, int>
#define make_unique(v) sort(all(v)), v.erase(unique(all(v)), v.end())
typedef long long ll;
using namespace std;
int V, E, u, v;
ll w;
ll res = 0, M = 0;
struct Edge {
ll cost;
int node[2];
Edge(int u,int v,ll cost) {
this->cost = cost;
this->node[0] = u;
this->node[1] = v;
}
bool operator<(Edge e) {
return this->cost < e.cost;
}
};
vector <Edge> edges;
vector <int> Parent, Rank;
vector <bool> chk;
vector<pair<int, ll>> tree[1000];
int findParent(int u){
if (Parent[u] == u) return u;
return Parent[u] = findParent(Parent[u]);
}
void mergeParent(int u, int v){
u = findParent(u);
v = findParent(v);
if (u == v)return;
if (Rank[u] > Rank[v]) swap(u, v);
Parent[u] = v;
if (Rank[u] == Rank[v]) Rank[v]++;
}
void dfs(int k,ll w=0) {
M = max(M, w);
for (int i = 0;i < tree[k].size();i++) {
if (chk[tree[k][i].first] == false) {
chk[tree[k][i].first] = true;
dfs(tree[k][i].first, w+tree[k][i].second);
chk[tree[k][i].first] = false;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> V >> E;
Rank.resize(V, 1);
Parent.resize(V);
for (int i = 0;i < V;i++)Parent[i] = i;
for (int i = 0;i < E;i++) {
cin >> u >> v >> w;
edges.push_back(Edge(u, v, w));
}
sort(all(edges));
for (int i = 0;i < (int)edges.size();i++) {
ll cost = edges[i].cost;
int L = edges[i].node[0];
int R = edges[i].node[1];
if (findParent(L) == findParent(R))continue;
mergeParent(L, R);
tree[L].push_back({ R, cost });
tree[R].push_back({ L, cost });
res += cost;
}
for (int i = 0;i < V;i++) {
chk.clear();
chk.resize(V);
chk[i] = true;
dfs(i);
}
cout << res << '\n' << M;
return 0;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 10021 Watering the Fields (0) | 2021.03.29 |
---|---|
백준 소스코드 [C++] 16398 행성 연결 (0) | 2021.03.29 |
백준 소스코드 [C++] 16393 Lost Map (0) | 2021.03.28 |
백준 소스코드 [C++] 14167 Moocast (0) | 2021.03.28 |
백준 소스코드 [C++] 2887 행성 터널 (0) | 2021.03.28 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- test coverage
- 에드몬드 카프 알고리즘
- rxswift
- 최단경로문제
- 다익스트라 시간복잡도
- 강한 순환 참조
- WWDC17
- 포드 풀커슨 알고리즘
- Testable
- 코딩대회
- WWDC16
- observeOn
- 부스트캠프 6기
- IOS
- mach-o
- 벨만포드 알고리즘
- State Restoration
- CompositionalLayout
- WWDC19
- CPU와 Memory
- 네트워크 플로우
- 최단경로 알고리즘
- WWDC21
- 벨만포드 시간복잡도
- 최대 매칭
- 최단경로 문제
- MeTal
- 컴퓨터 추상화
- 네트워크 유량
- HIG
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함