标签:des style os io for art 代码 ar
Description
Input
Output
Sample Input
2 1 1 2 100 3 2 1 2 40 2 3 50 3 3 1 2 3 1 3 4 2 3 10
Sample Output
100 90 7
题意:一个Acmer要出去旅行,有n个城市m条路,一个城市不能去2次以上,求经过所有城市一次的最短路。
思路:3进制计算,就是要开一个数组存3进制数了。
AC代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <stdlib.h>
using namespace std;
const int INF=0x1f1f1f1f;
int mp[15][15];
int dp[59050][15];
int pow3[12]={0,1,3,9,27,81,243,729,2187,6561,19683,59049};
int n,m;
int p[59050][15];
int main(){
for(int i=0;i<59050;i++){
int temp=i;
for(int j=1;j<=10;j++){
p[i][j]=temp%3;
temp/=3;
if(temp==0) break;
}
}
while(~scanf("%d%d",&n,&m)){
memset(mp,INF,sizeof(mp));
for(int i=0;i<m;i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(c<mp[a][b])//竟然有多重边T_T,坑死我啦;
mp[a][b]=mp[b][a]=c;
}
memset(dp,INF,sizeof(dp));
for(int i=1;i<=n;i++) dp[pow3[i]][i]=0;
bool flag;
int ans=INF;
for(int s=0;s<pow3[n+1];s++){
flag=true;
for(int i=1;i<=n;i++){
if(p[s][i]==0) flag=false;
if(dp[s][i]==INF) continue;
for(int j=1;j<=n;j++){
if(i==j) continue;
if(p[s][j]>=2) continue;
if(mp[i][j]==INF) continue;
int ts=s+pow3[j];
dp[ts][j]=min(dp[ts][j],dp[s][i]+mp[i][j]);
}
}
if(flag){
for(int k=1;k<=n;k++){
ans=min(ans,dp[s][k]);
}
}
}
if(ans==INF) printf("-1\n");
else printf("%d\n",ans);
}
return 0;
}
HDU 3001 Travelling,布布扣,bubuko.com
标签:des style os io for art 代码 ar
原文地址:http://blog.csdn.net/kimi_r_17/article/details/38301943