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
- 수학
- 그래프
- 트리
- 스택
- 알고리즘
- 우선순위 큐
- 백준
- 브루트포스
- ue5
- 구현
- 그리디 알고리즘
- 시뮬레이션
- 자료구조
- 백트래킹
- 문자열
- 정렬
- 누적 합
- 다이나믹 프로그래밍
- Team Fortress 2
- c++
- DFS
- 유니온 파인드
- 투 포인터
- 재귀
- BFS
- VR
- Unreal Engine 5
- 유니티
- 다익스트라
- XR Interaction Toolkit
Archives
- Today
- Total
1일1알
백준 1865번 웜홀 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;
const int INF = 987654321;
struct node {
int s;
int e;
int t;
};
vector<node> nodes;
bool IsPossibleReduceTime(int n) {
vector<int> dist(n + 1, INF);
dist[1] = 0;
for (int i = 1; i < n; i++) {
for (int j = 0; j < nodes.size(); j++) {
int s = nodes[j].s;
int e = nodes[j].e;
int t = nodes[j].t;
if (dist[e] > dist[s] + t) {
dist[e] = dist[s] + t;
}
}
}
for (int j = 0; j < nodes.size(); j++) {
int s = nodes[j].s;
int e = nodes[j].e;
int t = nodes[j].t;
if (dist[e] > dist[s] + t) {
return true;
}
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
int n, m, w;
cin >> n >> m >> w;
for (int i = 0; i < m; i++) {
int s, e, t;
cin >> s >> e >> t;
nodes.push_back({ s,e,t });
nodes.push_back({ e,s,t });
}
for (int i = 0; i < w; i++) {
int s, e, t;
cin >> s >> e >> t;
nodes.push_back({ s,e,-t });
}
bool res = IsPossibleReduceTime(n);
if (res) cout << "YES";
else cout << "NO";
cout << "\n";
nodes.clear();
}
};
'알고리즘' 카테고리의 다른 글
백준 1918번 후위 표기식 C++ (0) | 2022.06.21 |
---|---|
백준 1167 트리의 지름 C++ (0) | 2022.06.20 |
백준 11404번 플로이드 C++ (0) | 2022.06.18 |
백준 1967번 트리의 지름 C++ (0) | 2022.06.17 |
백준 1753번 최단경로 C++ (0) | 2022.06.16 |