标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 891 Accepted Submission(s): 399
#include<bits/stdc++.h> using namespace std; const int INF=0x3f3f3f3f; int d[20][20],dp[(1<<20)][20]; int main(){ int t,n,m,a,b,c; scanf("%d",&t); while(t--){ memset(d,INF,sizeof(d)); memset(dp,INF,sizeof(dp)); scanf("%d%d",&n,&m); for(int i=0;i<m;i++){ scanf("%d%d%d",&a,&b,&c); if(d[a][b]>c){ d[a][b]=d[b][a]=c; } } for(int i=1;i<=n;i++) d[i][i]=0; for(int k=1;k<=n;k++){ for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(d[i][k]<INF&&d[k][j]<INF) d[i][j] = d[i][j]<(d[i][k]+d[k][j])?d[i][j]:(d[i][k]+d[k][j]); } } } dp[1][1]=0; for(int s=1;s<(1<<n);s++){ for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(( (s&(1<<(i-1)))==0 ) &&( (s&(1<<(j-1))) )) dp[s|(1<<(i-1))][i]=min(dp[s|(1<<(i-1))][i],dp[s][j]+d[j][i]); } } } int ans=INF; for(int i=1;i<=n;i++){ ans=min(ans,dp[(1<<n)-1][i]+d[i][1]); } printf("%d\n",ans); } return 0; }
HDU 5418——Victor and World——————【状态压缩+floyd】
标签:
原文地址:http://www.cnblogs.com/chengsheng/p/4780788.html