码迷,mamicode.com
首页 > 其他好文 > 详细

HDU3001 Travelling —— 状压DP(三进制)

时间:2017-10-07 17:34:39      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:contest   time   include   hdu   man   desc   super   split   should   

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3001

 

Travelling

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8286    Accepted Submission(s): 2703


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
 
 
 
 
题解:
 
 
代码如下:
技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <vector>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 #define ms(a,b) memset((a),(b),sizeof((a)))
13 using namespace std;
14 typedef long long LL;
15 const double EPS = 1e-8;
16 const int INF = 2e9;
17 const LL LNF = 2e18;
18 const int MAXN = 10+10;
19 
20 int n, m;
21 int g[MAXN][MAXN], c[MAXN], dp[200000][10];
22 
23 int main()
24 {
25     c[0] = 1;
26     for(int i = 1; i<=10; i++)  //计算三进制
27         c[i] = c[i-1]*3;
28 
29     while(scanf("%d%d", &n, &m)!=EOF)
30     {
31         memset(g, -1, sizeof(g));
32         for(int i = 1; i<=m; i++)
33         {
34             int u, v, w;
35             scanf("%d%d%d", &u, &v, &w);
36             u--; v--;
37             if(g[u][v]!=-1) w = min(w, g[u][v]);
38             g[u][v] = g[v][u] = w;
39         }
40 
41         memset(dp, -1, sizeof(dp));
42         for(int i = 0; i<n; i++)
43             dp[c[i]][i] = 0;
44 
45         int ans = INF;
46         for(int s = 1; s<c[n]; s++)
47         for(int j = 0; j<n; j++)
48         {
49             if(dp[s][j]==-1) continue;  //此状态不存在, 跳过
50 
51             int cnt = 0;
52             for(int k = 0; k<n; k++)
53             {
54                 if(j!=k && g[j][k]!=-1 && (s/c[k])%3<2 )
55                 {
56                     int tmp = s + c[k];
57                     if(dp[tmp][k]==-1 || dp[tmp][k]>dp[s][j]+g[j][k])
58                         dp[tmp][k] = dp[s][j] + g[j][k];
59                 }
60 
61                 if((s/c[k])%3) cnt++;   //统计状态s下走了多少个点
62             }
63 
64             if(cnt==n) ans = min(ans, dp[s][j]);    //如果状态s下走完所有点, 则更新答案
65         }
66 
67         printf("%d\n", ans==INF?-1:ans);
68     }
69 }
View Code

 

HDU3001 Travelling —— 状压DP(三进制)

标签:contest   time   include   hdu   man   desc   super   split   should   

原文地址:http://www.cnblogs.com/DOLFAMINGO/p/7635000.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!