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
- XR Interaction Toolkit
- 브루트포스
- 누적 합
- BFS
- 문자열
- 재귀
- 구현
- 유니티
- 자료구조
- 백트래킹
- Unreal Engine 5
- VR
- 다익스트라
- ue5
- Team Fortress 2
- 유니온 파인드
- 투 포인터
- 그리디 알고리즘
- 정렬
- 트리
- c++
- 수학
- 알고리즘
- DFS
- 스택
- 우선순위 큐
- 다이나믹 프로그래밍
- 시뮬레이션
- 그래프
- 백준
Archives
- Today
- Total
1일1알
백준 11404번 플로이드 C++ 본문
플로이드-와샬 알고리즘을 이용해서 쉽게 풀 수 있는 문제이다.
그리고 궁금해서 다익스트라로 모든 정점을 돌면서도 답을 구해봤는데 시간은 비슷하게 나왔다.
#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 <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
vector<vector<int>> graph;
const int INF = 987654321;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
vector<vector<int>> dist(n + 1, vector<int>(n + 1, INF));
for (int i = 1; i <= n; i++) {
dist[i][i] = 0;
}
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
dist[a][b] = min(dist[a][b], c);
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (dist[i][k] + dist[k][j] >= dist[i][j])
continue;
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (dist[i][j] == INF)
cout << 0;
else
cout << dist[i][j];
cout << " ";
}
cout << "\n";
}
};
#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 <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
const int INF = 987654321;
int n, m;
vector<vector<int>> vertexs;
struct VertexCost {
bool operator<(const VertexCost& other) const {
return cost < other.cost;
}
bool operator>(const VertexCost& other) const {
return cost > other.cost;
}
int vertex;
int cost;
};
void Dijikstra(int start) {
vector<int> best(n + 1, INF);
priority_queue<VertexCost, vector<VertexCost>, greater<VertexCost>> pq;
pq.push({ start,0 });
best[start] = 0;
while (!pq.empty()) {
auto curr = pq.top();
pq.pop();
if (curr.cost > best[curr.vertex])
continue;
for (int i = 1; i <= n; i++) {
if (vertexs[curr.vertex][i] == -1)
continue;
int nextCost = curr.cost + vertexs[curr.vertex][i];
if (nextCost >= best[i])
continue;
best[i] = nextCost;
pq.push({ i,nextCost });
}
}
for (int i = 1; i <= n; i++) {
if (best[i] == INF)
cout << 0;
else
cout << best[i];
cout << " ";
}
cout << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
vertexs = vector<vector<int>>(n + 1, vector<int>(n + 1, INF));
for (int i = 1; i <= n; i++) {
vertexs[i][i] = 0;
}
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
vertexs[a][b] = min(vertexs[a][b], c);
}
for (int i = 1; i <= n; i++) {
Dijikstra(i);
}
};
'알고리즘' 카테고리의 다른 글
백준 1167 트리의 지름 C++ (0) | 2022.06.20 |
---|---|
백준 1865번 웜홀 C++ (0) | 2022.06.19 |
백준 1967번 트리의 지름 C++ (0) | 2022.06.17 |
백준 1753번 최단경로 C++ (0) | 2022.06.16 |
백준 1504번 특정한 최단 경로 C++ (0) | 2022.06.15 |