标签:alt fine can efi print space amp gif ide
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define inf 1000000000 5 using namespace std; 6 int ans=inf,n,m,num[5000],dp[5000][15],dis[15][15],dvs[5000][5000],ff[5000][15]; 7 int mmin(int a,int b){ 8 if(a==-1)return b; 9 if(b==-1)return a; 10 return min(a,b); 11 } 12 int main() 13 { 14 scanf("%d%d",&n,&m); 15 int mass=(1<<n)-1; 16 for(int i=1;i<=n;i++)num[1<<(i-1)]=i; 17 for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)dis[i][j]=inf; 18 for(int i=1;i<=n;i++)dis[i][i]=0; 19 for(int i=1;i<=m;i++){ 20 int u,v,w;scanf("%d%d%d",&u,&v,&w); 21 dis[u][v]=min(dis[u][v],w); 22 dis[v][u]=dis[u][v]; 23 } 24 if(n==1){ 25 printf("0\n");return 0; 26 } 27 for(int k=1;k<=n;k++) 28 for(int i=1;i<=n;i++) 29 for(int j=1;j<=n;j++)dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); 30 for(int s1=1;s1<=mass;s1++){ 31 for(int i=1;i<=n;i++){ 32 ff[s1][i]=inf; 33 for(int j=1;j<=n;j++)if((s1&(1<<(j-1)))>0)ff[s1][i]=min(ff[s1][i],dis[i][j]); 34 } 35 } 36 for(int s1=0;s1<=mass;s1++) 37 { 38 dvs[s1][0]=0; 39 for(int s2=1;s2<=mass;s2++)dvs[s1][s2]=inf; 40 } 41 for(int s1=1;s1<=mass;s1++){ 42 for(int s2=1;s2<=mass;s2++)if((s1&s2)==0){ 43 int j=num[s2&(-s2)]; 44 dvs[s1][s2]=dvs[s1][s2-(s2&(-s2))]+ff[s1][j]; 45 //printf("%d %d %d %d\n",s1,s2,j,dvs[s1][s2]); 46 } 47 } 48 memset(dp,-1,sizeof(dp)); 49 for(int i=1;i<=n;i++)dp[1<<(i-1)][0]=0; 50 for(int i=0;i<=n;i++) 51 for(int sta=1;sta<=mass;sta++){ 52 int vs=sta^mass,v1=vs; 53 if(dp[sta][i]==-1)continue; 54 while(vs>0){ 55 if(dvs[sta][vs]<inf) 56 dp[sta|vs][i+1]=mmin(dp[sta|vs][i+1],dp[sta][i]+dvs[sta][vs]*(i+1)); 57 //printf("==%d %d %d %d %d\n",sta,vs,sta|vs,i+1,dp[sta|vs][i+1]); 58 vs=v1&(vs-1); 59 } 60 } 61 for(int i=1;i<=n+1;i++)ans=mmin(ans,dp[mass][i]); 62 printf("%d\n",ans); 63 return 0; 64 }
标签:alt fine can efi print space amp gif ide
原文地址:http://www.cnblogs.com/wxxlouisa/p/8007255.html