BOJ-TSP: 외판원 순회 시리즈

  1. https://www.acmicpc.net/problem/2098
  2. https://www.acmicpc.net/problem/10971
  3. https://www.acmicpc.net/problem/16991
  • 2월동안 187문제 풀기(지난달 누적 87개) (4/187)
  • 일단 외판원 순회 1을 비트마스크 DP로 풀면 2도 공짜다.
  • 종료조건시에 0을 고려하는 부분을 생각하지 않아서 삽질을 많이함.
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
#include <bits/stdc++.h>
using namespace std;

long long N, arr[16][16], dp[16][1<<16];

long long go(int c, int mask) {
  if(mask == (1<<N)-1) return arr[c][0] ? arr[c][0] : INT_MAX;
  if(dp[c][mask] != -1) return dp[c][mask];

  long long ans = INT_MAX;
  for(int i = 0; i < N; i++) {
    if(mask & 1 << i) continue;
    if(!arr[c][i]) continue;
    ans = min(ans, go(i, mask | 1 << i) + arr[c][i]);
  }
  return dp[c][mask] = ans;
}

int main() {
  cin >> N;
  for(int i =0 ;i  < N; i++)
    for (int j = 0; j < N; j++)
      cin >> arr[i][j];
  for(int i = 0; i < N; i++)
    for (int j = 0; j < (1<<N); j++)
      dp[i][j] = -1;
  cout << go(0,1);
}
  • 외판원 순회 3의 경우는 그냥 거리 계산만 다르게 해주면 된다.
  • 다만 실수처리는 잘 할것.
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
32
33
34
35
#include <bits/stdc++.h>
using namespace std;

struct Point {
  int x, y;
} arr[16];
int N;
double dp[16][1<<16];
const double NV = 1e12;

double dist(int a, int b) {
  return sqrt((arr[b].x-arr[a].x)*(arr[b].x-arr[a].x) + (arr[b].y-arr[a].y)*(arr[b].y-arr[a].y));
}

double go(int c, int mask) {
  if(mask == (1<<N)-1) return dist(c, 0);
  if(dp[c][mask] != NV) return dp[c][mask];

  double ans = NV;
  for(int i = 0; i < N; i++) {
    if(mask & 1 << i) continue;
    ans = min(ans, go(i, mask | 1 << i) + dist(c,i));
  }
  return dp[c][mask] = ans;
}

int main() {
  cin >> N;
  for(int i =0 ;i  < N; i++)
      cin >> arr[i].x >> arr[i].y;
  for(int i = 0; i < N; i++)
    for (int j = 0; j < (1<<N); j++)
      dp[i][j] = NV;
  printf("%.9lf", go(0,1));
}