Travelling

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

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

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#define M 25
#define INF 0x1f1f1f1f
#define N 65000
using namespace std;
int n,m;
int state[M]={0,1,3,9,27,81,243,729,2187,6561,19683,59049};//state[i]表示第i个点城市状态用3进制表示是多少
int dis[N][M];//dis[i][j]表示i状态j点走过几次
int dp[N][M];//dp[i][j]表示第i个状态最后到达j的最小距离
int g[M][M];//表示两点间的距离
/*
寻找最短路径用了floyd算法的原理,求两个点之前的最短距离,不断寻找中间点来缩小权值;
状态转移:遍历所有的三进制状态,找到上一个状态(第j个城市没去过的或者去过一次的)然后转移到这个状态j位置上加一的状态上,但是能不能转移
要看本次转移是不是会缩短时间
*/
int main()
{
//freopen("in.txt","r",stdin);
for(int i=0;i<59050;i++)
{
int x=i;
for(int j=1;j<=10;j++)//最多有十位
{
dis[i][j]=x%3;
x/=3;
if(x==0)
break;
//cout<<j<<endl;
}
//cout<<i<<endl;
}//初始化第i个状态的j位上的数是几
while(scanf("%d%d",&n,&m)!=EOF)
{
int a,b,c;
memset(g,INF,sizeof g);
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;
}//处理输入
memset(dp,INF,sizeof dp);
int tol=pow(3.0,n);
for(int i=1;i<=n;i++) dp[state[i]][i]=0;//初始化从最后到达i城市最小距离的这个状态转移到i城市需要的最小时间就是0;
int cur=INF;
//下面求最短路径的原理是floyd算法(两个点之间不断寻找中间点来缩小权值)
for(int i=0;i<tol;i++)//枚举所有可能的状态
{
int flag=1;;//用于判断当前状态是不是没走
for(int j=1;j<=n;j++)//枚举你需要找的中间过程的城市
{
if(dis[i][j]==0) flag=0;//判断这个是不是
if(dp[i][j]==INF) continue;
//cout<<"dis[i][j]="<<dis[i][j]<<endl;
for(int k=1;k<=n;k++)//枚举当前当前状态(就是你要转移到的状态)
{
if(j==k) continue;
// cout<<"come in"<<endl;
if(g[j][k]!=INF&&dis[i][k]<2)//从j到k的道路是通的,并且在这个状态中k点经过的次数不到两次
{
int newstate=i+state[k];//新的状态就是走到这个城市,给k点经过的次数加一
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)//如果取到正无穷就是那么没有联通路所以就不可能走完
puts("-1");
else
printf("%d\n",cur);
}
return 0;
}

  

hdu 3001 Travelling(状态压缩 三进制)的更多相关文章

  1. HDU 3001【状态压缩DP】

    题意: 给n个点m条无向边. 要求每个点最多走两次,要访问所有的点给出要求路线中边的权值总和最小. 思路: 三进制状态压缩DP,0代表走了0次,1,2类推. 第一次弄三进制状态压缩DP,感觉重点是对数 ...

  2. HDU 3001(状态压缩dp)

    状态压缩dp的第一题! 题意:Mr ACMer想要进行一次旅行,他决定访问n座城市.Mr ACMer 可以从任意城市出发,必须访问所有的城市至少一次,并且任何一个城市访问的次数不能超过2次.n座城市间 ...

  3. Hdu 3001 Travelling 状态DP

    题目大意 一次旅游,经过所有城市至少一次,并且任何一座城市访问的次数不能超过两次,求最小费用 每个城市最多访问两次,用状态0,1,2标识访问次数 把城市1~N的状态按照次序连接在一起,就组成了一个三进 ...

  4. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

  5. hdu 5724 SG+状态压缩

    Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  6. HDU 3001 Travelling(状态压缩DP+三进制)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 题目大意:有n个城市,m条路,每条路都有一定的花费,可以从任意城市出发,每个城市不能经过两次以上 ...

  7. HDU 3001 Travelling (三进制状态压缩 DP)

    题意:有 n 个city,能够选择任一城市作为起点,每一个城市不能訪问超过2次, 城市之间有权值,问訪问所有n个城市须要的最小权值. 思路:由于每一个城市能够訪问最多两次,所以用三进制表示訪问的状态. ...

  8. hdu 3001 Travelling (三进制)【状压dp】

    <题目链接> 题目大意: 给出n个点和m条边,求经过所有点所需的最小花费,每个点最多经过两次. 解题分析: TSP问题类型,由于此题每个点有三种状态,所以采用三进制状态压缩,0.1.2 分 ...

  9. HDU - 3001 Travelling(三进制状压dp)

    Travelling After coding so many days,Mr Acmer wants to have a good rest.So travelling is the best ch ...

随机推荐

  1. Opengl4.5 中文手册—C

    索引 A      B    C      D     E     F     G H      I    J      K     L     M     N O      P    Q      ...

  2. Linux学习——shell编程之环境变量配置文件

    小白学习,在学习中总结! shell编程之环境变量配置文件 一:环境变量配置文件 1 shell编程之环境变量配置 变量类型: 用户自定义变量(本地变量) 环境变量 :定义每个用户的操作环境,如pat ...

  3. Spring之注解实现aop(面向切面编程)

    1:Aop(aspect object programming)面向切面编程,名词解释:    1.1:功能:让关注点代码与业务逻辑代码分离    1.2:关注点        重复代码就叫做关注点  ...

  4. 分享基于分布式Http长连接框架--架构模型

    我画了个简单的架构图来帮助说明: 其实为发布订阅架构模式. 生产者和消费者我们统一可理解为客户端,消息中间件可认为是服务端. 生产者和消费者做为客户端要跟服务端交互,则先通过代理订阅服务端,订阅成功后 ...

  5. bzoj1143 祭祀river(最大独立集)

    [CTSC2008]祭祀river Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2175  Solved: 1098[Submit][Status] ...

  6. 关于http与https区别

    http与https: http叫超文本传输协议,信息为明文传输.https是具有安全性的传输协议,是由http+ssl层,需要到ca申请证书,一般需要费用.信息为加密传输,需要验证用户身份.二者的端 ...

  7. SQLite中的时间日期函数

    SQLite包含了如下时间/日期函数: datetime().......................产生日期和时间 date()...........................产生日期 t ...

  8. Jquery使用mouseenter和mouseleave实现鼠标经过弹出层且可以点击

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Jquery使用mousee ...

  9. 强大的grep,sed和awk--用案例来讲解

    准备工作: 先简单了解grep,sed和awk功能 1) grep 显示匹配特定模式的内容 grep -v 'boy' test.txt 过滤掉test.txt文件的boy,显示其余内容 grep ' ...

  10. npm install 时--save-dev和--save的区别

    package.json中两个字段含义简介 一直在使用npm包管理器,对于npm install module --save-dev 和 npm install module --save这两个的区别 ...