Travelling
Time limit:6000/3000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 6600 Accepted Submission (s): 2144
Problem Descriptionafter Coding so many DAYS,MR Acmer wants to has a good rest. So travelling are 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 is M roads here,following a fee as usual. But Mr. Acmer gets bored so easily the he doesn ' t want to visit a city more than twice! And he's so mean, he wants to minimize, the total fee! He is lazy. So he turns.
Inputthere was several test cases,the first line is a intergers n (1<=n<=10) and M,which means he needs to visit n Cities and there is M roads he can choose,then m lines Follow,each line would include three Intergers A, B and C (1<=a,b& Lt;=n), means there is a road between A and B and the cost are 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 10
Sample Output100907
Source multi-university Training Contest 11-host by Hrbeu
#include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #define M 25#define INF 0x1f1f1f1f#define N 65000using namespace Std;int n,m;int state[m]={0,1,3,9,27,81,243,729,2187,6561,19683,59049};/ /state[i] denotes the first point of the city state with a 3 binary representation of how much int dis[n][m];//dis[i][j] represents I state J points through several times int dp[n][m];//dp[i][j] represents the first I state finally reaches the minimum distance of J int g[m][ m];//represents the distance between two points//find the shortest path using the principle of the Floyd algorithm, find the shortest distance before two points, constantly looking for the middle point to reduce the weight; State shift: Traverse all three-state, Find the previous state (the J city has not been to or once) and then transfer to this state J position plus a state, but can not be transferred to see if this transfer will shorten the time */int main () {//freopen ("In.txt", "R", stdin); for (int i=0;i<59050;i++) {int x=i;for (int j=1;j<=10;j++)//up to 10 bits {dis[i][j]=x%3;x/=3;if (x==0) break;//cout< <j<<endl;} Cout<<i<<endl;} The number of J bits that initialize the status of I is a few while (scanf ("%d%d", &n,&m)!=eof) {int a,b,c;memset (g,inf,sizeof g), and for (int i=0;i<m;i++ {scanf ("%d%d%d", &a,&b,&c), if (c<g[a][b]) g[a][b]=g[b][a]=c;} Processing input memset (dp,inf,sizeof dp); int Tol=pow (3.0,n); for (int i=1;i<=n;i++) dp[state[i]][i]=0;// Initialize this state from the last to reach I city minimum distance transfer to I city needThe minimum time is 0;int cur=inf;//the principle of the shortest path is the Floyd algorithm (two points constantly looking for intermediate points to reduce the weight) for (int i=0;i<tol;i++)//Enumerate all possible states {int flag=1;;/ /used to determine whether the current state is not walking for (int j=1;j<=n;j++)//enumeration You need to find the middle process of the city {if (dis[i][j]==0) flag=0;//judge If this is not if (Dp[i][j]==inf) continue;//cout<< "dis[i][j]=" <<dis[i][j]<<endl;for (int k=1;k<=n;k++)//enumerates the current current state (which is the state you want to transfer to) {if (j==k) continue;//cout<< "Come In" <<endl;if (G[J][K]!=INF&&DIS[I][K]<2)//The road from J to K is pass, And in this state, the number of K points passed less than two times {int newstate=i+state[k];//new state is to go to the city, the number of K-point passes plus a dp[newstate][k]=min (dp[newstate][k],dp[i][ J]+g[j][k]);} cout<< "dp[newstate][k]=" <<DP[NEWSTATE][K]<<ENDL;} Cout<<j<<endl;} if (flag) for (int j=1;j<=n;j++) cur=min (dp[i][j],cur);} if (Cur==inf)//If you take the positive infinity is so there is no link road so there is no way to finish puts ("1"); elseprintf ("%d\n", cur);} return 0;}
HDU 3001 Travelling (state compression three-in)