티스토리 뷰
https://www.acmicpc.net/problem/1865
백준 소스코드 [C++] 1865 웜홀
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
typedef long long ll;
using namespace std;
int V, E, W, v, e, weight;
vector <pair<int, int> >adj[500];
void bellman(int src) {
vector <ll> srctodesti(V, 987654321);
srctodesti[src] = 0;
bool check = false;
for (int iter = 0;iter < V;iter++) {
check = false;
for (int here = 0;here < V;here++) {
for (int num = 0;num < (int)adj[here].size();num++) {
int there = adj[here][num].first;
int cost = adj[here][num].second;
if (srctodesti[there] > srctodesti[here] + cost) {
srctodesti[there] = srctodesti[here] + cost;
if (iter == V - 1)check = true;
}
}
}
}
if (check == true) cout << "YES" << '\n';
else cout << "NO" << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
cin >> V >> E >> W;
for (int i = 0;i < V;i++) adj[i].clear();
for (int i = 0;i < E;i++) {
cin >> v >> e >> weight;
adj[v - 1].push_back({ e - 1,weight });
adj[e - 1].push_back({ v - 1,weight });
}
for (int i = 0;i < W;i++) {
cin >> v >> e >> weight;
adj[v - 1].push_back({ e - 1,-weight });
}
bellman(0);
}
return 0;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 13913 숨바꼭질 4 (0) | 2020.09.03 |
---|---|
백준 소스코드 [C++] 13549 숨바꼭질 3 (0) | 2020.09.03 |
백준 소스코드 [C++] 1261 알고스팟 (0) | 2020.09.01 |
백준 소스코드 [C++] 18223 민준이와 마산 그리고 건우 (0) | 2020.09.01 |
백준 소스코드 [C++] 4485 녹색 옷 입은 애가 젤다지 (0) | 2020.09.01 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 포드 풀커슨 알고리즘
- mach-o
- WWDC16
- 네트워크 유량
- rxswift
- IOS
- State Restoration
- CPU와 Memory
- 컴퓨터 추상화
- 최대 매칭
- HIG
- 다익스트라 시간복잡도
- 최단경로 알고리즘
- WWDC21
- CompositionalLayout
- observeOn
- Testable
- WWDC17
- 네트워크 플로우
- 강한 순환 참조
- 에드몬드 카프 알고리즘
- 최단경로문제
- 코딩대회
- 최단경로 문제
- 벨만포드 알고리즘
- test coverage
- 벨만포드 시간복잡도
- MeTal
- 부스트캠프 6기
- WWDC19
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함