TravellingTime
limit:6000/3000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3905 Accepted Submission (s): 1234
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 Input
2 11 2 1003 21 2 402 3 503 31 2 31 3 42 3 10
Sample Output
dp+ State compression: Each point can only pass at most 2 times, consider using 3 binary storage state;
#include <stdio.h> #include <math.h> #include <string.h> #include <iostream> #include < algorithm>using namespace std; #define N 12#define M 59050#define LL long longconst int inf=0x1f1f1f1f; Note Initialization value int tri[n]= {0,1,3,9,27,81,243,729,2187,6561,19683,59049};int g[n][n];int dig[m][n]; DIG[I][J] record I state whether J Point appears, several times int dp[m][n]; DP[S][J] Under state S, the shortest distance to J for the end of void Inti ()///To find the corresponding 3 binary bits of each state s {int i,j,t; for (i=1;i<m;i++) {for (t=i,j=1;j<=10;j++) {dig[i][j]=t%3; Find out the number of cities that reach each city in this state t/=3; if (!t) break; }}}int Main () {int i,j,a,b,c; int n,m,s; Inti (); while (scanf ("%d%d", &n,&m)!=-1) {memset (g,inf,sizeof (g)); Memset (Dp,inf,sizeof (DP)); for (i=0;i<m;i++) {scanf ("%d%d%d", &a,&b,&c); G[a][b]=g[b][a]=min (C,g[a][b]); } for (i=1;i<=n;i++)//start state. Be able to either city as a starting point. dp[tri[i]][i]=0; Natural initial distanceto 0 int ans=inf; for (s=1;s<tri[n+1];s++)//The value of the other state is updated when the S state is at the end of I {int f=1; for (i=1;i<=n;i++) {if (dig[s][i]==0)//infer the current state s, whether each city has reached f=0; if (Dp[s][i]==inf) continue; for (j=1;j<=n;j++)//dp[s][i] state to dp[s+tri[j]][j] State {if (g[i][j]==inf| | i==j| | dig[s][j]>=2) continue; int NEWS=S+TRI[J]; Dp[news][j]=min (Dp[news][j],dp[s][i]+g[i][j]); }} if (f) for (i=1;i<=n;i++) ans=min (Ans,dp[s][i]); } if (Ans==inf) ans=-1; printf ("%d\n", ans); } return 0;}
</pre><p></p><p></p><p><span style= "FONT-SIZE:18PX; Color: #33ccff ">bfs+ State compression: </span></p><p><span style=" Color:rgb (51,204,255); Font-size:18px "> starts with each point in the queue, simulating 3 binary processing for each state, and finally + optimization.</span></p><p></p><pre code_snippet_id= "479354" Snippet_file_name= "blog_20141004_2_ 7195338 "name=" code "class=" CPP > #include <iostream> #include <stdio.h> #include <math.h># include<string.h> #include <algorithm> #include <iostream> #include <queue>using namespace std ; #define N 12#define LL long longconst int inf=0x3fffffff;int g[n][n];int n,m,ans;int mark[n][60000];struct node{int x , t,s,cnt; Location, time, status, number of friend bool operator< (node A,node b) {return a.t>b.t; }};int gettmp (int x,int k)///Get x the K bit in the 3 binary is how much {//inferred that the point has passed. After several times the int t; while (x) {t=x%3; k--; if (k==0) break; x/=3; } return k?0:t;} void Inti ()//initialize array {int i,j; for (i=1;i<=n;i++) {for (j=0;j< (int) pow (3,n); j + +) Mark[i][j]=inf; }}void BFS () {int i; priority_queue<node>q; Node Cur,next; for (i=1;i<=n;i++) { Cur.x=i; Cur.s=pow (3, (i-1)); cur.t=0; Cur.cnt=1; Q.push (cur); mark[i][0]=0; } while (!q.empty ()) {cur=q.top (); Q.pop (); for (i=1;i<=n;i++) {if (G[cur.x][i]==inf)//blocked continue; next.cnt=cur.cnt; NEXT.S=CUR.S; Next.t=cur.t+g[cur.x][i]; if (ans<next.t)//optimization is very important continue; Next.x=i; int t=gettmp (next.s,i); The point passed several times, if (t>=2)//After 2 times can not go after the continue; Next.s+=pow (3, (i-1)); This point passes through the number of times plus an if (t==0)//After a new attraction {next.cnt++; if (next.cnt==n) {ans=min (ans,next.t); Continue }} if (Next.t<mark[i][next.s]) {mark[i][next.s]=next.t; Q.push (next); }}}}int Main () {int a,b,c,i,j; while (scanf ("%d%d", &n,&m)!=-1) {for (i=0;i<=n;i++) for (j=1;j<=n;j++) g[i ][j]= (i==j?
0:inf); for (i=0;i<m;i++) {scanf ("%d%d%d", &a,&b,&c); G[a][b]=g[b][a]=min (G[A][B],C); } Ans=inf; Inti (); BFS (); if (Ans==inf) ans=-1; printf ("%d\n", ans); } return 0;}
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Hdu 3001 travelling (tsp problem)