题目链接: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.对于每一个点, 有三种状态:没有被访问、被访问一次、被访问两次。所以需要用三进制进行压缩。
2.dp[status][last]表示:在状态为status下(即走了多少个点,以及每个点走了多少次),最后走的是last点的最小花费。
3.对于一个已有状态:dp[s][j],枚举每一个点k,如果这个点k不是j,而k、j有边,且k被访问的次数不超过2次,那么下一步就可以访问k点。
4.类似的题目:HDU4856
 
 
代码如下:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = +; int n, m;
int g[MAXN][MAXN], c[MAXN], dp[][]; int main()
{
c[] = ;
for(int i = ; i<=; i++) //计算三进制
c[i] = c[i-]*; while(scanf("%d%d", &n, &m)!=EOF)
{
memset(g, -, sizeof(g));
for(int i = ; i<=m; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
u--; v--;
if(g[u][v]!=-) w = min(w, g[u][v]);
g[u][v] = g[v][u] = w;
} memset(dp, -, sizeof(dp));
for(int i = ; i<n; i++)
dp[c[i]][i] = ; int ans = INF;
for(int s = ; s<c[n]; s++)
for(int j = ; j<n; j++)
{
if(dp[s][j]==-) continue; //此状态不存在, 跳过 int cnt = ;
for(int k = ; k<n; k++)
{
if(j!=k && g[j][k]!=- && (s/c[k])%< )
{
int tmp = s + c[k]; //递推出下一个状态
if(dp[tmp][k]==- || dp[tmp][k]>dp[s][j]+g[j][k]) //更新
dp[tmp][k] = dp[s][j] + g[j][k];
} if((s/c[k])%) cnt++; //统计状态s下走了多少个点
} if(cnt==n) ans = min(ans, dp[s][j]); //如果状态s下走完所有点, 则更新答案
}
printf("%d\n", ans==INF?-:ans);
}
}

最新文章

  1. eclipse编辑jsp保存的时候特别卡解决办法
  2. angular路由 模块 依赖注入
  3. python——进程基础
  4. UVa 11889 Benefit(数论)
  5. RecyclerView使用时遇到的问题
  6. [转]BEHAVOUR TREE
  7. jQuery获取属性之自己遇到的问题
  8. saltstack:使用教程之一安装及客户端返回写入MySQL
  9. jenkins 集成 redmine 账户验证的方案
  10. POCO Controller
  11. hibulder中使用git教程
  12. JavaWeb网上商城项目中sql语句问题的解决
  13. virtualbox下centos虚拟机安装,并网卡配置桥接方式上网,使得和host可以互Ping通。
  14. npm 离线安装依赖
  15. codeforces895E. Eyes Closed
  16. MXNET:权重衰减
  17. _event_active_team
  18. CCF CSP 201412-4 最优灌溉
  19. React ref的用法
  20. c语言简单实现telnet客户端

热门文章

  1. Python Tornado简单的http request
  2. python018 Python3 输入和输出
  3. P2866 糟糕的一天
  4. CodeForces 556 --Case of Fake Numbers
  5. Divide Groups(分组)(二分图染色)
  6. 【Codeforces Round #501 (Div. 3)】
  7. free delete malloc new(——高品质量程序设计指南第16章)
  8. CPM、CPC、CPA、PFP、CPS、CPL、CPR介绍
  9. vagrant的学习 之 优化
  10. oc温习六:预处理指令