์ต๋จ ๊ฒฝ๋ก
๋ ์ ์ ์ฌ์ด์ ๊ฒฝ๋ก ์ค ๊ฐ์ ์ ๊ฐ์ค์น ํฉ์ด ์ต์์ธ ๊ฒฝ๋ก๋ฅผ ์ฐพ๋ ์๊ณ ๋ฆฌ์ฆ์ด๋ค.
1. ๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ
๊ฐ์ค์น๊ฐ ์ ์ ์์๋๋ก ๊ทธ ์ ์ ์ ์ฐ๊ฒฐ๋ ์ ์ ์ ๋ฐฉ๋ฌธํ๋ฉด์ ๊ฐ์ ๊ฐฑ์ ํ๋ ์๊ณ ๋ฆฌ์ฆ์ด๋ค.
- ์์ ๊ฐ์ค์น๋ฅผ ๊ฐ์ง ๊ฒฝ๋ก๋ ๊ตฌํ ์ ์๋ค.
- ๊ฐ์ค์น ํฉ์ ์ต๋๊ฐ์ ๊ฐ์ค์น ์ต๋๊ฐ * (์ ์ ๊ฐ์ - 1) ์ด๋ค.
1) ๋ฌธ์
https://www.acmicpc.net/problem/1753
1753๋ฒ: ์ต๋จ๊ฒฝ๋ก
์ฒซ์งธ ์ค์ ์ ์ ์ ๊ฐ์ V์ ๊ฐ์ ์ ๊ฐ์ E๊ฐ ์ฃผ์ด์ง๋ค. (1≤V≤20,000, 1≤E≤300,000) ๋ชจ๋ ์ ์ ์๋ 1๋ถํฐ V๊น์ง ๋ฒํธ๊ฐ ๋งค๊ฒจ์ ธ ์๋ค๊ณ ๊ฐ์ ํ๋ค. ๋์งธ ์ค์๋ ์์ ์ ์ ์ ๋ฒํธ K(1≤K≤V)๊ฐ ์ฃผ์ด์ง๋ค. ์ ์งธ ์ค๋ถํฐ E๊ฐ์ ์ค์ ๊ฑธ์ณ ๊ฐ ๊ฐ์ ์ ๋ํ๋ด๋ ์ธ ๊ฐ์ ์ ์ (u, v, w)๊ฐ ์์๋๋ก ์ฃผ์ด์ง๋ค. ์ด๋ u์์ v๋ก ๊ฐ๋ ๊ฐ์ค์น w์ธ ๊ฐ์ ์ด ์กด์ฌํ๋ค๋ ๋ป์ด๋ค. u์ v๋ ์๋ก ๋ค๋ฅด๋ฉฐ w๋ 10 ์ดํ์ ์์ฐ์์ด๋ค. ์๋ก ๋ค๋ฅธ ๋
www.acmicpc.net
#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
1504๋ฒ: ํน์ ํ ์ต๋จ ๊ฒฝ๋ก
์ฒซ์งธ ์ค์ ์ ์ ์ ๊ฐ์ N๊ณผ ๊ฐ์ ์ ๊ฐ์ E๊ฐ ์ฃผ์ด์ง๋ค. (2 ≤ N ≤ 800, 0 ≤ E ≤ 200,000) ๋์งธ ์ค๋ถํฐ E๊ฐ์ ์ค์ ๊ฑธ์ณ์ ์ธ ๊ฐ์ ์ ์ a, b, c๊ฐ ์ฃผ์ด์ง๋๋ฐ, a๋ฒ ์ ์ ์์ b๋ฒ ์ ์ ๊น์ง ์๋ฐฉํฅ ๊ธธ์ด ์กด์ฌํ๋ฉฐ, ๊ทธ ๊ฑฐ๋ฆฌ๊ฐ c๋ผ๋ ๋ป์ด๋ค. (1 ≤ c ≤ 1,000) ๋ค์ ์ค์๋ ๋ฐ๋์ ๊ฑฐ์ณ์ผ ํ๋ ๋ ๊ฐ์ ์๋ก ๋ค๋ฅธ ์ ์ ๋ฒํธ v1๊ณผ v2๊ฐ ์ฃผ์ด์ง๋ค. (v1 ≠ v2, v1 ≠ N, v2 ≠ 1)
www.acmicpc.net
#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
11404๋ฒ: ํ๋ก์ด๋
์ฒซ์งธ ์ค์ ๋์์ ๊ฐ์ n(1 ≤ n ≤ 100)์ด ์ฃผ์ด์ง๊ณ ๋์งธ ์ค์๋ ๋ฒ์ค์ ๊ฐ์ m(1 ≤ m ≤ 100,000)์ด ์ฃผ์ด์ง๋ค. ๊ทธ๋ฆฌ๊ณ ์ ์งธ ์ค๋ถํฐ m+2์ค๊น์ง ๋ค์๊ณผ ๊ฐ์ ๋ฒ์ค์ ์ ๋ณด๊ฐ ์ฃผ์ด์ง๋ค. ๋จผ์ ์ฒ์์๋ ๊ทธ ๋ฒ์ค์ ์ถ๋ฐ ๋์์ ๋ฒํธ๊ฐ ์ฃผ์ด์ง๋ค. ๋ฒ์ค์ ์ ๋ณด๋ ๋ฒ์ค์ ์์ ๋์ a, ๋์ฐฉ ๋์ b, ํ ๋ฒ ํ๋๋ฐ ํ์ํ ๋น์ฉ c๋ก ์ด๋ฃจ์ด์ ธ ์๋ค. ์์ ๋์์ ๋์ฐฉ ๋์๊ฐ ๊ฐ์ ๊ฒฝ์ฐ๋ ์๋ค. ๋น์ฉ์ 100,000๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ ์์ฐ์์ด๋ค. ์์
www.acmicpc.net
#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
1956๋ฒ: ์ด๋
์ฒซ์งธ ์ค์ V์ E๊ฐ ๋น์นธ์ ์ฌ์ด์ ๋๊ณ ์ฃผ์ด์ง๋ค. (2<=V<=400, 0<=E<=V*(V-1)) ๋ค์ E๊ฐ์ ์ค์๋ ๊ฐ๊ฐ ์ธ ๊ฐ์ ์ ์ a, b, c๊ฐ ์ฃผ์ด์ง๋ค. a๋ฒ ๋ง์์์ b๋ฒ ๋ง์๋ก ๊ฐ๋ ๊ฑฐ๋ฆฌ๊ฐ c์ธ ๋๋ก๊ฐ ์๋ค๋ ์๋ฏธ์ด๋ค.
www.acmicpc.net
#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
11657๋ฒ: ํ์๋จธ์
์ฒซ์งธ ์ค์ ๋์์ ๊ฐ์ N (1 ≤ N ≤ 500), ๋ฒ์ค ๋ ธ์ ์ ๊ฐ์ M (1 ≤ M ≤ 6,000)์ด ์ฃผ์ด์ง๋ค. ๋์งธ ์ค๋ถํฐ M๊ฐ์ ์ค์๋ ๋ฒ์ค ๋ ธ์ ์ ์ ๋ณด A, B, C (1 ≤ A, B ≤ N, -10,000 ≤ C ≤ 10,000)๊ฐ ์ฃผ์ด์ง๋ค.
www.acmicpc.net
#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]);
}
}
1865๋ฒ: ์ํ
์ฒซ ๋ฒ์งธ ์ค์๋ ํ ์คํธ์ผ์ด์ค์ ๊ฐ์ TC(1 ≤ TC ≤ 5)๊ฐ ์ฃผ์ด์ง๋ค. ๊ทธ๋ฆฌ๊ณ ๋ ๋ฒ์งธ ์ค๋ถํฐ TC๊ฐ์ ํ ์คํธ์ผ์ด์ค๊ฐ ์ฐจ๋ก๋ก ์ฃผ์ด์ง๋๋ฐ ๊ฐ ํ ์คํธ์ผ์ด์ค์ ์ฒซ ๋ฒ์งธ ์ค์๋ ์ง์ ์ ์ N(1 ≤ N ≤ 500),
www.acmicpc.net
#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");
}
}