๐Ÿ•๏ธ ICPC Sinchon/Shortest Path

[BOJ G3][C++] ๋ฐฑ์ค€ 1865๋ฒˆ: ์›œํ™€

์„ ๋‹ฌ 2022. 11. 3. 17:31
๋ฐ˜์‘ํ˜•

https://www.acmicpc.net/problem/1865

 

1865๋ฒˆ: ์›œํ™€

์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ํ…Œ์ŠคํŠธ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ TC(1 ≤ TC ≤ 5)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ TC๊ฐœ์˜ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๊ฐ€ ์ฐจ๋ก€๋กœ ์ฃผ์–ด์ง€๋Š”๋ฐ ๊ฐ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค์˜ ์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ์ง€์ ์˜ ์ˆ˜ N(1 ≤ N ≤ 500),

www.acmicpc.net

 

๋ฌธ์ œ

๋•Œ๋Š” 2020๋…„, ๋ฐฑ์ค€์ด๋Š” ์›”๋“œ๋‚˜๋ผ์˜ ํ•œ ๊ตญ๋ฏผ์ด๋‹ค. ์›”๋“œ๋‚˜๋ผ์—๋Š” N๊ฐœ์˜ ์ง€์ ์ด ์žˆ๊ณ  N๊ฐœ์˜ ์ง€์  ์‚ฌ์ด์—๋Š” M๊ฐœ์˜ ๋„๋กœ์™€ W๊ฐœ์˜ ์›œํ™€์ด ์žˆ๋‹ค. (๋‹จ ๋„๋กœ๋Š” ๋ฐฉํ–ฅ์ด ์—†์œผ๋ฉฐ ์›œํ™€์€ ๋ฐฉํ–ฅ์ด ์žˆ๋‹ค.) ์›œํ™€์€ ์‹œ์ž‘ ์œ„์น˜์—์„œ ๋„์ฐฉ ์œ„์น˜๋กœ ๊ฐ€๋Š” ํ•˜๋‚˜์˜ ๊ฒฝ๋กœ์ธ๋ฐ, ํŠน์ดํ•˜๊ฒŒ๋„ ๋„์ฐฉ์„ ํ•˜๊ฒŒ ๋˜๋ฉด ์‹œ์ž‘์„ ํ•˜์˜€์„ ๋•Œ๋ณด๋‹ค ์‹œ๊ฐ„์ด ๋’ค๋กœ ๊ฐ€๊ฒŒ ๋œ๋‹ค. ์›œํ™€ ๋‚ด์—์„œ๋Š” ์‹œ๊ณ„๊ฐ€ ๊ฑฐ๊พธ๋กœ ๊ฐ„๋‹ค๊ณ  ์ƒ๊ฐํ•˜์—ฌ๋„ ์ข‹๋‹ค.

์‹œ๊ฐ„ ์—ฌํ–‰์„ ๋งค์šฐ ์ข‹์•„ํ•˜๋Š” ๋ฐฑ์ค€์ด๋Š” ํ•œ ๊ฐ€์ง€ ๊ถ๊ธˆ์ฆ์— ๋น ์กŒ๋‹ค. ํ•œ ์ง€์ ์—์„œ ์ถœ๋ฐœ์„ ํ•˜์—ฌ์„œ ์‹œ๊ฐ„์—ฌํ–‰์„ ํ•˜๊ธฐ ์‹œ์ž‘ํ•˜์—ฌ ๋‹ค์‹œ ์ถœ๋ฐœ์„ ํ•˜์˜€๋˜ ์œ„์น˜๋กœ ๋Œ์•„์™”์„ ๋•Œ, ์ถœ๋ฐœ์„ ํ•˜์˜€์„ ๋•Œ๋ณด๋‹ค ์‹œ๊ฐ„์ด ๋˜๋Œ์•„๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ๊ถ๊ธˆํ•ด์กŒ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์€ ๋ฐฑ์ค€์ด๋ฅผ ๋„์™€ ์ด๋Ÿฐ ์ผ์ด ๊ฐ€๋Šฅํ•œ์ง€ ๋ถˆ๊ฐ€๋Šฅํ•œ์ง€ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์—ฌ๋ผ.

์ž…๋ ฅ

์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ํ…Œ์ŠคํŠธ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ TC(1 ≤ TC ≤ 5)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ TC๊ฐœ์˜ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๊ฐ€ ์ฐจ๋ก€๋กœ ์ฃผ์–ด์ง€๋Š”๋ฐ ๊ฐ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค์˜ ์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ์ง€์ ์˜ ์ˆ˜ N(1 ≤ N ≤ 500), ๋„๋กœ์˜ ๊ฐœ์ˆ˜ M(1 ≤ M ≤ 2500), ์›œํ™€์˜ ๊ฐœ์ˆ˜ W(1 ≤ W ≤ 200)์ด ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ M+1๋ฒˆ์งธ ์ค„์— ๋„๋กœ์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง€๋Š”๋ฐ ๊ฐ ๋„๋กœ์˜ ์ •๋ณด๋Š” S, E, T ์„ธ ์ •์ˆ˜๋กœ ์ฃผ์–ด์ง„๋‹ค. S์™€ E๋Š” ์—ฐ๊ฒฐ๋œ ์ง€์ ์˜ ๋ฒˆํ˜ธ, T๋Š” ์ด ๋„๋กœ๋ฅผ ํ†ตํ•ด ์ด๋™ํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์„ ์˜๋ฏธํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  M+2๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ M+W+1๋ฒˆ์งธ ์ค„๊นŒ์ง€ ์›œํ™€์˜ ์ •๋ณด๊ฐ€ S, E, T ์„ธ ์ •์ˆ˜๋กœ ์ฃผ์–ด์ง€๋Š”๋ฐ S๋Š” ์‹œ์ž‘ ์ง€์ , E๋Š” ๋„์ฐฉ ์ง€์ , T๋Š” ์ค„์–ด๋“œ๋Š” ์‹œ๊ฐ„์„ ์˜๋ฏธํ•œ๋‹ค. T๋Š” 10,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜ ๋˜๋Š” 0์ด๋‹ค.

๋‘ ์ง€์ ์„ ์—ฐ๊ฒฐํ•˜๋Š” ๋„๋กœ๊ฐ€ ํ•œ ๊ฐœ๋ณด๋‹ค ๋งŽ์„ ์ˆ˜๋„ ์žˆ๋‹ค. ์ง€์ ์˜ ๋ฒˆํ˜ธ๋Š” 1๋ถ€ํ„ฐ N๊นŒ์ง€ ์ž์—ฐ์ˆ˜๋กœ ์ค‘๋ณต ์—†์ด ๋งค๊ฒจ์ ธ ์žˆ๋‹ค.

์ถœ๋ ฅ

TC๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ ๋งŒ์•ฝ์— ์‹œ๊ฐ„์ด ์ค„์–ด๋“ค๋ฉด์„œ ์ถœ๋ฐœ ์œ„์น˜๋กœ ๋Œ์•„์˜ค๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋ฉด YES, ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉด NO๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

ํ’€์ด

์šฐ์„ .. ์ž…๋ ฅ์ด ์ƒ๋‹นํžˆ ๋ณต์žกํ•˜๊ฒŒ ๋‚˜์˜ค๋ฏ€๋กœ ์˜ˆ์ œ๋ฅผ ์ง์ ‘ ๋จธ๋ฆฌ์†์œผ๋กœ ๊ตฌํ˜„ํ•ด๋ณด๋ฉฐ ์ž…๋ ฅ ํ˜•ํƒœ๋ฅผ ์ž˜ ํŒŒ์•…ํ•˜์ž

 

๋„๋กœ,,, ์›œํ™€ ๊ฝค๋‚˜ ๋ณต์žกํ•ด๋ณด์ด์ง€๋งŒ ๊ฒฐ๋ก ์ ์œผ๋กœ๋Š”
๊ฐ€์ค‘์น˜๋ฅผ ํ•ฉํ–ˆ์„๋•Œ ์Œ์ˆ˜๊ฐ€ ๋‚˜์˜ค๋Š” "์Œ์˜์‚ฌ์ดํด" ์ด ์žˆ๋Š”์ง€๋งŒ ํ™•์ธํ•˜๋ฉด ๋œ๋‹ค

 

์Œ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ์ง€๋‹Œ ๊ฐ„์„ ์ด ์žˆ์„ ๋•Œ ์ตœ๋‹จ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ๋ฒจ๋งŒ-ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ด๋ณด๋ฉด

[๐ŸŒฒ Altu-Bitu/1101 ์ตœ๋‹จ๊ฒฝ๋กœ] - ๋ฐฑ์ค€ 11657๋ฒˆ: ํƒ€์ž„๋จธ์‹ 

์ตœ๋‹จ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๋Š” ๊ณผ์ •์—์„œ ์Œ์˜ ์‚ฌ์ดํด์„ ๋ฐœ๊ฒฌํ–ˆ์„ ๋•Œ ํ•จ์ˆ˜๋ฅผ ์ค‘๋‹จํ•ด์ค˜์•ผํ•œ๋‹ค.
์ด ๋ถ€๋ถ„์„ ์ด์šฉํ•˜์—ฌ ๋ณธ ๋ฌธ์ œ์—์„œ๋Š” ์Œ์˜์‚ฌ์ดํด์˜ ์กด์žฌ ์—ฌ๋ถ€๋งŒ ์•Œ์•„๋‚ด๋ฉด ๋œ๋‹ค.

 

๊ฝค ์˜ค๋žœ ๊ณ ๋ฏผ์„ ํ†ตํ•ด ํ’€์—ˆ๋Š”๋ฐ,,

๊ทธ ๊ณ ๋ฏผ์˜ ํ”์ ์€ ์ฝ”๋“œ ์•„๋ž˜ ์‹œํ–‰์ฐฉ์˜ค์—.. ใ… ใ… 

// Authored by : seondal
// Co-authored by : -

// #include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <tuple>

using namespace std;

typedef tuple<int, int, int> tp;

const int INF = 6e7; // ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์˜ ์ตœ๋Œ“๊ฐ’ : ๊ฐ„์„ ๊ฐฏ์ˆ˜(2500+2500+500)*์‹œ๊ฐ„(10000) = 52e6

bool isThereNegativeCycle(int n, int path, vector<tp>&edges) {
        vector<int> dist(n+1, INF);
        
        for(int i=1; i<=n; i++) {
            for(int j=0; j<path; j++) {
                int s, d, w;
                tie(s,d,w) = edges[j];
                
                int next_weight = dist[s] + w;
                if(next_weight < dist[d]) {
                    dist[d] = next_weight;
                    
                    if(i==n)
                        return true;
                }
            }
        }
    
    return false;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    
    int tc, n,m,w, s,e,t;
    cin >> tc;
    while(tc--) {
        // ์ž…๋ ฅ
        cin >> n >> m >> w;
        vector<tp> edges; // ๊ฐ„์„  ์ •๋ณด ์ €์žฅ
        for(int i=0; i<m; i++) { // ๋„๋กœ์ •๋ณด ์ž…๋ ฅ๋ฐ›๊ธฐ (์–‘๋ฐฉํ–ฅ)
            cin >> s >> e >> t;
            edges.push_back({s, e, t});
            edges.push_back({e, s, t});
        }
        for(int i=0; i<w; i++) { // ์›œํ™€์ •๋ณด ์ž…๋ ฅ๋ฐ›๊ธฐ (๋‹จ๋ฐฉํ–ฅ)
            cin >> s >> e >> t;
            edges.push_back({s, e, t*-1});
        }
        
        // ์ถœ๋ ฅ
        if(isThereNegativeCycle(n, 2*m+w, edges))
            cout << "YES\n";
        else
            cout << "NO\n";
    }
    
    return 0;
}

/*
 */

 

์‹œํ–‰์ฐฉ์˜ค 1

์˜ˆ์ œ๋Š” ์ž˜ ๋˜๋Š”๋ฐ 5% ์ •๋„์—์„œ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค ๊ฐ€ ๋œจ๋Š”๊ฒฝ์šฐ,
์‹œ์ž‘์ ์ด ๋‹จ์ ˆ๋˜์–ด์žˆ์–ด์„œ ์Œ์˜์‚ฌ์ดํด์ด ๋‚˜์˜ค์ง€ ์•Š์€ ๊ฒฝ์šฐ๋ฅผ ๊ณ ๋ คํ•˜์ž

 

์‹œํ–‰์ฐฉ์˜ค 2

์œ„ ์‹œํ–‰์ฐฉ์˜ค1์„ ๊ฒช๊ณ ,

์ฒ˜์Œ์—๋Š” ๋ชจ๋“  ์ ์„ ๋ฒจ๋งŒํฌ๋“œ(๋‹ค์ต์ŠคํŠธ๋ผ)์˜ ์‹œ์ž‘์ ์œผ๋กœ ๋Œ๋ ค๋ณด๋ฉฐ ์Œ์˜ ์‚ฌ์ดํด์„ ์ฐพ์•˜๋Š”๋ฐ
94% ์ •๋„์—์„œ ์‹œ๊ฐ„์ดˆ๊ณผ ๊ฐ€ ๋–ด๋‹ค.

// for๋ฌธ์œผ๋กœ ๋Œ๋ ค์ฃผ๊ธฐ
bool isThereNegativeCycle(int start, int n, int path, vector<tp>&edges) {
    
    while(start <= n) {
        vector<int> dist(n+1, INF);
        dist[start] = 0;
        
        for(int i=1; i<=n; i++) {
            for(int j=0; j<path; j++) {
                int s, d, w;
                tie(s,d,w) = edges[j];
                
                if(dist[s] == INF)
                    continue;
                
                int next_weight = dist[s] + w;
                if(next_weight < dist[d]) {
                    dist[d] = next_weight;
                    
                    if(i==n)
                        return true;
                }
            }
        }
        start++;
    }
    
    return false;
}
// ์žฌ๊ท€ํ•จ์ˆ˜๋กœ ๋Œ๋ ค๋ณด๊ธฐ
bool isThereNegativeCycle(int start, int n, int path, vector<tp>&edges) {
    
    if(start>n) // ๋ชจ๋“  ์ •์ ์„ ์‹œ์ž‘์ ์œผ๋กœ ํ•ด๋ดค์Œ์—๋„ ์Œ์‚ฌ์ดํด์ด ์—†๋‹ค๋ฉด
        return false;
    
    vector<int> dist(n+1, INF);
    dist[start] = 0;
    
    for(int i=1; i<=n; i++) {
        for(int j=0; j<path; j++) {
            int s, d, w;
            tie(s,d,w) = edges[j];
            
            if(dist[s] == INF)
                continue;
            
            int next_weight = dist[s] + w;
            if(next_weight < dist[d]) {
                dist[d] = next_weight;
                
                if(i==n)
                    return true;
            }
        }
    }

    return isThereNegativeCycle(start+1, n, path, edges);
}

 

์•„๋ฌด๋ž˜๋„ ๋ฐ˜๋ณต๋ฌธ์„ 3์ค‘์œผ๋กœ ๋Œ๋ฆฌ๋Š”๊ฑด ๋ฌด๋ฆฌ์˜€๋‹ค.

์‹œ๊ฐ„์„ ์–ด๋–ป๊ฒŒ ์ค„์ผ์ง€๋งŒ ๊ณ ๋ฏผ์„ ํ–ˆ๋Š”๋ฐ,,

 

์•Œ๊ณ ๋ณด๋‹ˆ ์‹œ์ž‘์ ์€ ์ค‘์š”ํ•˜์ง€ ์•Š์•˜๋‹ค (!)

 

๋ณธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋Œ๋ ค๋ณด๋ฉฐ ์Œ์˜์‚ฌ์ดํด์ด ์žˆ๋Š”์ง€๋งŒ ํ™•์ธํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์—
dist[start]=0 ์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๊ณผ์ •๋„,

dist[d]==INF ๋ฅผ ํŒ๋‹จํ•˜๋Š” ๊ณผ์ •๋„,

์ „ํ˜€ ํ•„์š”ํ•˜์ง€ ์•Š์•˜๋‹ค.

 

์ฐธ๊ณ 

https://4z7l.github.io/2021/03/04/algorithms-boj-1865.html

 

[C++ ์•Œ๊ณ ๋ฆฌ์ฆ˜] ๋ฐฑ์ค€ 1865๋ฒˆ : ์›œํ™€ - HERSTORY

๋ฒจ๋งŒํฌ๋“œ <!-- ### --> ํ’€์ด๊ณผ์ • ๋ฒจ๋งŒ-ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•ด ํ•ด๊ฒฐํ•˜์˜€๋‹ค. ์‹œ๊ฐ„์ด ์ค„์–ด๋“ค๋ฉด์„œ ์ถœ๋ฐœ ์œ„์น˜๋กœ ๋Œ์•„์˜ค๋Š” ๊ฒƒ ์„ ๋ณด๊ณ  ๋ฒจ๋งŒํฌ๋“œ๋ฅผ ๋– ์˜ฌ๋ฆฌ๊ธฐ ๋„๋กœ๋Š” ๋ฌด๋ฐฉํ–ฅ์ด๋ฏ€๋กœ ์–‘๋ฐฉํ–ฅ ๊ฐ„์„  2๊ฐœ๋ฅผ ์ถ”๊ฐ€

4z7l.github.io

 

๋ฐ˜์‘ํ˜•