728x90
๋ฐ์ํ
N๊ฐ์ ๋ ธ๋์ M๊ฐ์ ์์ง๊ฐ ์๋ ๊ทธ๋ํ์์, ์ธ ๋ ธ๋ s, a, t๊ฐ ์ฃผ์ด์ก์ ๋ s์์ a๋ฅผ ๋ฐ๋์ ์ง๋์ t๊น์ง ๋๋ฌํ๋ ๊ฐ์ฅ ์งง์ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
์ ๋ ฅ
ํ์ค ์ ๋ ฅ์ผ๋ก ์ ๋ ฅ์ ๋ฐ๋๋ค. ์ฒซ ์ค์๋ ๋ ธ๋์ ๊ฐ์ N๊ณผ ์์ง์ ๊ฐ์ M์ด ์ฃผ์ด์ง๋ค. ๋ ธ๋์ ๊ฐ์๋ 3 ์ด์ 100 ์ดํ์ด๊ณ , ์์ง์ ๊ฐ์๋ 0๊ฐ ์ด์ N(N-1)๊ฐ ์ดํ์ด๋ค.
๋๋ฒ์งธ ์ค์๋ ์ธ ์ ์ s a t๊ฐ ์ฃผ์ด์ง๋ค. ๋ ธ๋๋ค์ 0 ์ด์ N ๋ฏธ๋ง์ธ ์ ์๋ค๋ก ํํ๋๋ฉฐ, ์ด ์ธ์ ์๋ ๊ฐ๊ฐ ์์ ๋ ธ๋, ์ค๊ฐ ๋ ธ๋, ๋์ฐฉ ๋ ธ๋๋ฅผ ๋ํ๋ธ๋ค.
์ธ๋ฒ์งธ ์ค๋ถํฐ ์ด M์ค์ ์์ง ์ ๋ณด๊ฐ ์ฃผ์ด์ง๋ค. ํ ์ค์ ์์ง ํ๋์ ๋ํ ์ ๋ณด๋ฅผ ๋ํ๋ด๋ฉฐ, ์ธ ์ ์ U V W๋ก ์ด๋ฃจ์ด์ง๋ค. U๋ ์์ง์ ์์ ๋ ธ๋, V๋ ์์ง์ ๋์ฐฉ ๋ ธ๋, W๋ ์์ง์ ๊ฐ์ค์น์ด๋ค. ์ด ๊ทธ๋ํ๋ ์ ํฅ ๊ทธ๋ํ์ด๋ฉฐ ์์ง์ ๊ฐ์ค์น๋ -100 ์ด์ 100 ์ดํ์ธ ์ ์์ด๋ค. U=V, ์ฆ ์์ ์์ ์์ ์ผ๋ก ์ด์ด์ง๋ ์์ง๋ ์์ผ๋ฉฐ, ์ด ๊ทธ๋ํ์๋ ์์ ๊ฐ์ค์น๋ฅผ ๊ฐ๋ ์ฌ์ดํด์ด ์๋ค.
์ถ๋ ฅ
ํ์ค ์ถ๋ ฅ์ผ๋ก ์ถ๋ ฅํ๋ค. ํ ์ค๋ก ์ถ๋ ฅํ๊ณ , ์ด ์ค์๋ s์์ a๋ฅผ ๊ฑฐ์ณ b๋ก ๊ฐ๋ ์ต๋จ ๊ฑฐ๋ฆฌ๋ฅผ ์ถ๋ ฅํ๋ค.
์์ ์ ๋ ฅ
4 6
0 2 3
0 1 2
0 2 3
0 3 1
1 2 -1
1 3 4
2 3 2
์์ ์ถ๋ ฅ
3
bfs/dfs ๋ฌธ์ ํ๋ฒ๋ ์ ํ์ด๋ดค์ ๋ ๋์จ ๊ทธ๋ํ ๊ณผ์ ...
์ด๋๋ bfs๊ฐ ์ด๋ ค์์ ๊ทธ๋ฅ ๋ฐ๋ณต๋ฌธ์ผ๋ก ์ด์ฌํ ํ์๋ค.
๊ณผ๊ฑฐ์ ๋ฉ์ฒญํ ๋,, ๊ทธ๋๋ ๋ง์ ๋ฐ์๊ฑฐ ๋ณด๋ฉด ๊ฝค๋ ๋๋ํ์์ง๋
"์์ ๊ฐ์ค์น๋ฅผ ๊ฐ๋ '์ฌ์ดํด'์ด ์๋ค"๋ผ๊ณ ํ์ผ๋ฏ๋ก Bellman Ford ์๊ณ ๋ฆฌ์ฆ์ ์ผ๋๊ฑธ๋ก ๊ธฐ์ตํ๋ค.
s->a์ ์ต๋จ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๊ณ , a->t์ ์ต๋จ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํด์ ๋ํด์ฃผ์๋ค.
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
using namespace std;
int dist[101];
struct Edge {
int s;
int e;
int val;
Edge(int u, int v, int w) {
s = u;
e = v;
val = w;
}
};
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int n, m; // ๋
ธ๋, ์์ง
int s, a, t; // ์์, ์ค๊ฐ, ๋์ฐฉ
int u, v, w; // ์์ง ์์๋
ธ๋, ์์ง ๋์ฐฉ๋
ธ๋, ๊ฐ์ค์น
cin >> n >> m >> s >> a >> t;
vector<Edge> ed;
for (int i = 0; i < m; ++i){
cin >> u >> v >> w;
ed.push_back(Edge(u, v, w));
}
for (int i = 0; i < n+1; i++) {
dist[i] = 100000;
}
dist[s] = 0;
for (int i = 0; i < n+1; i++) {
for (int j = 0; j < ed.size(); j++) {
int start = ed[j].s;
int end = ed[j].e;
int wei = ed[j].val;
int newWei = dist[start] + wei;
if (dist[start] != 100000 && newWei < dist[end]) {
dist[end] = newWei;
}
}
}
// cout << "s_to_a = "<< dist[a] << "\n";
int s_to_a = dist[a];
if(s_to_a == 100000) { // ๊ฒฝ์ ์ง์ ๊ฐ ์ ์์ ๋ ์์ธ์ฒ๋ฆฌ
exit(0);
}
//
for (int i = 0; i < n+1; i++) {
dist[i] = 100000;
}
dist[a] = 0;
for (int i = 0; i < n+1; i++) {
for (int j = 0; j < ed.size(); j++) {
int start = ed[j].s;
int end = ed[j].e;
int wei = ed[j].val;
int newWei = dist[start] + wei;
if (dist[start] != 100000 && newWei < dist[end]) {
dist[end] = newWei;
}
}
}
// cout << "a_to_t = " << dist[t]<<"\n";
int a_to_t = dist[t];
if (a_to_t == 100000)
{ // ๊ฒฝ์ ์ง์์ ๋์ฐฉ์ ์ผ๋ก ๊ฐ ์ ์์ ๋ ์์ธ์ฒ๋ฆฌ
exit(0);
}
cout << s_to_a + a_to_t;
return 0;
}
728x90
๋ฐ์ํ
'๐ ์ ๊ณต ๊ณต๋ถ > ์๊ณ ๋ฆฌ์ฆ ํด์ ๋ฐ ์ค๊ณ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์๊ณ ๋ฆฌ์ฆ] ์คํจ ํจ์ - ์ค๋ณต ์ต๋ ๋ถ๋ถ๋ฌธ์์ด(cpp) (0) | 2023.04.11 |
---|---|
[์๊ณ ๋ฆฌ์ฆ] ๋ฌธ์์ด ๋ค๋ฃจ๊ธฐ - ๋ก๋ง์ซ์(cpp) (0) | 2023.04.11 |
[์๊ณ ๋ฆฌ์ฆ] DP - ๊ณ๋จ ์ค๋ฅด๊ธฐ(cpp) (0) | 2023.04.11 |
[์๊ณ ๋ฆฌ์ฆ] Greedy algorithm - ์ด์งํธ ๋๋์ (cpp) (1) | 2023.02.09 |
[์๊ณ ๋ฆฌ์ฆ] ์ ๋ ฌ์ ์ต์ ์ฑ (0) | 2023.01.16 |