Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- VR
- 수학
- 그래프
- 스택
- 유니티
- ue5
- 백준
- XR Interaction Toolkit
- 투 포인터
- 알고리즘
- 재귀
- 트리
- 브루트포스
- Unreal Engine 5
- 자료구조
- 다이나믹 프로그래밍
- c++
- 유니온 파인드
- 문자열
- 구현
- 우선순위 큐
- 다익스트라
- 그리디 알고리즘
- 시뮬레이션
- BFS
- 누적 합
- Team Fortress 2
- DFS
- 정렬
- 백트래킹
Archives
- Today
- Total
1일1알
백준 16958번 텔레포트 C++ 본문
먼저 2중 for문으로 한 도시에서 다른 모든 도시까지 가는 거리들을 텔레포트도 고려하여서 구하고,
플로이드-와샬 알고리즘으로 최소 거리를 구했다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
struct city {
city() {};
city(int s, int x, int y) :s(s), x(x), y(y) {};
int s;
int x;
int y;
};
int n, t, m;
vector<city> v(1001);
vector<vector<int>> dists(1001, vector<int>(1001, 987654321));
int GetDist(int start, int end) {
int ret;
if (v[start].s == 1 && v[end].s == 1) {
ret = min(abs(v[start].x - v[end].x) + abs(v[start].y - v[end].y), t);
}
else {
ret = abs(v[start].x - v[end].x) + abs(v[start].y - v[end].y);
}
return ret;
}
void GetMinDist() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == j) continue;
dists[i][j] = GetDist(i, j);
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 1; k <= n; k++) {
if (j == k) continue;
if (dists[j][k] > dists[j][i] + dists[i][k]) {
dists[j][k] = dists[j][i] + dists[i][k];
}
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> t;
for (int i = 1; i <= n; i++) {
int s, x, y;
cin >> s >> x >> y;
v[i] = city(s, x, y);
}
GetMinDist();
cin >> m;
for (int i = 0; i < m; i++) {
int start, end;
cin >> start >> end;
cout << dists[start][end] << "\n";
}
};
'알고리즘' 카테고리의 다른 글
백준 3019번 테트리스 C++ (0) | 2022.01.21 |
---|---|
백준 16925번 문자열 추측 C++ (0) | 2022.01.20 |
백준 16945번 매직 스퀘어로 변경하기 C++ (0) | 2022.01.18 |
백준 16943번 숫자 재배치 C++ (0) | 2022.01.17 |
백준 16922번 로마 숫자 만들기 C++ (0) | 2022.01.16 |