์ต๋จ ๊ฒฝ๋ก
๋ ์ ์ ์ฌ์ด์ ๊ฒฝ๋ก ์ค ๊ฐ์ ์ ๊ฐ์ค์น ํฉ์ด ์ต์์ธ ๊ฒฝ๋ก๋ฅผ ์ฐพ๋ ์๊ณ ๋ฆฌ์ฆ์ด๋ค.
1. ๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ
๊ฐ์ค์น๊ฐ ์ ์ ์์๋๋ก ๊ทธ ์ ์ ์ ์ฐ๊ฒฐ๋ ์ ์ ์ ๋ฐฉ๋ฌธํ๋ฉด์ ๊ฐ์ ๊ฐฑ์ ํ๋ ์๊ณ ๋ฆฌ์ฆ์ด๋ค.
- ์์ ๊ฐ์ค์น๋ฅผ ๊ฐ์ง ๊ฒฝ๋ก๋ ๊ตฌํ ์ ์๋ค.
- ๊ฐ์ค์น ํฉ์ ์ต๋๊ฐ์ ๊ฐ์ค์น ์ต๋๊ฐ * (์ ์ ๊ฐ์ - 1) ์ด๋ค.
1) ๋ฌธ์
https://www.acmicpc.net/problem/1753
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int INF = 1e9;
vector<pair<int, int>> graph[20001];
int dist[20001];
int main()
{
int V, E, K;
cin >> V >> E >> K;
for (int i = 1, u, v, w; i <= E; i++) {
cin >> u >> v >> w;
graph[u].push_back(make_pair(v, w));
}
for (int i = 1; i <= V; i++)
dist[i] = INF;
priority_queue<pair<int, int>> pq;
pq.push(make_pair(0, K)); // ๊ฐ์ค์น, ์ ์
dist[K] = 0;
while (!pq.empty()) {
int tv = pq.top().second;
int tw = -pq.top().first;
pq.pop();
for (int i = 0; i < graph[tv].size(); i++) {
int nv = graph[tv][i].first;
int nw = tw + graph[tv][i].second;
if (nw < dist[nv]) {
dist[nv] = nw;
pq.push(make_pair(-nw, nv));
}
}
}
for (int i = 1; i <= V; i++) {
if (dist[i] == INF)
cout << "INF\n";
else
cout << dist[i] << "\n";
}
}
https://www.acmicpc.net/problem/1504
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int INF = 1e9;
int V, E;
vector<pair<int, int>> graph[801];
long long dist[801];
void Dijkstra(int start)
{
for (int i = 1; i <= V; i++)
dist[i] = INF;
priority_queue<pair<int, int>> pq;
pq.push(make_pair(0, start));
dist[start] = 0;
while (!pq.empty()) {
int tv = pq.top().second;
int tw = -pq.top().first;
pq.pop();
for (int i = 0; i < graph[tv].size(); i++) {
int nv = graph[tv][i].first;
int nw = tw + graph[tv][i].second;
if (nw < dist[nv]) {
dist[nv] = nw;
pq.push(make_pair(-nw, nv));
}
}
}
}
int main()
{
cin >> V >> E;
for (int i = 0, a, b, c; i < E; i++) {
cin >> a >> b >> c;
graph[a].push_back({ b, c });
graph[b].push_back({ a, c });
}
int v1, v2;
cin >> v1 >> v2;
// res1: 1 -> v1 -> v2 -> v
// res2: 1 -> v2 -> v1 -> v
long long res1 = 0, res2 = 0;
Dijkstra(1);
res1 += dist[v1], res2 += dist[v2];
Dijkstra(v1);
res1 += dist[v2], res2 += dist[V];
Dijkstra(v2);
res1 += dist[V], res2 += dist[v1];
res1 = res1 < res2 ? res1 : res2;
if (res1 >= INF)
cout << -1;
else
cout << res1;
}
2. ํ๋ก์ด๋ ์์ฌ ์๊ณ ๋ฆฌ์ฆ
๊ฑฐ์ณ๊ฐ๋ ์ง์ ์ด ๋ฌ๋ผ์ง ๋๋ง๋ค ์ต์๊ฐ์ ๊ฐฑ์ ํ๋ ์๊ณ ๋ฆฌ์ฆ์ด๋ค. A→B๋ก ๊ฐ ๋, A→B๋ณด๋ค A→C→B์ ๊ฐ์ด C๋ฅผ ๊ฑฐ์ณ๊ฐ๋ ๊ฒฝ์ฐ๊ฐ ๋ ์ต์๊ฐ์ด๋ผ๋ฉด ๊ฐฑ์ ํ๋ค.
1) ๋ฌธ์
https://www.acmicpc.net/problem/11404
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int INF = 1e9;
int graph[101][101];
int main()
{
int N, M;
cin >> N >> M;
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++)
graph[i][j] = INF;
for (int i = 0, a, b, c; i < M; i++) {
cin >> a >> b >> c;
if (c < graph[a][b])
graph[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 (graph[i][k] + graph[k][j] < graph[i][j])
graph[i][j] = graph[i][k] + graph[k][j];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
if (i == j || graph[i][j] == INF)
cout << "0 ";
else
cout << graph[i][j] << " ";
}
cout << "\n";
}
}
https://www.acmicpc.net/problem/1956
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int INF = 1e9;
int graph[401][401];
int main()
{
int V, E;
cin >> V >> E;
for (int i = 1; i <= V; i++)
for (int j = 1; j <= V; j++)
graph[i][j] = INF;
for (int i = 1, a, b, c; i <= E; i++) {
cin >> a >> b >> c;
graph[a][b] = c;
}
for (int k = 1; k <= V; k++)
for (int i = 1; i <= V; i++)
for (int j = 1; j <= V; j++)
if (graph[i][k] + graph[k][j] < graph[i][j])
graph[i][j] = graph[i][k] + graph[k][j];
int ans = INF;
for (int i = 1; i <= V; i++)
ans = min(ans, graph[i][i]);
if (ans == INF)
cout << -1;
else
cout << ans;
}
3. ๋ฒจ๋ง ํฌ๋ ์๊ณ ๋ฆฌ์ฆ
- ์์ ๊ฐ์ค์น๋ฅผ ํ์ฉํ์ง๋ง ์์ ์ฌ์ดํด์ ํ์ฉํ์ง ์๋๋ค.
- ์ต๋จ ๊ฒฝ๋ก๋ฅผ ๊ตฌํ๋ ๊ณผ์ ์ N๋ฒ ๋ ๋ฐ๋ณตํ๊ณ N๋ฒ์งธ์ ์ต๋จ๊ฑฐ๋ฆฌ๊ฐ ๊ฐฑ์ ๋๋ค๋ฉด ์์ ์ฌ์ดํด์ด๋ผ๊ณ ๊ฐ์ฃผํ๋ค.
1) ๋ฌธ์
https://www.acmicpc.net/problem/11657
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int N, M;
cin >> N >> M;
vector<pair<int, int>> v[501];
for (int i = 0, A, B, C; i < M; i++) {
cin >> A >> B >> C;
v[A].push_back({ B, C });
}
long long dist[501];
fill(dist, dist + N + 1, 1e9);
dist[1] = 0;
bool flag = false;
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++) {
if (dist[j] == 1e9)
continue;
for (auto& k : v[j]) {
if (dist[j] + k.second < dist[k.first]) {
dist[k.first] = dist[j] + k.second;
if (i == N)
flag = true;
}
}
}
if (flag)
cout << -1;
else {
for (int i = 2; i <= N; i++)
printf("%lld\n", dist[i] == 1e9 ? -1 : dist[i]);
}
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int T;
cin >> T;
while (T--) {
int n, m, w;
cin >> n >> m >> w;
vector<pair<int, int>> v[501];
for (int i = 0; i < m; ++i) {
int s, e, t;
cin >> s >> e >> t;
v[s].push_back({ e, t });
v[e].push_back({ s, t });
}
for (int i = 0; i < w; ++i) {
int s, e, t;
cin >> s >> e >> t;
v[s].push_back({ e, -t });
}
int dist[501];
fill(dist, dist + 501, 1e9);
dist[1] = 0;
bool flag = false;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
for (auto& k : v[j]) {
if (dist[j] + k.second < dist[k.first]) {
dist[k.first] = dist[j] + k.second;
if (i == n)
flag = true;
}
}
}
cout << (flag == true ? "YES\n" : "NO\n");
}
}