-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBELLMAN_FORD.cpp
More file actions
108 lines (89 loc) · 2 KB
/
BELLMAN_FORD.cpp
File metadata and controls
108 lines (89 loc) · 2 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
https://www.acmicpc.net/problem/11657
CLRS와 다르게 RELAX부분에 Distance[u]의 inf조건 검사를 한다. 꼭 해야한다.
*/
#include <iostream>
#include<vector>
#include<climits>
using namespace std;
//54퍼에서땡
const int MAX = 600;
constexpr int INF = INT32_MAX;
//bool visit[MAX] = { 0, };
std::vector<std::vector<std::pair<int, int>>> Graph; // [v] index에 u,w 을 저장.
int Distance[MAX]; //S로부터 index i까지의 거리.
void RELAX(int u, int v, int w)//(Distance[u] != INF )&&
{
if((Distance[u] != INF) && (Distance[v] > Distance[u] + w))
{
Distance[v] = Distance[u] + w;
//predecessor_subgraph[v] = u;
}
}
void INITIALIZE_SINGLE_SOURCE(int s) //start std::vector<std::vector<int>> Graph ,int s
{
for (std::size_t i = 1; i < Graph.size(); i++)
{
Distance[i] = INF;
}
Distance[s] = 0;
}
bool BELLMAN_FORD(int s)
{
INITIALIZE_SINGLE_SOURCE(s);
for (std::size_t i = 1; i < Graph.size()-1; i++)// 간선 u v w = Graph[v][]
{
for (std::size_t u = 1; u < Graph.size(); u++)
{
for (std::size_t j = 0; j < Graph[u].size(); j++)
{
int v = Graph[u][j].first;
int w = Graph[u][j].second;
RELAX(u, v, w);
}
}
}
for (std::size_t u = 1; u < Graph.size(); u++)
{
for (std::size_t j = 0; j < Graph[u].size(); j++)
{
int v = Graph[u][j].first;
if ((Distance[v] > Distance[u] + Graph[u][j].second ) && Distance[u] != INF) //출력초과가 나는 지점
{
Distance[v] = -1;
return false;
}
}
}
return true;
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
int V, E, start;
std::cin >> V >> E;
Graph.resize(V + 1);
for (int i = 0, _v, _u, _w; i < E; i++)
{
std::cin >> _v >> _u >> _w;
Graph[_v].push_back(std::make_pair(_u, _w)); //간선 ,, 인접한정점 순으로 넣는다.
}
if (!BELLMAN_FORD(1))
{
cout << -1 <<'\n';
return 0;
}
for (int i = 2; i <= V; i++)
{
if (Distance[i] >= (INF - 5000000))
{
std::cout << -1 << '\n';
}
else
{
std::cout << Distance[i] << '\n';
}
}
return 0;
}