标签:des style blog http io color ar os sp
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4166 Accepted Submission(s): 1339
状压dp、仔细想一下
#include <iostream> #include <cstdio> #include <cstring> using namespace std; #define INF 0x3f3f3f3f int n,m; int s[12]; int mpt[12][12]; int tr[12][60000]; int dp[12][60000]; void init() //预处理s和tr数组 { int i,j,t; s[0]=1; for(i=1;i<=10;i++) { s[i]=3*s[i-1]; } for(j=0;j<s[10];j++) { t=j; for(i=0;i<10;i++) { tr[i][j]=t%3; t/=3; } } } void solve() { int i,j,k,MAX=s[n]; for(i=0;i<n;i++) { dp[i][s[i]]=0; } for(j=0;j<MAX;j++) { for(i=0;i<n;i++) { if(tr[i][j]==0) continue; for(k=0;k<n;k++) { if(i==k) continue; if(tr[k][j]==0) continue; int prej=j-s[i]; dp[i][j]=min(dp[i][j],dp[k][prej]+mpt[k][i]); } } } int ans=INF; for(i=0;i<n;i++) { for(j=0;j<s[n];j++) { for(k=0;k<n;k++) { if(tr[k][j]==0) break; } if(k==n) ans=min(ans,dp[i][j]); } } if(ans==INF) puts("-1"); else printf("%d\n",ans); } int main () { init(); while(scanf("%d%d",&n,&m)!=EOF) { memset(dp,INF,sizeof(dp)); memset(mpt,INF,sizeof(mpt)); while(m--) { int a,b,c; scanf("%d%d%d",&a,&b,&c); a--; b--; if(c<mpt[a][b]) { mpt[a][b]=c; mpt[b][a]=c; } } solve(); } return 0; }
标签:des style blog http io color ar os sp
原文地址:http://www.cnblogs.com/hate13/p/4104729.html