14284번: 간선 이어가기 2
정점 n개, 0개의 간선으로 이루어진 무방향 그래프가 주어진다. 그리고 m개의 가중치 간선의 정보가 있는 간선리스트가 주어진다. 간선리스트에 있는 간선 하나씩 그래프에 추가해 나갈 것이다.
www.acmicpc.net
다익스트라
문제를 읽는 순간 s와 t를 연결하는 최소 간선? 하고 바로 스패닝 트리를 돌렸다.
근데 답이 안나온다. 예제 1번도 안나와서 이상해서 다시 읽어봤더니 다익스트라 문제인듯했다..
[백준 2211] 네트워크 복구 C++
2211번: 네트워크 복구 첫째 줄에 두 정수 N, M이 주어진다. 다음 M개의 줄에는 회선의 정보를 나타내는 세 정수 A, B, C가 주어진다. 이는 A번 컴퓨터와 B번 컴퓨터가 통신 시간이 C (1 ≤ C ≤ 10)인 회
hyeo-noo.tistory.com
이 때도 스패닝트리인 척 하는 문제에 낚였는데 이번에도 낚였다.ㅜ
다익스트라를 돌리다가 t를 만나면 지금까지의 비용을 출력하고 종료한다.
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
#define fasti ios_base::sync_with_stdio(false); cin.tie(0);
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 1e9+7
#define pii pair<int, int>
typedef long long ll;
// typedef pair<int, int> pii;
using namespace std;
int N, M, s, t;
vector<pii > adj[5001];
int dist[5001];
void input(){
cin >> N >> M;
int a, b, c;
for(int i = 0; i < M; i++){
cin >> a >> b >> c;
adj[a].push_back({c, b});
adj[b].push_back({c, a});
}
cin >> s >> t;
for(int i = 1; i <= N; i++){
dist[i] = INF;
}
}
void solve(){
priority_queue<pii, vector<pii >, greater<pii > > pq;
pq.push({0, s});
dist[s] = 0;
while(!pq.empty()){
int now = pq.top().second;
int now_cost = pq.top().first;
pq.pop();
if(now_cost > dist[now]) continue;
if(now == t){
cout << now_cost << "\n";
return;
}
for(int i = 0; i < adj[now].size(); i++){
int next = adj[now][i].second;
int next_cost = now_cost + adj[now][i].first;
if(next_cost < dist[next]){
dist[next] = next_cost;
pq.push({next_cost, next});
}
}
}
}
int main(){
fastio
input();
solve();
return 0;
}
|
cs |
'Algorithm > BOJ' 카테고리의 다른 글
[백준 17182] 우주 탐사선 (C++) (0) | 2021.08.04 |
---|---|
[백준 2026] 소풍 (C++) (0) | 2021.08.04 |
[백준 14391] 종이 조각 (C++) (2) | 2021.08.04 |
[백준 2243] 사탕상자 (C++) (0) | 2021.08.03 |
[백준 13308] 주유소 (C++) (0) | 2021.08.02 |