티스토리 뷰
https://www.acmicpc.net/problem/2098
2098번: 외판원 순회
첫째 줄에 도시의 수 N이 주어진다. (2 ≤ N ≤ 16) 다음 N개의 줄에는 비용 행렬이 주어진다. 각 행렬의 성분은 1,000,000 이하의 양의 정수이며, 갈 수 없는 경우는 0이 주어진다. W[i][j]는 도시 i에서 j
www.acmicpc.net
const fs = require('fs');
const board = fs.readFileSync("./dev/stdin").toString().split('\n').map(v=>v.split(' ').map(Number))
const [N] = board.shift();
let cost = Array.from(Array(N),()=>Array(1<<N).fill(-1));
const dfs = (now,visited)=>{
if(visited==(1<<N)-1){
if(board[now][0]==0) return Infinity;
else return board[now][0];
}
if(cost[now][visited] != -1) return cost[now][visited];
cost[now][visited] = Infinity;
for(let i = 0; i<N; i++){
if(board[now][i]==0) continue;
if((visited & (1<<i))) continue;
cost[now][visited] = Math.min(cost[now][visited],board[now][i]+dfs(i,visited| (1<<i)));
}
return cost[now][visited];
}
console.log(dfs(0,1));
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js) 백준 1103번: 게임 (0) | 2022.06.06 |
---|---|
Node.js) 백준 17070번: 파이프 옮기기 1 (0) | 2022.06.04 |
Node.js)백준 12852번: 1로 만들기 2 (0) | 2022.04.29 |
Node.js) 백준 15989번: 1, 2, 3 더하기 4 (0) | 2022.04.26 |
Node.js)백준 2240번: 자두나무 (0) | 2022.04.26 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 개발자면접
- BFS
- 투포인터 연습
- 서버점검
- 다이나믹프로그래밍
- 동적프로그래밍
- 면접비
- 서버개발
- 투포인터
- 은둔청년체험
- MySQL
- 다이나밍프로그래밍
- 최소공통조상
- create databases;
- 면접질문
- 롱베케이션
- 로드나인
- node.js
- MOD
- create db
- DB 생성
- 그래프
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함