After coding so many days,Mr Acmer wants to have a good rest.So travelling is the best choice!He has decided to visit n cities(he insists on seeing all the cities!And he does not mind which city being his start station because superman can bring him to any city at first but only once.), and of course there are m roads here,following a fee as usual.But Mr Acmer gets bored so easily that he doesn't want to visit a city more than twice!And he is so mean that he wants to minimize the total fee!He is lazy you see.So he turns to you for help.
InputThere are several test cases,the first line is two intergers n(1<=n<=10) and m,which means he needs to visit n cities and there are m roads he can choose,then m lines follow,each line will include three intergers a,b and c(1<=a,b<=n),means there is a road between a and b and the cost is of course c.Input to the End Of File.OutputOutput the minimum fee that he should pay,or -1 if he can't find such a route.Sample Input2 11 2 1003 21 2 402 3 503 31 2 31 3 42 3 10Sample Output100907题意:
n个城市,m条路径,每条城市最多经过2遍,求n个城市至少走一遍,最少花费多少。方法:3进制状态压缩,之后DP代码:#includeusing namespace std;typedef long long LL;LL f[60000][20];bool v[60000][20];int n,m;LL c[22][22];LL cf[300];inline bool scan_d(int &num) { char in;bool IsN=false; in=getchar(); if(in==EOF) return false; while(in!='-'&&(in<'0'||in>'9')) in=getchar(); if(in=='-'){ IsN=true;num=0;} else num=in-'0'; while(in=getchar(),in>='0'&&in<='9'){ num*=10,num+=in-'0'; } if(IsN) num=-num; return true;}void init(){ cf[0]=1; for(int i=1;i<12;i++) cf[i]=cf[i-1]*3;}LL count(int s[]){ LL ans=0; for(int i=n-1;i>=0;i--){ // cout< <<" "< <<" "<< 0){ s[num++]=x%3; x/=3; }}LL dfs(LL x,int y){// cout< <<" "< < =2) continue; s[i]++; LL np=count(s); // cout<<" \t\t"< <<" "<<" "< <<" "<<<" "<< ans) zans=ans; s[i]--; } f[x][y]=zans; return zans;}int main(){ init(); while(scan_d(n)){ scan_d(m); for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) c[i][j]=-1; for(int i=1;i<=m;i++){ int l,r,w; scan_d(l); scan_d(r); scan_d(w); l--;r--; if(c[l][r]==-1||c[l][r]>w) c[l][r]=w; if(c[r][l]==-1||c[r][l]>w) c[r][l]=w; } LL zans=0x3f3f3f3f; int s[10]; for(int k=0;k