Skip to content

Latest commit

ย 

History

History
617 lines (438 loc) ยท 24.6 KB

0728-dfs-bfs-shortest_path.md

File metadata and controls

617 lines (438 loc) ยท 24.6 KB

2020.07.28.Tue

๐Ÿ“š๊ณต๋ถ€ํ•œ ๊ฑฐ List๐Ÿ“š

  • ์ข…๋งŒ๋ถ - ๊ทธ๋ž˜ํ”„์˜ ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰ (๊ฐ„์„ )
  • ์ข…๋งŒ๋ถ - ๊ทธ๋ž˜ํ”„์˜ ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰
  • ์ข…๋งŒ๋ถ - ์ตœ๋‹จ ๊ฒฝ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜

๐ŸŒž[์ข…๋งŒ๋ถ] ๊ทธ๋ž˜ํ”„์˜ ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰๐ŸŒž

๐ŸŒด ๊ฐ„์„ ์˜ ๋ถ„๋ฅ˜ ๐ŸŒด

๊ทธ๋ž˜ํ”„์˜ ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ (ํ˜น์€ DFS Spanning Tree)๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋‚˜๋ฉด ๊ทธ๋ž˜ํ”„์˜ ๊ฐ„์„ ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ถ„๋ฅ˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

๐ŸŒฑ ํŠธ๋ฆฌ ๊ฐ„์„ (Tree Edge)

์ŠคํŒจ๋‹ ํŠธ๋ฆฌ์— ํฌํ•จ๋œ ๊ฐ„์„ 

๐ŸŒฑ ์ˆœ๋ฐฉํ–ฅ ๊ฐ„์„ (Forward Edge)

์„ ์กฐ์—์„œ ์ž์†์œผ๋กœ ์—ฐ๊ฒฐ๋˜์ง€๋งŒ, ํŠธ๋ฆฌ ๊ฐ„์„ ์ด ์•„๋‹Œ ๊ฐ„์„ (์ค‘๊ฐ„๋…ธ๋“œ๋ฅผ ๊ฑฐ์น˜๋Š” ๋‹ค๋ฅธ ๊ฒฝ๋กœ๊ฐ€ ์žˆ์œผ๋ฉด ์•ˆ๋จ)

๐ŸŒฑ์—ญ๋ฐฉํ–ฅ ๊ฐ„์„ (Back Edge)

์ž์†์—์„œ ์„ ์กฐ๋กœ ์—ฐ๊ฒฐ๋˜๋Š” ๊ฐ„์„ 

๐ŸŒฑ ๊ต์ฐจ ๊ฐ„์„ (Cross Edge)

์„ ์กฐ์™€ ์ž์† ๊ด€๊ณ„๊ฐ€ ์•„๋‹Œ ์ •์ ๋“ค ๊ฐ„์— ์—ฐ๊ฒฐ๋œ ๊ฐ„์„ 


๐ŸŒฑ ๋ฌดํ–ฅ ๊ทธ๋ž˜ํ”„ ๊ฐ„์„ ์˜ ๋ถ„๋ฅ˜

๋ฌดํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ๋Š” ๊ต์ฐจ ๊ฐ„์„ ์ด ์—†๋‹ค. ๋˜ํ•œ ์—ญ๋ฐฉํ–ฅ, ์ˆœ๋ฐฉํ–ฅ ๊ฐ„์„ ๋„ ์—†๋‹ค.

๐ŸŒฑ ์œ„์ƒ ์ •๋ ฌ์˜ ์ •๋‹น์„ฑ ์ฆ๋ช…

dfs(u)๊ฐ€ dfs(v)๋ณด๋‹ค ์ผ์ฐ ์ข…๋ฃŒํ•  ๊ฒฝ์šฐ, u์—์„œ v๋กœ ๊ฐ€๋Š” ๊ฐ„์„ ์ด ์กด์žฌํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์„ ์ฆ๋ช…ํ•˜๋ฉด ๋œ๋‹ค.

๐ŸŒฑ ์‚ฌ์ดํด ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธํ•˜๊ธฐ

์‚ฌ์ดํด ์กด์žฌ ์—ฌ๋ถ€ = ์—ญ๋ฐฉํ–ฅ ๊ฐ„์„  ์กด์žฌ ์—ฌ๋ถ€


๐ŸŒด๊ฐ„์„ ์„ ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฐฉ๋ฒ•๐ŸŒด

discovered[] : ๊ฐ ์ •์ ์„ ๋ช‡ ๋ฒˆ์งธ๋กœ ๋ฐœ๊ฒฌํ–ˆ๋Š”์ง€ ์ €์žฅ

dfs(u)์—์„œ ๊ฐ„์„  (u,v)๋ฅผ ๊ฒ€์‚ฌํ–ˆ์„ ๋•Œ,

  • v๋ฅผ ๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†๋‹ค. ๐Ÿ‘‰ ํŠธ๋ฆฌ ๊ฐ„์„ 

  • v๋ฅผ ๋ฐฉ๋ฌธํ•œ ์ ์ด ์žˆ๋‹ค.

    • v๊ฐ€ u๋ณด๋‹ค ๋Šฆ๊ฒŒ ๋ฐœ๊ฒฌ๋˜์—ˆ๋‹ค. (= v๊ฐ€ u์˜ ์ž์†) ๐Ÿ‘‰ ์ˆœ๋ฐฉํ–ฅ ๊ฐ„์„ 
    • v๊ฐ€ u๋ณด๋‹ค ์ผ์ฐ ๋ฐœ๊ฒฌ๋˜์—ˆ๋‹ค. (= v๋Š” u์˜ ์„ ์กฐ) ๐Ÿ‘‰ ์—ญ๋ฐฉํ–ฅ ๊ฐ„์„ 
      • dfs(v) ์ข…๋ฃŒ ํ›„, dfs(u) ์ข…๋ฃŒ ๐Ÿ‘‰ ๊ต์ฐจ ๊ฐ„์„ 

โŒจ๏ธ ๊ฐ„์„ ์„ ๊ตฌ๋ถ„ํ•˜๋Š” ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰์˜ ๊ตฌํ˜„

discovered[i] : i๋ฒˆ ์ •์ ์˜ ๋ฐœ๊ฒฌ์ˆœ์„œ

finished[i] : dfs(i)๊ฐ€ ์ข…๋ฃŒํ–ˆ์œผ๋ฉด 1, ์•„๋‹ˆ๋ฉด 0

vector<vector<int>> adj;//์ธ์ ‘๋ฆฌ์ŠคํŠธ
vector<int> discovered, finished;
int counter;//์ „์—ญ๋ณ€์ˆ˜
void dfs2(int here) {
  discovered[here] = counter++;//๋ฐœ๊ฒฌ์ˆœ์„œ 
  for(int i=0; i<adj[here].size(); ++i) { //here์˜ ์ธ์ ‘๋…ธ๋“œ๋งŒํผ
    int there = adj[here][i]; //๊ฐ ์ธ์ ‘๋…ธ๋“œ๋ฅผ there๋กœ ์„ค์ •ํ•˜๊ณ 
    if(discovered[there] == -1) {//there๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•œ ์  ์—†๋‹ค๋ฉด
      cout<<'tree edge'<<endl; //ํŠธ๋ฆฌ ๊ฐ„์„ ์ด๋‹ค
      dfs2(there); //there ์ธ์ ‘๋…ธ๋“œ์— ๋Œ€ํ•ด dfs์ง„ํ–‰(์žฌ๊ท€ํ˜ธ์ถœ)
    }
    //there์˜ ๋ฐœ๊ฒฌ์ˆœ์„œ ๊ฐ’์ด ๋” ํฌ๋ฉด here -> there์ˆœ์„œ์ด๋ฏ€๋กœ ์ˆœ๋ฐฉํ–ฅ ๊ฐ„์„ ์ด๋‹ค
    else if(discovered[here] < discovered[there]) cout<<"forwared edge"<<endl;
    //here์˜ ๋ฐœ๊ฒฌ์ˆœ์„œ ๊ฐ’์ด ๋” ํฌ๊ณ , dfs(there)์ด ์ข…๋ฃŒํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์—ญ๋ฐฉํ–ฅ ๊ฐ„์„ ์ด๋‹ค
    else if(finished[there] == 0) cout<<"back edge"<<endl;
    //๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ๋Š” ๋ชจ๋‘ ๊ต์ฐจ ๊ฐ„์„ ์ด๋‹ค
    else cout<<"cross edge"<<endl;
  }
  finished[here] = 1; //dfs์ข…๋ฃŒ ์‹œ,finished ๊ฐ’ ์žฌ์„ค์ •
}

๐ŸŒด ์ ˆ๋‹จ์ (Cut Vertex) ๐ŸŒด

๐ŸŒฑ ์ ˆ๋‹จ์ (cut vertex)์ด๋ž€?

์ด ์ ๊ณผ ์ธ์ ‘ํ•œ ๊ฐ„์„ ๋“ค์„ ๋ชจ๋‘ ์ง€์› ์„ ๋•Œ, ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋‘ ๊ฐœ ์ด์ƒ์œผ๋กœ ๋‚˜๋ˆ ์ง€๋Š” ์ •์ 

๐ŸŒฑ ์ ˆ๋‹จ์ ์ธ์ง€ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•

ํ•ด๋‹น ์ •์ ์„ ๊ทธ๋ž˜ํ”„์—์„œ ์‚ญ์ œ ํ›„ ์ปดํฌ๋„ŒํŠธ ๊ฐœ์ˆ˜๊ฐ€ ๋Š˜์–ด๋‚ฌ๋Š”์ง€ ํ™•์ธ ๐Ÿ‘‰ ๋น„ํšจ์œจ์ ์ž„

  1. DFS ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ๋ฅผ ๋งŒ๋“ ๋‹ค.
  2. ๋ฌดํ–ฅ ๊ทธ๋ž˜ํ”„์—๋Š” ๊ต์ฐจ๊ฐ„์„  ์กด์žฌ x, ์ฆ‰ ์–ด๋–ค ์ •์  u์™€ ์—ฐ๊ฒฐ๋œ ์ •์ ๋“ค์€ ๋ชจ๋‘ u์˜ ์„ ์กฐ ํ˜น์€ ์ž์†์ด๋‹ค.
  3. ์–ด๋–ค ์ •์  u๋ฅผ ์‚ญ์ œํ–ˆ์„ ๋•Œ, ์ปดํฌ๋„ŒํŠธ ์ˆ˜๊ฐ€ ๋Š˜์–ด๋‚˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋Š” u์˜ ์„ ์กฐ/์ž์†์ด ๋ชจ๋‘ ์—ญ๋ฐฉํ–ฅ ๊ฐ„์„ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์„ ๋•Œ ๋ฟ์ด๋‹ค.
  4. ํ˜น์€ ์–ด๋–ค ์ •์  u๊ฐ€ ๋ฃจํŠธ์ด๊ณ  u์˜ ์ž์†์ด 1์ดํ•˜์ธ ๊ฒฝ์šฐ์—๋„ ๊ทธ๋ž˜ํ”„๊ฐ€ ์ชผ๊ฐœ์ง€์ง€ ์•Š๋Š”๋‹ค. (๊ทธ๋ƒฅ ํ™€๋ผ๋‹น ์—†์–ด์ ธ๋ฒ„๋ฆฌ๊ธฐ๋•Œ๋ฌธ)

๐ŸŒฑ ์ด์ค‘ ๊ฒฐํ•ฉ ์ปดํฌ๋„ŒํŠธ(biconnected component)

๋ฌดํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ ์ ˆ๋‹จ์ ์„ ํฌํ•จํ•˜์ง€ ์•Š๋Š” ์„œ๋ธŒ๊ทธ๋ž˜ํ”„

๐Ÿ“ ์˜ˆ์ œ::์ ˆ๋‹จ์  ์ฐพ๊ธฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜

๐Ÿฆ˜๋ฌธ์ œ ๐Ÿฆ˜

ํ•œ ๋ฒˆ์˜ ๊นŠ์ด์šฐ์„ ํƒ์ƒ‰์œผ๋กœ ๋ชจ๋“  ์ ˆ๋‹จ์ ์„ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜์˜ ๊ตฌํ˜„

โŒจ๏ธ ๋ฌดํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ ์ ˆ๋‹จ์ ์„ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜๐Ÿ’ฆ

findCutVertex() : ๋งค๊ฐœ๋ณ€์ˆ˜ here๋ฅผ ๋ฃจํŠธ๋กœ ํ•˜๋Š” ์„œ๋ธŒํŠธ๋ฆฌ์— ์žˆ๋Š” ์ ˆ๋‹จ์ ์„ ์ฐพ๋Š” ํ•จ์ˆ˜

vector<vector<int>> adj;
vector<int> discovered; //๊ฐ ์ •์ ์˜ ๋ฐœ๊ฒฌ์ˆœ์„œ, -1๋กœ ์ดˆ๊ธฐํ™”
vector<bool> isCutVertex; //๊ฐ ์ •์ ์˜ ์ ˆ๋‹จ์ ์—ฌ๋ถ€๋ฅผ ์ €์žฅ, false๋กœ ์ดˆ๊ธฐํ™”
int counter = 0;//๋ฐœ๊ฒฌ์ˆœ์„œ ์ €์žฅํ•˜๋Š” ๋ณ€์ˆ˜
int findCutVertex(int here, bool isRoot) {
  discovered[here] = counter++;//๋ฐœ๊ฒฌ์ˆœ์„œ ๊ฐ’ ๊ฐฑ์‹ 
  //ret : ํ•ด๋‹น ์„œ๋ธŒํŠธ๋ฆฌ์—์„œ ์—ญ๋ฐฉํ–ฅ ๊ฐ„์„ ์œผ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ •์  ์ค‘ ๊ฐ€์žฅ ์ผ์ฐ ๋ฐœ๊ฒฌ๋œ ์ •์ ์˜ ๋ฐœ๊ฒฌ์‹œ์ 
  int ret = discovered[here];//ret์— ํ˜„์žฌ ๋…ธ๋“œ์˜ ๋ฐœ๊ฒฌ์ˆœ์„œ๋ฅผ ์ €์žฅํ•˜๊ณ 
  int children = 0;//์ž์† ์„œ๋ธŒํŠธ๋ฆฌ์˜ ๊ฐœ์ˆ˜(๋ฃจํŠธ์ธ ๊ฒฝ์šฐ, ์ž์†์ด 2์ด์ƒ์ด์–ด์•ผ ํ•จ)
  for(int i=0; i<adj[here].size(); ++i) {//์ธ์ ‘๋…ธ๋“œ์— ๋Œ€ํ•ด
    int there = adj[here][i];//๊ฐ ์ธ์ ‘๋…ธ๋“œ๋ฅผ there์ด๋ผ ์ €์žฅํ•˜๊ณ 
    if(discovered[there] == -1) {//there ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†๋‹ค๋ฉด
      ++children;//์ผ๋‹จ here์˜ ์ž์‹๋…ธ๋“œ 1๊ฐœ ์ถ”๊ฐ€ํ•ด์ฃผ๊ณ ~
      //์žฌ๊ท€ํ˜ธ์ถœ๋กœ ์ž์‹๋…ธ๋“œ there์ด ๋ฃจํŠธ์ธ ์„œ๋ธŒํŠธ๋ฆฌ์— ์ ˆ๋‹จ์ ์„ ์ฐพ๋Š”๋‹ค.
      int subtree = findCutVertex(there, false);
      //here๊ฐ€ ๋ฃจํŠธ๊ฐ€ ์•„๋‹ˆ๋ฉด์„œ there์„œ๋ธŒํŠธ๋ฆฌ์˜ ์ ˆ๋‹จ์ ๊ฐœ์ˆ˜๊ฐ€ there์˜ ๋ฐœ๊ฒฌ ์ˆœ์„œ์ด์ƒ์ด๋ฉด,
      //์ฆ‰, ๊ทธ ๋…ธ๋“œ๊ฐ€ ์ž๊ธฐ ์ž์‹  ์ดํ•˜์— ์žˆ๋‹ค๋ฉด ํ˜„์žฌ ์œ„์น˜๋Š” ์ ˆ๋‹จ์ 
      if(!isRoot && subtree >= discovered[there])
         isCutVertex[here] = true;
      ret = min(ret, subtree);
    }
    else 
      ret = min(ret, discovered[there]);
  }
  if(isRoot) //๋ฃจํŠธ์ธ ๊ฒฝ์šฐ ์ž์†์ด 2์ด์ƒ์ด๋ฉด ์ ˆ๋‹จ์ ์—ฌ๋ถ€์— true
    isCutVertex[here] = (children >= 2);
  return ret;
}

๐ŸŒด ๋‹ค๋ฆฌ(bridge) ๐ŸŒด

์–ด๋–ค ๊ฐ„์„ ์„ ์‚ญ์ œํ–ˆ์„ ๋•Œ, ์ด ๊ฐ„์„ ์„ ํฌํ•จํ•˜๋˜ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋‘ ๊ฐœ๋กœ ์ชผ๊ฐœ์ง€๋Š” ๊ฒฝ์šฐ, ๊ทธ ๊ฐ„์„ ์„ **๋‹ค๋ฆฌ(bridge)**๋ผ๊ณ  ํ•œ๋‹ค.

  • bridge๋Š” ํ•ญ์ƒ Tree Edge์ด๋‹ค.
  • (u, v) ๊ฐ„์„ ์„ ์ œ์™ธํ•˜๊ณ , u๋ณด๋‹ค ๋†’์€ ์ •์ ๐Ÿ’ฆ์— ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์—ญ๋ฐฉํ–ฅ ๊ฐ„์„ ์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ, (u, v)๋Š” bridge์ด๋‹ค.
  • ์—ญ๋ฐฉํ–ฅ ๊ฐ„์„  ์ค‘ ์ž์‹ ์˜ ๋ถ€๋ชจ๋กœ ๊ฐ€๋Š” ๊ฐ„์„ ์„ ๋ฌด์‹œํ•œ ๋’ค, v์™€ ๊ทธ ์ž์†๋“ค์—์„œ ์—ญ๋ฐฉํ–ฅ ๊ฐ„์„ ์œผ๋กœ ๋‹ฟ์„ ์ˆ˜ ์žˆ๋Š” ์ •์ ์˜ ์ตœ์†Œ ๋ฐœ๊ฒฌ ์ˆœ์„œ๊ฐ€ u ์ดํ›„๋ผ๋ฉด (u, v)๋Š” bridge์ด๋‹ค.๐Ÿ’ฆ

๐ŸŒด ๊ฐ•๊ฒฐํ•ฉ ์ปดํฌ๋„ŒํŠธ(Strongly Connected Components_SCC) ๐ŸŒด

  • ์ด์ค‘ ๊ฒฐํ•ฉ ์ปดํฌ๋„ŒํŠธ์™€ ๋น„์Šทํ•˜์ง€๋งŒ, ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ ์ •์˜๋˜๋Š” ๊ฐœ๋…์ด๋‹ค.

  • ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ u์™€ v์— ๋Œ€ํ•ด ์–‘๋ฐฉํ–ฅ์œผ๋กœ ๊ฐ€๋Š” ๊ฒฝ๋กœ๊ฐ€ ์žˆ์„ ๋•Œ ๋‘ ์ •์ ์€ ๊ฐ™์€ SCC์— ์†ํ•ด์žˆ๋‹ค๊ณ  ํ•œ๋‹ค.

  • ๊ทธ๋ž˜ํ”„์—์„œ ๊ฐ SCC ์‚ฌ์ด๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฐ„์„ ๋“ค์„ ๋ชจ์œผ๋ฉด SCC๋“ค์„ ์ •์ ์œผ๋กœ ํ•˜๋Š” DAG(Directed Acylic Graph_์‚ฌ์ดํด ์—†๋Š” ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„)๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

  • ๊ทธ๋ž˜ํ”„์˜ ์••์ถ•(Condensation) : ๊ทธ๋ž˜ํ”„์˜ ์ •์ ๋“ค์„ SCC๋ณ„๋กœ ๋ถ„๋ฆฌํ•˜๊ณ  ๊ฐ SCC๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ์ •์ ๋“ค์„ ๋‹บ๋Š” ์ƒˆ๋กœ์šด ๊ทธ๋ž˜ํ”„๋ฅผ ๋งŒ๋“œ๋Š” ๊ณผ์ •

  • ๊ทธ๋ž˜ํ”„๊ฐ€ ๋‘ ๊ฐœ ์ด์ƒ์˜ SCC๋กœ ๋‚˜๋ˆ ์ง€๋ฉด, ํ•œ ์ง€์ ์—์„œ ๋‹ค๋ฅธ ์ง€์ ์œผ๋กœ ๊ฐˆ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค๋Š” ์˜๋ฏธ.

๐ŸŒฑ ํƒ€์ž” ์•Œ๊ณ ๋ฆฌ์ฆ˜(Tarjain Algorithms)๐Ÿ’ฆ

  • ํ•œ ๋ฒˆ์˜ ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰์œผ๋กœ ๊ฐ ์ •์ ์„ SCC๋ณ„๋กœ ๋ถ„๋ฆฌํ•œ๋‹ค.
  1. DFS ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ๋ฅผ ๋งŒ๋“ ๋‹ค.
  2. ์ด ๋•Œ, ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰์„ ์ˆ˜ํ–‰ํ•˜๋ฉด์„œ ๊ฐ ์ •์ ์„ SCC๋กœ ๋ฌถ๋Š”๋‹ค.

โŒจ๏ธ ํƒ€์ž”์˜ ๊ฐ•๊ฒฐํ•ฉ ์ปดํฌ๋„ŒํŠธ ๋ถ„๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ตฌํ˜„๐Ÿ’ฆ


๐ŸŒฑSCC์˜ ์œ„์ƒ์ •๋ ฌ

์ƒˆ SCC๊ฐ€ ์ƒ๊ฒจ๋‚˜๋Š” ์‹œ์ ์€ ํ•ญ์ƒ scc()ํ•จ์ˆ˜๊ฐ€ ์ข…๋ฃŒํ•˜๊ธฐ ์ง์ „์ด๋‹ค.

๊ทธ๋ž˜์„œ ๊ฐ SCC๋Š” ์œ„์ƒ์ •๋ ฌ์˜ ์—ญ์ˆœ์œผ๋กœ ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ง„๋‹ค. (= ์ข…๋ฃŒํ•  ๋•Œ ๋งˆ๋‹ค ์ €์žฅ๋œ SCC์ˆœ์„œ๋ฅผ ๋’ค์ง‘์œผ๋ฉด ์œ„์ƒ์ •๋ ฌ ๊ฒฐ๊ณผ์ด๋‹ค.)


๐ŸŒด ์ง€๋ฐฐ์ง‘ํ•ฉ(dominating set)๊ณผ ๋ฃจํŠธ ์—†๋Š” ํŠธ๋ฆฌ(unrooted tree) ๐ŸŒด

๐ŸŒฑ ์ง€๋ฐฐ ์ง‘ํ•ฉ(dominating set)

๊ทธ๋ž˜ํ”„์˜ ๋ชจ๋“  ์ •์ ์„ ์ง€๋ฐฐํ•˜๋Š” ์ •์ ์˜ ๋ถ€๋ถ„์ง‘ํ•ฉ์„ ๋งํ•˜๋ฉฐ, ์ด ๋•Œ ๊ฐ ์ •์ ์ด ์ž๊ธฐ ์ž์‹ ๊ณผ ๋ชจ๋“  ์ธ์ ‘ํ•œ ์ •์ ๋“ค์„ ์ง€๋ฐฐํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค.

ํŠธ๋ฆฌ์˜ ์ตœ์†Œ ์ง€๋ฐฐ ์ง‘ํ•ฉ์„ ์ฐพ๋Š” ๋ฐฉ๋ฒ•

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

  • ์žŽ ๋…ธ๋“œ๋Š” ์„ ํƒํ•˜์ง€ ์•Š๋Š”๋‹ค.(์ž์‹ ๊ณผ ๋ถ€๋ชจ๋…ธ๋“œ๋งŒ ์ง€๋ฐฐํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ)
  • ์ด ์™ธ ๋…ธ๋“œ๋Š” ๋‹ค์Œ์˜ ์กฐ๊ฑด ๋งŒ์กฑ์—ฌ๋ถ€์— ๋”ฐ๋ผ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ• ์ง€, ์„ ํƒํ•˜์ง€ ์•Š์„์ง€ ๊ฒฐ์ •ํ•œ๋‹ค.
    • ์ž์† ์ค‘ ์•„์ง ์ง€๋ฐฐ๋‹นํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด, ์„ ํƒ
    • ์—†๋‹ค๋ฉด, ์„ ํƒํ•˜์ง€ ์•Š๋Š”๋‹ค.

๐ŸŒฑ ๋ฃจํŠธ ์—†๋Š” ํŠธ๋ฆฌ (unrooted tree)

์‚ฌ์ดํด์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ทธ๋ž˜ํ”„๋Š” ๋…ธ๋“œ ๊ฐ„์˜ ์ƒํ•˜๊ด€๊ณ„๊ฐ€ ์—†์„ ๋ฟ, ํŠธ๋ฆฌ์™€ ๊ฐ™์€ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง€๋Š”๋ฐ ์ด๋ฅผ ๋ฃจํŠธ ์—†๋Š” ํŠธ๋ฆฌ๋ผ ํ•œ๋‹ค.

๋ฃจํŠธ ์—†๋Š” ํŠธ๋ฆฌ์˜ ์†์„ฑ (์„œ๋กœ ๋™์น˜๊ด€๊ณ„)

  • ์ •ํ™•ํžˆ V-1๊ฐœ์˜ ๊ฐ„์„ ์ด ์žˆ๋‹ค.
  • ์‚ฌ์ดํด์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค.
  • ๋‘ ์ •์  ์‚ฌ์ด๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋‹จ์ˆœ ๊ฒฝ๋กœ๊ฐ€ ์ •ํ™•ํžˆ ํ•˜๋‚˜ ์žˆ๋‹ค.

๐Ÿ“ ์˜ˆ์ œ::๊ฐ์‹œ ์นด๋ฉ”๋ผ ์„ค์น˜(ID: GALLERY)

๐Ÿฆ˜๋ฌธ์ œ ๐Ÿฆ˜

๋ฏธ์ˆ ๊ด€์— ๊ฐ์‹œ ์นด๋ฉ”๋ผ ์„ค์น˜ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ํ•œ ๊ฐค๋Ÿฌ๋ฆฌ์— ๊ฐ์‹œ์นด๋ฉ”๋ผ๋ฅผ ์„ค์น˜ํ•˜๋ฉด ํ•ด๋‹น ๊ฐค๋Ÿฌ๋ฆฌ + ์—ฐ๊ฒฐ๋œ ๊ฐค๋Ÿฌ๋ฆฌ๋ฅผ ๋ชจ๋‘ ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•  ๋•Œ, ๋ชจ๋“  ๊ฐค๋Ÿฌ๋ฆฌ๋ฅผ ๊ฐ์‹œํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์ตœ์†Œ ๊ฐ์‹œ ์นด๋ฉ”๋ผ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ.

โšก๏ธkeypointโšก๏ธ

  • dfs(here)๋Š” here๋ฅผ ๋ฃจํŠธ๋กœ ํ•˜๋Š” ์„œ๋ธŒํŠธ๋ฆฌ๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ณ , ๋ฐ˜ํ™˜ํ•˜๋ฉด์„œ
    • ํ•ด๋‹น ๋…ธ๋“œ๊ฐ€ ์ง€๋ฐฐ ์ง‘ํ•ฉ์˜ ์ผ๋ถ€๋กœ ์„ ํƒ๋˜์—ˆ๋Š”์ง€(=์ž์† ์ค‘ ์•„์ง ์ง€๋ฐฐ๋˜์ง€ ์•Š์€ ๋…ธ๋“œ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ) ๐Ÿ‘‰ INSTALLED
    • ์•„๋‹ˆ๋ฉด ๋‹ค๋ฅธ ๋…ธ๋“œ์— ์ง€๋ฐฐ๋‹นํ•˜๊ณ  ์žˆ๋Š”์ง€ ๐Ÿ‘‰ WATCHED
    • ํ˜น์€ ์ง€๋ฐฐ๋‹นํ•˜๊ณ  ์žˆ์ง€ ์•Š๋Š”์ง€๋ฅผ ๋ฐ˜ํ™˜ ๐Ÿ‘‰ UNWATCHED

โŒจ๏ธ ๊ฐ์‹œ ์นด๋ฉ”๋ผ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„

int V; //๊ฐค๋Ÿฌ๋ฆฌ ์ˆ˜
vector<int> adj[MAX_V]; //์ธ์ ‘ํ–‰๋ ฌ
vector<bool> visited; //๋ฐฉ๋ฌธ์—ฌ๋ถ€ 
const int UNWATCHED = 0;//๊ฐ์‹œ๋˜์ง€ ์•Š์€ ๊ฐค๋Ÿฌ๋ฆฌ
const int WATCHED = 1;//๊ฐ์‹œ๋˜๋Š” ๊ฐค๋Ÿฌ๋ฆฌ
const int INSTALLED = 2;//์„ค์น˜๋œ ์นด๋ฉ”๋ผ
int installed;//ํ˜„์žฌ๊นŒ์ง€ ์„ค์น˜๋œ ์นด๋ฉ”๋ผ ์ˆ˜
int dfs(int here) {
    visited[here] = true;//๋ฐฉ๋ฌธ์—ฌ๋ถ€ ์ฐธ์œผ๋กœ ์„ค์ •
    int childeren[3] = {0,0,0};//์„œ๋ธŒํŠธ๋ฆฌ์˜ UNWATCHED, WATCHED, INSTALLED ์ •๋ณด์ €์žฅ
    for(int i=0; i<adj[here].size(); ++i) {//์ž์†๋…ธ๋“œ ์ˆ˜๋งŒํผ
      int there = adj[here][i];//๊ฐ ์ธ์ ‘๋…ธ๋“œ๋ฅผ there๋กœ ์„ค์ •ํ•˜๊ณ 
      if(!visited[there]) //์•„์ง ๋ฐฉ๋ฌธ์•ˆํ–ˆ๋‹ค๋ฉด
         ++children[dfs(there)];//ํ•ด๋‹น ๋…ธ๋“œ์˜ children๋ฐฐ์—ด ์ •๋ณด ๊ฐฑ์‹ 
    }
    if(children[UNWATCHED]) {//์•„์ง ๊ฐ์‹œ๋˜์ง€ ์•Š์€ ๊ฐค๋Ÿฌ๋ฆฌ๊ฐ€ ์žˆ๋‹ค๋ฉด
      ++installed;//์นด๋ฉ”๋ผ ์„ค์น˜ํ•ด์ฃผ๊ณ 
      return INSTALLED;//์„ค์น˜๋˜์—ˆ๋‹ค๊ณ  ์ฒดํฌ
    }
    if(children[INSTALLED])//์„ค์น˜๋œ ์นด๋ฉ”๋ผ๊ฐ€ ์žˆ๋‹ค๋ฉด ๋˜ ์„ค์น˜ํ•  ํ•„์š”์—†๊ณ 
      return WATCHED;//๊ฐ์‹œ๋˜๋Š” ๊ฐค๋Ÿฌ๋ฆฌ ์ˆ˜ ๋ฐ˜ํ™˜ํ•˜๊ณ 
    return UNWATCHED;//๊ทธ ์™ธ์—๋Š” ๊ฐ์‹œ๋˜์ง€ ์•Š๋Š” ๊ฐค๋Ÿฌ๋ฆฌ ์ˆ˜ ๋ฐ˜ํ™˜
}
//๊ทธ๋ž˜ํ”„๋ฅผ ๊ฐ์‹œํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ์นด๋ฉ”๋ผ ์ตœ์†Œ ์ˆ˜ ๋ฐ˜ํ™˜
int installCamera() {
    installed = 0;//ํ˜„์žฌ๊นŒ์ง€ ์„ค์น˜๋œ ์นด๋ฉ”๋ผ ์ˆ˜
    visited = vector<bool>(V, false);//์ดˆ๊ธฐํ™”
    for(int u=0; u<V; ++u) {//๊ฐค๋Ÿฌ๋ฆฌ ์ˆ˜๋งŒํผ
      //์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๊ณ  u๊ฐ€ ์•„์ง ๊ฐ์‹œ๋˜์ง€ ์•Š๋Š” ์ƒํƒœ๋ผ๋ฉด
      if(!visitied[u] && dfs(u) == UNWATCHED)
        ++installed;//์นด๋ฉ”๋ผ ์„ค์น˜
    }
    return installed;//์ด ๊ฐœ์ˆ˜ ๋ฐ˜ํ™˜
}

โฑTime Complexityโฑ

O(g+h)


๐ŸŒด (2-)SAT๋ฌธ์ œ ๐ŸŒด ๐Ÿ’ฆ

์ฐธ์ด๋ƒ, ๊ฑฐ์ง“์ด๋ƒ์˜ ๊ฒฐ์ •์„ ์—ฌ๋Ÿฌ ๋ฒˆ ํ•ด์•ผํ•˜๋Š” ๋ฌธ์ œ๋“ค์€ ๋ถˆ๋ฆฐ ๊ฐ’ ๋งŒ์กฑ์„ฑ ๋ฌธ์ œ(Boolean Satisfiability Problem) ํ˜น์€ SAT๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

๐ŸŒฑ SAT ๋ฌธ์ œ๋ž€?

๋ถˆ๋ฆฐ ๊ฐ’ ๋ณ€์ˆ˜์˜ ์ฐธ ํ˜•ํƒœ์™€ ๊ฑฐ์ง“ ํ˜•ํƒœ๋“ค๋กœ ๊ตฌ์„ฑ๋œ ์‹์ด ์ฃผ์–ด์งˆ ๋•Œ, ์ด ์‹์˜ ๊ฐ’์„ ์ฐธ์œผ๋กœ ํ•˜๋Š” ๋ณ€์ˆ˜์˜ ์กฐํ•ฉ์ด ์žˆ๋Š”์ง€ ์ฐพ๋Š” ๊ฒƒ.

๐ŸŒฑ 2-SAT ๋ฌธ์ œ

๋…ผ๋ฆฌ์‹์„ ๋…ผ๋ฆฌ๊ณฑ ์ •๊ทœํ˜•์œผ๋กœ ํ‘œํ˜„ํ–ˆ์„ ๋•Œ ๊ฐ ์ ˆ์— ์ตœ๋Œ€ ๋‘ ๊ฐœ์˜ ๋ณ€์ˆ˜๋งŒ์ด ์กด์žฌํ•˜๋Š” SAT๋ฌธ์ œ

๊ทธ๋ž˜ํ”„๋ฅผ ์ด์šฉํ•ด ๋‹คํ•ญ ์‹œ๊ฐ„์— ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.


๐ŸŒž[์ข…๋งŒ๋ถ] ๊ทธ๋ž˜ํ”„์˜ ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰๐ŸŒž

๐ŸŒด๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰๐ŸŒด

์‹œ์ž‘์ ์— ๊ฐ€๊นŒ์šด ์ •์ ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ๋ฐฉ๋ฌธํ•˜๋Š” ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜

๐ŸŒฑ๋™์ž‘์›๋ฆฌ/๊ณผ์ •

  1. ๊ฐ ์ •์ ์„ ๋ฐฉ๋ฌธํ•  ๋•Œ๋งˆ๋‹ค ๋ชจ๋“  ์ธ์ ‘ ์ •์ ๋“ค์„ ๊ฒ€์‚ฌํ•œ๋‹ค.
  2. ์ด ์ค‘ ์ฒ˜์Œ๋ณด๋Š” ์ •์ ์„ ๋ฐœ๊ฒฌํ•˜๋ฉด ๋ฐฉ๋ฌธํ•  ์ •์  ๋ชฉ๋ก ํ(queue)์— ์ €์žฅํ•ด๋†“๋Š”๋‹ค.
  3. ์ธ์ ‘ํ•œ ์ •์ ๋“ค์„ ๋ชจ๋‘ ๊ฒ€์‚ฌํ•˜๊ณ ๋‚˜์„œ, ๋ฐฉ๋ฌธํ•  ์ •์  ๋ชฉ๋ก์—์„œ ๋‹ค์Œ ์ •์ ์„ ๊บผ๋‚ด์„œ ๋ฐฉ๋ฌธํ•œ๋‹ค.

โŒจ๏ธ ๊ทธ๋ž˜ํ”„์˜ ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰

vector<vector<int>> adj;//์ธ์ ‘๋ฆฌ์ŠคํŠธ
vector<int> bfs(int start) {
  vector<bool> discovered(adj.size(), false);//๋ฐœ๊ฒฌ์—ฌ๋ถ€ ์ดˆ๊ธฐํ™”
  queue<int> q;//๋ฐฉ๋ฌธํ•  ์ •์  ๋ชฉ๋ก
  vector<int> order;//๋ฐฉ๋ฌธํ•œ ์ •์  ๋ชฉ๋ก
  discovered[start] = true;//์‹œ์ž‘๋…ธ๋“œ์˜ ๋ฐœ๊ฒฌ์—ฌ๋ถ€: ์ฐธ
  q.push(start);//๋ฐฉ๋ฌธํ•  ์ •์  ๋ชฉ๋ก์— ์‹œ์ž‘๋…ธ๋“œ ์ถ”๊ฐ€
  while(!q.empty()) {//๋ฐฉ๋ฌธํ•  ์ •์  ๋ชฉ๋ก์ด ์žˆ๋Š” ํ•œ
    int here = q.front();//๋ฐฉ๋ฌธํ•  ์ •์  ๋ชฉ๋ก์˜ ์ฒซ๋ฒˆ์จฐ 
    q.pop();//๋ฐฉ๋ฌธํ–ˆ์œผ๋‹ˆ, ๋ชฉ๋ก์—์„œ๋Š” ์ œ๊ฑฐ
    order.push_back(here);//๋ฐฉ๋ฌธํ•œ ๋ชฉ๋ก์—๋Š” ์ถ”๊ฐ€
    for(int i=0; i<adj[here].size(); ++i) {//์ธ์ ‘๋ชฉ๋ก์— ๋Œ€ํ•ด
      int there = adj[here][i];//ํ˜„์žฌ ์ธ์ ‘๋ชฉ๋ก์„ there๋กœ ์„ค์ •ํ•˜๊ณ 
      if(!discovered[there]) {//there์„ ์•„์ง ๋ฐœ๊ฒฌ์•ˆํ–ˆ์—ˆ๋‹ค๋ฉด
        q.push(there);//๋ฐฉ๋ฌธํ•  ์ •์  ๋ชฉ๋ก์— ์ถ”๊ฐ€ํ•˜๊ณ 
        discovered[there] = true;//๋ฐœ๊ฒฌ์—ฌ๋ถ€๋Š” ์ฐธ์œผ๋กœ ์„ค์ •
      }
    }
  }
  return order;//๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ ์ˆœ์„œ๋ชฉ๋ก์„ ๋ฐ˜ํ™˜
}

โฑTime Complexityโฑ

์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„ํ•œ ๊ฒฝ์šฐ : O(|V|+|E|)

์ธ์ ‘ ํ–‰๋ ฌ๋กœ ๊ตฌํ˜„ํ•œ ๊ฒฝ์šฐ : O(|V|^2)

๐ŸŒฑ ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ(BFS Spanning Tree)

๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰์—์„œ ์ƒˆ ์ •์ ์„ ๋ฐœ๊ฒฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ–ˆ๋˜ ๊ฐ„์„ ๋“ค๋งŒ์„ ๋ชจ์€ ํŠธ๋ฆฌ


๐ŸŒด๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰๊ณผ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๐ŸŒด

์ตœ๋‹จ ๊ฒฝ๋กœ ๋ฌธ์ œ: ๋‘ ์ •์ ์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒฝ๋กœ ์ค‘ ๊ฐ€์žฅ ๊ธธ์ด๊ฐ€ ์งง์€ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ

  • ์‹œ์ž‘์ ์œผ๋กœ๋ถ€ํ„ฐ ๋‹ค๋ฅธ ๋ชจ๋“  ์ •์ ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ BFS Spanning Tree์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

  • BFS Spanning Tree์—์„œ ๊ฐ ์ •์ ์œผ๋กœ๋ถ€ํ„ฐ ํŠธ๋ฆฌ์˜ ๋ฃจํŠธ์ธ ์‹œ์ž‘์ ์œผ๋กœ ๊ฐ€๋Š” ๊ฒฝ๋กœ๊ฐ€ ์‹ค์ œ ๊ทธ๋ž˜ํ”„ ์ƒ์—์„œ์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ์ด๋‹ค.


โŒจ๏ธ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰

bfs2() : start์—์„œ ์‹œ์ž‘ํ•ด ๊ทธ๋ž˜ํ”„๋ฅผ ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ํ•˜๊ณ  ์‹œ์ž‘์ ๋ถ€ํ„ฐ ๊ฐ ์ •์ ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ์™€ ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ๋ฅผ ๊ณ„์‚ฐ

distance[i] : start๋ถ€ํ„ฐ i๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ

parent[i] : ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ์—์„œ i์˜ ๋ถ€๋ชจ์˜ ๋ฒˆํ˜ธ, ๋ฃจํŠธ๋ฉด ์ž๊ธฐ์ž์‹ 

void bfs2(int start, vector<int>& distance, vector<int>& parent) {
  distance = vector<int>(adj.size(), -1);
  parent = vector<int>(adj.size(), -1);
  queue<int> q;
  distance[start] = 0;
  parent[start] = start;
  q.push(start);
  while(!q.empty()) {
    int here = q.front();
    q.pop();
    for(int i=0; i<adj[here].size(); ++i) {
      int there = adj[here][i];
      if(distance[there] == -1) {
        q.push(there);
        distance[there] = distance[here] + 1;
        parent[there] = here;
      }
    }
  }
}

shortestPath() : v๋กœ๋ถ€ํ„ฐ ์‹œ์ž‘์ ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ ๊ณ„์‚ฐ

vector<int> shortestPath(int v, const vector<int>& parent) {
  vector<int> path(1,v);//์ตœ๋‹จ๊ฒฝ๋กœ๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด
  while(parent[v] != v) {//๋ฃจํŠธ์— ๋„๋‹ฌํ•˜์ง€ ์•Š๋Š” ํ•œ
    v = parent[v];//v๋ฅผ ๋ถ€๋ชจ๋กœ ์„ค์ •ํ•˜๊ณ  
    path.push_back(v);//์ตœ๋‹จ๊ฒฝ๋กœ์— ์ถ”๊ฐ€ํ•œ๋‹ค
  }
  //์‹œ์ž‘์ ๋ถ€ํ„ฐ ๊ฒฝ๋กœ๊ตฌํ•ด์•ผ๋˜๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ๋Š” ๋ฃจํŠธ๊ฐ€ ๋์ ์ด ๋˜๋„๋ก ๊ตฌํ–ˆ์œผ๋ฏ€๋กœ ๋’ค์ง‘์–ด์ค€๋‹ค.
  reverse(path.begin(), path.end());
  return path;//์ตœ๋‹จ๊ฒฝ๋กœ ๋ฐ˜ํ™˜
}

๐Ÿ“ ์˜ˆ์ œ::Sorting Game(ID: SORTGAME)

๐Ÿฆ˜๋ฌธ์ œ ๐Ÿฆ˜

์—ฐ์†๋œ ๋ถ€๋ถ„ ๊ตฌ๊ฐ„์˜ ์ˆœ์„œ๋ฅผ ๋’ค์ง‘๋Š” ๊ฒƒ์„ ๋’ค์ง‘๊ธฐ ์—ฐ์‚ฐ์ด๋ผ๊ณ  ํ•  ๋•Œ, ์ค‘๋ณต์—†๋Š” ์ •์ˆ˜ ๋ฐฐ์—ด์„ ๋’ค์ง‘๊ธฐ ์—ฐ์‚ฐ์„ ์ด์šฉํ•ด ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์ตœ์†Œ ์—ฐ์‚ฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ

โšก๏ธkeypointโšก๏ธ

  • ๊ทธ๋ž˜ํ”„๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ณผ์ •์„ ์ƒ๋žตํ•˜๊ณ  ๋ถ€๋ถ„ ๊ตฌ๊ฐ„์„ ๋’ค์ง‘์œผ๋ฉด์„œ ๊ทธ๋•Œ ๊ทธ๋•Œ ๊ทธ๋ž˜ํ”„์˜ ๊ฐ„์„ ์„ ๋งŒ๋“ ๋‹ค.
  • ์ •์  ํ๋„ ์ •์ˆ˜์˜ ๋ฐฐ์—ด์„ ํฌํ•จํ•˜๊ณ , distance[]๋Š” ์ •์ˆ˜์˜ ๋ฐฐ์—ด์„ ํ‚ค๋กœ ๊ฐ–๋Š” map์œผ๋กœ ๋ฐ”๊พธ์–ด ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

โŒจ๏ธ Sorting Game ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜๐Ÿ’ฆ

perm : ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ์ •์ˆ˜ ๋ฐฐ์—ด

bfs() : perm์„ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์ตœ์†Œ ๋’ค์ง‘๊ธฐ ์—ฐ์‚ฐ์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐ

๐Ÿ’ฆ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๋ถ€๋ถ„ ๊ตฌ๊ฐ„์„ ๋’ค์ง‘์–ด ๋ณด๋Š” ๋ถ€๋ถ„ ๋‹ค์‹œ ๋ณด๊ธฐ ๐Ÿ’ฆ

int bfs(const vector<int>& perm) {
  int n = perm.size();//์ˆ˜์—ด์„ ์ด๋ฃจ๋Š” ์ˆซ์ž๊ฐœ์ˆ˜
  vector<int> sorted = perm;//sorted_perm์ด ์ •๋ ฌ๋œ ํ˜•ํƒœ
  sort(sorted.begin(), sorted.end());//<algorithm>
  queue<vector<int>> q; //์ •์ ์˜ ๋ฐฉ๋ฌธ ๋ชฉ๋ก
  map<vector<int>, int> distance; //์‹œ์ž‘์ ๋ถ€ํ„ฐ ๊ฐ ์ •์ ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ
  distance[perm] = 0;//์‹œ์ž‘์ ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋Š” 0
  q.push(perm); //์‹œ์ž‘์ ์„ ๋ฐฉ๋ฌธ๋ชฉ๋ก์— ์ถ”๊ฐ€
  while(!q.empty()) {//๋ฐฉ๋ฌธํ•  ์ •์ ์ด ์žˆ๋Š” ํ•œ
    vector<int> here = q.front(); //์ด๋ฒˆ์— ๋ฐฉ๋ฌธํ•  ์ •์ (์ˆ˜์—ด)
    q.pop();//๋ฐฉ๋ฌธํ–ˆ์œผ๋‹ˆ ๋ฐฉ๋ฌธํ•  ๋ชฉ๋ก์—์„œ ์ œ๊ฑฐ
    //ํ˜„์žฌ ์ˆ˜์—ด์ด ์ •๋ ฌ๋œ sorted ์ˆ˜์—ด๊ณผ ๊ฐ™๋‹ค๋ฉด(=๋‹ต์ด๋ฏ€๋กœ) ์ตœ๋‹จ๊ฑฐ๋ฆฌ ๋ฐ˜ํ™˜
    if(here == sorted) return distance[here];
    int cost = distance[here];//์•„๋‹ˆ๋ผ๋ฉด, ํ˜„์žฌ๊นŒ์ง€ ์—ฐ์‚ฐ์˜ ์ˆ˜๋ฅผ cost์— ์ €์žฅํ•˜๊ณ 
    //๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๋ถ€๋ถ„ ๊ตฌ๊ฐ„์„ ๋’ค์ง‘์–ด ๋ณธ๋‹ค. 
    for(int i=0; i<n; ++i) {
      for(int j=i+2; i<=n; ++j) {
        reverse(here.begin()+i, here.begin()+j);
        if(distance.count(here) == 0) {
          distance[here] = cost + 1;
          q.push(here);
        }
        reverse(here.begin()+i, here.begin()+j);
      }
    }
  }
  return -1;
}

๐Ÿ’ก ๋‹ค์Œ์˜ ์†์„ฑ์„ ์ด์šฉํ•ด ์ตœ์ ํ™”ํ•ด๋ณด์ž ๐Ÿ’ก

  • ์ˆซ์ž๋“ค์ด ๋‹ฌ๋ผ๋„ ์ƒ๋Œ€์ ์ธ ํฌ๊ธฐ๊ฐ€ ๊ฐ™์€ ๋ฐฐ์—ด๋“ค์— ๋Œ€ํ•œ ๋‹ต์€ ๊ฐ™๋‹ค.
  • ์ƒํƒœ๊ณต๊ฐ„์ด ์–‘๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์ด๋ฏ€๋กœ, ์‹œ์ž‘ ์ •์ ์—์„œ ๋ชฉํ‘œ ์ •์ ์œผ๋กœ ๊ฐ€๋Š” ์ตœ๋‹จ๊ฑฐ๋ฆฌ == ๋ชฉํ‘œ ์ •์ ์—์„œ ์‹œ์ž‘ ์ •์ ์œผ๋กœ ๊ฐ€๋Š” ์ตœ๋‹จ๊ฑฐ๋ฆฌ ์ด๋‹ค. ์ฆ‰, ํ•œ ๋ฐฐ์—ด์„ ์ •๋ ฌํ•˜๋Š” ๋ฐ ๋“œ๋Š” ์—ฐ์‚ฐ์˜ ์ˆ˜๋Š” ์ •๋ ฌ๋œ ๋ฐฐ์—ด์„ ์›๋ž˜ ๋ฐฐ์—ด๋กœ ๋งŒ๋“œ๋Š” ๋ฐ ๋“œ๋Š” ์—ฐ์‚ฐ์˜ ์ˆ˜์™€ ๊ฐ™๋‹ค.

โšก๏ธkeypointโšก๏ธ

์ •๋ ฌ๋œ ๋ฐฐ์—ด์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“  ์ƒํƒœ๊นŒ์ง€ ๋„๋‹ฌํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๋’ค์ง‘๊ธฐ ์—ฐ์‚ฐ์˜ ์ˆ˜๋ฅผ ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰์„ ์ด์šฉํ•ด ๊ณ„์‚ฐํ•ด๋‘”๋‹ค.

โŒจ๏ธ Sorting Game ๋ฌธ์ œ๋ฅผ ๋น ๋ฅด๊ฒŒ ํ•ด๊ฒฐํ•˜๋Š” ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰์˜ ๊ตฌํ˜„

precalc() : ๋ชจ๋“  ์ˆœ์—ด์— ๋Œ€ํ•ด ํ•„์š”ํ•œ ๋’ค์ง‘๊ธฐ ์—ฐ์‚ฐ์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐ

solve() : precalc()์˜ ๊ฒฐ๊ณผ๋ฅผ ์ด์šฉํ•ด perm์„ ์ •๋ ฌํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๋’ค์ง‘๊ธฐ ์—ฐ์‚ฐ์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐ

๐Ÿ’ฆ precal : ์ด์ค‘ for๋ฌธ ๋ถ€๋ถ„ ๋‹ค์‹œ ๋ณด๊ธฐ / solve : fixed์˜ ์˜๋ฏธ๐Ÿ’ฆ

map<vector<int>,int> toSort; //ํ•„์š”ํ•œ ๋’ค์ง‘๊ธฐ ์—ฐ์‚ฐ์˜ ์ˆ˜
//๋ชจ๋“  ์ˆœ์—ด์— ๋Œ€ํ•ด toSort[] ๊ณ„์‚ฐ
void precalc(int n) {
  vector<int> perm(n);
  for(int i=0; i<n; ++i) perm[i] = i;
  queue<vector<int>> q;//๋ฐฉ๋ฌธํ•  ์ •์ ๋ชฉ๋ก
  q.push(perm);//์—ฐ์‚ฐ์˜ ์ˆ˜ ๊ตฌํ•  ์ˆ˜์—ด์„ ๋ฐฉ๋ฌธํ•  ์ •์ ๋ชฉ๋ก์— ์ถ”๊ฐ€
  toSort[perm] = 0;//์ดˆ๊ธฐ ๋’ค์ง‘๊ธฐ ์—ฐ์‚ฐ์€ 0์œผ๋กœ ์„ค์ •
  while(!q.empty()) {//๋ฐฉ๋ฌธํ•  ์ •์ (์ˆ˜์—ด)์ด ์žˆ๋Š” ํ•œ
    vector<int> here = q.front();//ํ˜„์žฌ ๋ฐฉ๋ฌธํ•œ ์ˆ˜์—ด์„ here์ด๋ผ ์„ค์ •
    q.pop();//๋ฐฉ๋ฌธํ–ˆ์œผ๋‹ˆ ๋ชฉ๋ก์—์„œ ์ œ๊ฑฐ
    int cost = toSort[here];//here์—์„œ์˜ ์—ฐ์‚ฐ์˜ ์ˆ˜๋ฅผ cost์— ์ €์žฅํ•˜๊ณ 
    for(int i=0; i<n; ++i) {
      for(int j=i+2; j<=n; ++j) {
        reverse(here.begin()+i, here.begin()+j);
        if(toSort.count(here) == 0) { 
          toSort[here] = cost + 1; //์—ฐ์‚ฐ์˜ ์ˆ˜ +1
          q.push(here);
        }
        reverse(here.begin()+i, here.begin()+j);
      }
    }
  }
}
int solve(const vector<int>& perm) {
  int n = perm.size();
  vector<int> fixed(n);
  for(int i=0; i<n; ++i) {
    int smaller = 0;
    for(int j=0; j<n; ++j) 
      if(perm[j] < perm[i]) //๋’ค์— ๊ฒƒ์ด ๋” ์ž‘๋‹ค๋ฉด 
        ++smaller; //๋’ค์ง‘๊ธฐ ์—ฐ์‚ฐ ํ•„์š”ํ•˜๋‹ˆ๊นŒ ์—ฐ์‚ฐ ์ˆ˜ +1
    fixed[i] = smaller;//?
  }
  return toSort[fixed];//?
}

๐Ÿ“ ์˜ˆ์ œ::์–ด๋ฆฐ์ด๋‚ (ID: CHILDRENDAY)

๐Ÿฆ˜๋ฌธ์ œ ๐Ÿฆ˜

n๋ช…์˜ ๋ชจ๋“  ์•„์ด๋“ค์—๊ฒŒ ๊ฐ™์€ ์ˆ˜(1๊ฐœ ์ด์ƒ)์˜ ์žฅ๋‚œ๊ฐ์„ ์ฃผ๋˜, m๋ช…์˜ ์š•์‹ฌ์Ÿ์ด ์•„์ด๋“ค์€ ๋‹ค๋ฅธ ์•„์ด๋“ค๋ณด๋‹ค ์žฅ๋‚œ๊ฐ์„ ํ•œ ๊ฐœ ๋” ์ฃผ๋ ค ํ•œ๋‹ค. ์žฅ๋‚œ๊ฐ์€ ๊ฐ€๋Šฅํ•œ ์ ๊ฒŒ ์‚ฌ๋ คํ•  ๋•Œ ๊ตฌ์ž…ํ•  ์ตœ์†Œ ์žฅ๋‚œ๊ฐ์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐ

โšก๏ธkeypointโšก๏ธ

  • ์„ธ ๊ฐ€์ง€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ตœ์†Œ ์ž์—ฐ์ˆ˜๋ฅผ ์ฐพ๋Š”๋‹ค.
    • n+m์ด์ƒ์ด์–ด์•ผ ํ•œ๋‹ค.
    • n์œผ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ m์ด์–ด์•ผ ํ•œ๋‹ค.
    • ์žฅ๋‚œ๊ฐ์— ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์ž๋ฆฟ์ˆ˜ ๋ชฉ๋ก์— ํฌํ•จ๋œ ์ˆซ์ž๋“ค๋กœ๋งŒ ๊ตฌ์„ฑ์ด ๋˜์–ด์•ผ ํ•œ๋‹ค.
  • ์ตœ์†Œ ์žฅ๋‚œ๊ฐ ์ˆ˜ c๋ฅผ ์•„์ด๋“ค์˜ ์ด ์ธ์› ์ˆ˜ n์„ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ์ •์ ์œผ๋กœ ํ‘œํ˜„ํ•œ ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„๋ฅผ ๋งŒ๋“ค์–ด ๋ณธ๋‹ค.

๐ŸŒž[์ข…๋งŒ๋ถ] ์ตœ๋‹จ ๊ฒฝ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜๐ŸŒž

๐ŸŒด ์ตœ๋‹จ ๊ฒฝ๋กœ ๋ฌธ์ œ(Shortest Path Problem) ๐ŸŒด

๊ทธ๋ž˜ํ”„์—์„œ ์ฃผ์–ด์ง„ ๋‘ ์ •์ ์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฐ€์žฅ ์งง์€ ๊ฒฝ๋กœ์˜ ๊ธธ์ด๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ

๊ฐ€์ค‘์น˜๊ฐ€ ์žˆ๋Š” ๊ทธ๋ž˜ํ”„ ์œ„์—์„œ์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

์‹ค์ œ ๊ฒฝ๋กœ ๊ณ„์‚ฐ์„ ์œ„ํ•ด์„œ๋Š” ๋ณ„๋„ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ณ  ์‹ค์ œ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์•„๋‚ด๋Š” ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€์ ์œผ๋กœ ์ž‘์„ฑํ•ด์•ผ ํ•จ.

๐Ÿ‘‰ BFS์—์„œ์ฒ˜๋Ÿผ ๊ทธ๋ž˜ํ”„๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๊ณผ์ •์—์„œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ , ๊ฑฐ์Šฌ๋Ÿฌ ์˜ฌ๋ผ๊ฐ€๋ฉฐ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.

๐ŸŒฑ ์Œ์ˆ˜ ๊ฐ„์„ ์˜ ์ค‘์š”์„ฑ

๊ฐ€์ค‘์น˜์˜ ํ•ฉ์ด ์Œ์ˆ˜์ธ ์‚ฌ์ดํด์ด ๋“ฑ์žฅํ•˜๋ฉด ๊ฒฝ๋กœ์˜ ๊ธธ์ด๊ฐ€ ์Œ์˜ ๋ฌดํ•œ๋Œ€๋กœ ๋ฐœ์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฆ‰, ์Œ์ˆ˜ ์‚ฌ์ดํด์ด ์žˆ๋Š” ๊ทธ๋ž˜ํ”„์—์„œ๋Š” ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ์ฐพ๊ธฐ ์–ด๋ ต๋‹ค.

๐ŸŒฑ ๋‹จ์ผ ์‹œ์ž‘์  vs ๋ชจ๋“  ์Œ ์•Œ๊ณ ๋ฆฌ์ฆ˜

๋‹จ์ผ ์‹œ์ž‘์  ์•Œ๊ณ ๋ฆฌ์ฆ˜ : ํ•˜๋‚˜์˜ ์‹œ์ž‘์ ์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“  ์ •์ ๊นŒ์ง€ ๊ฐ€๋Š” ์ตœ๋‹จ ๊ฑฐ๋ฆฌ

๋ชจ๋“  ์Œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ : ๋ชจ๋“  ์ •์ ์˜ ์Œ์— ๋Œ€ํ•œ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ

๐ŸŒฑ ๋ฐฉํ–ฅ/๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„

์ตœ๋‹จ ๊ฑฐ๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„ ๊ธฐ์ค€.

๋”ฐ๋ผ์„œ ๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ๋Š” ์–‘๋ฐฉํ–ฅ ๊ฐ„์„ ์„ ๋‘ ๊ฐœ์˜ ์ผ๋ฐฉํ†ตํ–‰ ๊ฐ„์„ ์œผ๋กœ ์ชผ๊ฐœ์„œ ๋ฐฉํ–ฅ๊ทธ๋ž˜ํ”„๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.

๐Ÿ’ก ์Œ์ˆ˜ ๊ฐ„์„ ์ด ์žˆ๋Š” ๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ๋Š” ํ•ญ์ƒ ์ตœ๋‹จ ๊ฒฝ๋กœ ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์—†๋‹ค.


๐ŸŒด ๋‹ค์ต์ŠคํŠธ๋ผ(Dijkstra) ๐ŸŒด

๋‹จ์ผ ์‹œ์ž‘์  ์ตœ๋‹จ ๊ฒฝ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ, ์‹œ์ž‘์ •์ ์œผ๋กœ๋ถ€ํ„ฐ ๋‹ค๋ฅธ ์ •์ ๋“ค๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐ

๐ŸŒฑ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์ด์šฉํ•˜๋Š” BFS

  • BFS์ฒ˜๋Ÿผ ์‹œ์ž‘์ ์—์„œ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์ •์ ๋“ค๋ถ€ํ„ฐ ๋ฐฉ๋ฌธํ•œ๋‹ค.

  • ์šฐ์„ ์ˆœ์œ„ ํ๊ฐ€ ์ €์žฅํ•˜๋Š” ์ •๋ณด

    • u : ์ •์ ์˜ ๋ฒˆํ˜ธ
    • cost : ์ง€๊ธˆ๊นŒ์ง€ ์ฐพ์•„๋‚ธ ํ•ด๋‹น ์ •์ ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ (๊ธฐ์ค€)
  • dist[] : ๊ฐ ์ •์ ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด

  • ์ •์  ๋ฐฉ๋ฌธํ•  ๋•Œ๋งˆ๋‹ค ๋ชจ๋“  ์ธ์ ‘ ์ •์ ์„ ๊ฒ€์‚ฌํ•œ๋‹ค.

โŒจ๏ธ ๋‹ค์ต์ŠคํŠธ๋ผ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜

adj[์—ฐ๊ฒฐ๋œ ์ •์ ๋ฒˆํ˜ธ, ๊ฐ„์„  ๊ฐ€์ค‘์น˜] : ์ธ์ ‘๋ฆฌ์ŠคํŠธ

dist[v] : v๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ

pq[์ตœ๋‹จ๊ฑฐ๋ฆฌ, ์ •์ ๋ฒˆํ˜ธ] : ๋ฐฉ๋ฌธํ•  ์ •์ ์„ ์ €์žฅํ•˜๊ณ  ์šฐ์„ ์ˆœ์œ„ ํ๋Š” ๊ฐ€์žฅ ํฐ ์›์†Œ๊ฐ€ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฐ€์ง€๋ฏ€๋กœ, ์ตœ๋‹จ๊ฑฐ๋ฆฌ๊ฐ€ ์ œ์ผ ํฐ ์ •์ ์ด ์œ„๋กœ ์˜ค๋„๋ก -๋ถ€ํ˜ธ๋ฅผ ๋ถ™์—ฌ์ค€๋‹ค.

int V; //์ •์ ์˜ ๊ฐœ์ˆ˜
vector<pair<int,int>> adj[MAX_V];
vector<int> dijkstra(int src) {
  vector<int> dist(V, INF);
  dist[src] = 0;
  priority_queue<pair<int,int>> pq;
  pq.push(make_pair(0,src));
  while(!pq.empty()) {
    int cost = -pq.top().first;//pq ์ตœ์ƒ๋‹จ ๋…ธ๋“œ์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ
    int here = pq.top().second;//pq ์ตœ์ƒ๋‹จ ๋…ธ๋“œ์˜ ๋ฒˆํ˜ธ
    pq.pop();//ํ•ด๋‹น ๋…ธ๋“œ์ •๋ณด๋Š” ์ œ๊ฑฐ
    //here๊นŒ์ง€ ์˜ค๋Š” cost๋ณด๋‹ค ์งง์€ ๊ฒฝ๋กœ๊ฐ€ ์ด๋ฏธ ๋ฐœ๊ฒฌ๋˜์—ˆ์œผ๋ฉด ๋‹ค์Œ ๋…ธ๋“œ๋กœ..
    if(dist[here] < cost) continue;
    for(int i=0; i<adj[here].size(); ++i) {//here ์ธ์ ‘ ๋…ธ๋“œ์— ๋Œ€ํ•ด
      int there = adj[here][i].first;//์ธ์ ‘๋…ธ๋“œ๋ฒˆํ˜ธ๋ฅผ there์— ์ €์žฅ
      //here๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฒฝ๋กœ cost์™€ here์—์„œ there๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ ํ•ฉ์„ nextDist๋ผ ํ•  ๋•Œ 
      int nextDist = cost + adj[here][i].second;
      //๋” ์งง์€ ๊ฒฝ๋กœ๊ฐ€ ์žˆ๋‹ค๋ฉด 
      if(dist[there] > nextDist) {
        dist[there] = nextDist; //๊ฐฑ์‹ 
        pq.push(make_pair(-nextDist, there));//์šฐ์„ ์ˆœ์œ„ ํ์— ์ถ”๊ฐ€
      }
    }
  }
  return dist;//์ตœ๋‹จ๊ฑฐ๋ฆฌ ๋ฐ˜ํ™˜
}