# openjudge 海贼王之伟大航路(luoge.cpp/1000ms/128M)

“我是要成为海贼王的男人！”，路飞一边喊着这样的口号，一边和他的伙伴们一起踏上了伟大航路的艰险历程。

4
0 10 20 999
5 0 90 30
99 50 0 10
999 1 2 0

5
0 18 13 98 8
89 0 45 78 43
22 38 0 96 12
68 19 29 0 52
95 83 21 24 0

100

137

1,2,3,4,5: 18+45+96+52=211
1,2,4,3,5: 18+78+29+12=137
1,3,2,4,5: 13+38+78+52=181
1,3,4,2,5: 13+96+19+43=171
1,4,2,3,5: 98+19+45+12=174
1,4,3,2,5: 98+29+38+43=208

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<ios>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<functional>
#include<bitset>
using namespace std;
const int maxn=20,inf=1e9;
int a[maxn][maxn],n,p[maxn];
int ans=inf,mine=inf;
void dfs(int x,int step,int k){
if(x==n){
if(step<ans) ans=step;
return;
}
if(step+mine*(n-1-k)>=ans) return;
int i;
for(i=1;i<n;i++)
if(!p[i]){
p[i]=true;
dfs(i,step+a[x][i],k+1);
p[i]=false;
}
if(k==n-2)
dfs(n,step+a[x][n],k+1);
return;
}
int main(){
int i,j,k,m;
freopen("luoge.in","r",stdin);
freopen("luoge.out","w",stdout);
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++){
scanf("%d",&a[i][j]);
if(i!=j && a[i][j]<mine)
mine=a[i][j];
}
p[1]=true;
dfs(1,0,0);
printf("%d\n",ans);
return 0;
}

//借用罗大神的代码^_^
#include<iostream>
#include<cstdio>
using namespace std;
const int N=18,INF=0x3fffffff;
int n;
struct graph
{
int map[N][N];
int f[1<<N][N];
void init()
{
for(int i=0;i<(1<<n);i++)
for(int j=1;j<=n;j++)
f[i][j]=INF;
f[1][1]=0;
}
{
scanf("%d",&n);
init();
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&map[i][j]);
}
void getans()
{
for(int i=1;i<(1<<n);i++)
for(int j=1;j<=n;j++)
{
if(f[i][j]==INF)continue;
for(int k=1;k<=n;k++)
{
if(i&(1<<(k-1)))continue;
int ti=i|(1<<(k-1));
f[ti][k]=min(f[ti][k],f[i][j]+map[j][k]);
}
}
int ans=f[(1<<n)-1][n];
printf("%d\n",ans);
}
}d;
int main()
{
freopen("luoge.in","r",stdin);
freopen("luoge.out","w",stdout);
d.getans();
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120