| 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 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 | 
                            Tags
                            
                        
                          
                          - tensorflow
 - 세그먼트 트리
 - 크루스칼
 - 알고리즘
 - back propagation
 - pytorch
 - 자바스크립트
 - dropout
 - c++
 - 이분 탐색
 - 문자열
 - dfs
 - NEXT
 - 가끔은 말로
 - 회고록
 - Overfitting
 - 2023
 - 너비 우선 탐색
 - 우선 순위 큐
 - 백트래킹
 - lazy propagation
 - 미래는_현재와_과거로
 - 플로이드 와샬
 - object detection
 - 다익스트라
 - 가끔은_말로
 - 조합론
 - BFS
 - DP
 - 분할 정복
 
                            Archives
                            
                        
                          
                          - Today
 
- Total
 
Doby's Lab
[알고리즘] 백준 12384번: 주간 미팅 (C++) 본문
https://www.acmicpc.net/problem/12834
12834번: 주간 미팅
첫째 줄에 KIST 기사단 팀원의 수 N, 장소의 수 V, 도로의 수 E가 주어진다. (N ≤ 100, V ≤ 1000, E ≤ 10000) 둘째 줄에 KIST의 위치 A와 씨알푸드의 위치 B가 주어진다. (1 ≤ A, B ≤ V) 셋째 줄에 팀원 N명의
www.acmicpc.net
간단한 다익스트라 문제다.
갈 수 없는 길이 나올 땐 -1 처리를 해주고, 모든 최단 경로를 더한 값을 출력하면 된다.
#include <iostream>
#include <queue>
#include <vector>
#include <utility>
#define MAX 1000 + 1
#define INF 987654321
#define pii pair<int, int>
using namespace std;
vector<pii> graph[MAX];
int n, v, e;
struct cmp {
	bool operator()(pii& a, pii& b) {
		return a.second > b.second;
	}
};
vector<int> dijkstra(int node) {
	vector<int> dist(v + 1, INF);
	dist[node] = 0;
	priority_queue<pii, vector<pii>, cmp> pq;
	pq.push({ node, 0 });
	while (!pq.empty()) {
		int now = pq.top().first;
		int cost = pq.top().second;
		pq.pop();
		if (dist[now] < cost) {
			continue;
		}
		for (int i = 0; i < graph[now].size(); i++) {
			int next = graph[now][i].first;
			int nextCost = graph[now][i].second;
			if (cost + nextCost < dist[next]) {
				dist[next] = cost + nextCost;
				pq.push({ next, dist[next] });
			}
		}
	}
	return dist;
}
int main() {
	cin >> n >> v >> e;
	int a, b;
	cin >> a >> b;
	vector<int> team;
	for (int i = 1, value; i <= n; i++) {
		cin >> value;
		team.push_back(value);
	}
	for (int i = 0; i < e; i++) {
		int start, end, w;
		cin >> start >> end >> w;
		graph[start].push_back({ end, w });
		graph[end].push_back({ start, w });
	}
	int sum = 0;
	for (int i = 0; i < team.size(); i++) {
		vector<int> temp = dijkstra(team[i]);
		if (temp[a] == INF) sum += -1;
		else sum += temp[a];
		if (temp[b] == INF) sum += -1;
		else sum += temp[b];
	}
	cout << sum;
	return 0;
}'PS > BOJ' 카테고리의 다른 글
| [자료구조] 백준 10999번: 구간 합 구하기 2 (C++), Lazy Propagation (0) | 2021.12.10 | 
|---|---|
| [자료구조] 백준 11505번: 구간 곱 구하기 (C++), 구간 곱 update함수 (0) | 2021.12.10 | 
| [자료구조] 백준 2268번: 수들의 합 7 (C++) (0) | 2021.12.09 | 
| [자료구조] 백준 2357번: 최솟값과 최댓값 (C++) (0) | 2021.12.09 | 
| [자료구조] 백준 10868번: 최솟값 (C++) (0) | 2021.12.09 |