AcWing 731. 毕业旅行问题 java 状态DP
原题链接
中等
作者:
henhen敲
,
2020-06-21 17:32:08
,
所有人可见
,
阅读 1000
import java.io.*;
class Main{
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static int nextInt()throws Exception{
in.nextToken();
return (int)in.nval;
}
public static void main(String[] args)throws Exception{
int n = nextInt();
int[][] g = new int[n][n];
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
g[i][j] = nextInt();
int[][] f = new int[1<<n][n];
for(int i=2; i<(1<<n); i++)
for(int j=0; j<n; j++)
if(((i>>j)&1)==1){
int m = i - (1<<j);
int min = 0x3f3f3f3f;
int t, k;
for(k=m, t=0; k!=0; k>>=1, t++)
if((k&1)==1)
min = Math.min(min, f[m][t]+g[t][j]);
f[i][j] = min;
}
int ans = 0x3f3f3f3f;
for(int i=1; i<n; i++)
ans = Math.min(ans, f[(1<<n)-1][i] + g[i][0]);
out.print(ans);
out.close();
}
}