티스토리 뷰
14950번: 정복자
서강 나라는 N개의 도시와 M개의 도로로 이루어졌다. 모든 도시의 쌍에는 그 도시를 연결하는 도로로 구성된 경로가 있다. 각 도로는 양방향 도로이며, 각 도로는 사용하는데 필요한 비용이 존재
www.acmicpc.net
백준 소스코드 [C++] 14950 정복자
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
#define all(v) v.begin(), v.end()
#define pii pair<int,int>
typedef long long ll;
using namespace std;
int n, m,u,v,w,t;
vector<pii> adj[100000];
bool checked[100000];
priority_queue<pii,vector<pii>,greater<pii>> pq;
vector<int> res;
vector<int> prim() {
checked[0] = true;
for (auto there : adj[0])
if (checked[there.second] == false)
pq.push(there);
while (pq.empty() != true) {
int there = pq.top().second;
int cost = pq.top().first;
pq.pop();
if (checked[there] == true) continue;
checked[there] = true;
res.push_back(cost);
for (auto there : adj[there])
if (checked[there.second] == false)
pq.push(there);
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m>>t;
for (int i = 0;i < m;i++) {
cin >> u >> v >> w;
adj[u - 1].push_back({w,v-1 });
adj[v - 1].push_back({w,u-1 });
}
vector<int> res = prim();
sort(all(res));
int ret = 0;
for (int i = 0;i < (int)res.size();i++)ret += res[i]+t*i;
cout << ret;
return 0;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 11049 행렬 곱셈 순서 (0) | 2020.12.10 |
---|---|
백준 소스코드 [C++] 12865 평범한 배낭 (0) | 2020.12.10 |
백준 소스코드 [C++] 1647 도시 분할 계획 (0) | 2020.11.26 |
백준 소스코드 [C++] 11869 님블 (0) | 2020.11.26 |
백준 소스코드 [C++] 11868 님 게임 2 (0) | 2020.11.25 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 다익스트라 시간복잡도
- 코딩대회
- Testable
- HIG
- WWDC17
- observeOn
- WWDC21
- mach-o
- 벨만포드 알고리즘
- CPU와 Memory
- 부스트캠프 6기
- 컴퓨터 추상화
- MeTal
- test coverage
- IOS
- 포드 풀커슨 알고리즘
- 강한 순환 참조
- 네트워크 유량
- 최단경로 문제
- WWDC19
- 벨만포드 시간복잡도
- 최대 매칭
- rxswift
- State Restoration
- 네트워크 플로우
- WWDC16
- 최단경로문제
- 에드몬드 카프 알고리즘
- CompositionalLayout
- 최단경로 알고리즘
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함