HDU3001 Travelling
Travelling
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7470 Accepted Submission(s): 2430
Problem Description
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.
Input
There
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.
Output
Output the minimum fee that he should pay,or -1 if he can't find such a route.
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
Source
Recommend
使用三进制来表示状态,转移略
目测n进制状压DP应该用刷表?(n > 2)
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 #include <stack> 8 #include <iostream> 9 inline void read(int &x){x = 0;char ch = getchar();char c = ch;while(ch < '0' || ch > '9')c = ch, ch = getchar();while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();if(c == '-')x = -x;} 10 inline int min(int a, int b){return a > b ? b : a;} 11 12 const int INF = 0x3f3f3f3f; 13 const int MAXN = 10 + 1; 14 const int MAXNUM = 59049; 15 16 int pow3[MAXN],n,m,ans,bit[MAXNUM][MAXN],dp[MAXNUM][MAXN]; 17 // 3^i state的第i位 已经经过state,到达i 18 19 int g[MAXN][MAXN],tmp1,tmp2; 20 21 inline void init() 22 { 23 pow3[0] = 1; 24 for(int i = 1;i <= MAXN;++ i)pow3[i] = pow3[i - 1] * 3; 25 for(int i = 0;i < MAXNUM;++ i) 26 { 27 int tmp = i; 28 for(int j = 1;j <= 10 && tmp;j ++) 29 { 30 bit[i][j] = tmp % 3; 31 tmp /= 3; 32 } 33 } 34 } 35 36 inline void empty() 37 { 38 memset(g, 0x3f, sizeof(g)); 39 memset(dp, 0x3f, sizeof(dp)); 40 ans = INF; 41 } 42 43 inline void in() 44 { 45 register int tmp; 46 for(int i = 1;i <= m;++ i) 47 { 48 read(tmp1);read(tmp2);read(tmp); 49 g[tmp1][tmp2] = g[tmp2][tmp1] = min(g[tmp1][tmp2], tmp); 50 } 51 } 52 53 inline void DP() 54 { 55 for(int i = 1;i <= n;i ++) 56 dp[pow3[i - 1]][i] = 0; 57 58 //不是二进制,不方便判断合法性,因此使用刷表法,直接构造合法状态 59 for(int S = 0;S < pow3[n];++ S) 60 { 61 int ok = true; 62 63 for(int i = 1;i <= n;++ i) 64 { 65 if(bit[S][i] == 0)ok = false; 66 67 //无法进行刷表 68 if(bit[S][i] == INF)continue; 69 70 for(int j = 1;j <= n;j ++) 71 { 72 if(i == j)continue; 73 74 if(g[i][j] == INF || bit[S][j] >= 2)continue; 75 int NOWS = S + pow3[j - 1]; 76 77 dp[NOWS][j] = min(dp[NOWS][j], dp[S][i] + g[i][j]); 78 } 79 } 80 if(ok) 81 for(int i = 1;i <= n;++ i) 82 ans = min(ans, dp[S][i]); 83 } 84 } 85 86 inline void out() 87 { 88 if(ans == INF)printf("-1\n"); 89 else printf("%d\n", ans); 90 } 91 92 int main() 93 { 94 bool b = true; 95 init(); 96 while(scanf("%d %d", &n, &m) != EOF) 97 { 98 empty(); 99 in(); 100 DP(); 101 out(); 102 } 103 return 0; 104 }