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. JAVA_String、StringBuilder、StringBuffer区别

    String.StringBuilder.StringBuffer均为字符串 类 需要注意的一些问题 String StringBuilder StringBuffer 一旦创建,不能对其内容进行更改 ...

  2. String类的一些常见的获取方法(5)

     String s = "aasfasfdtgsrast"; 1: int a = s.length() //返回字符串的长度 2: char s1 = charAt(int in ...

  3. 支持向量机SVM(Support Vector Machine)

    支持向量机(Support Vector Machine)是一种监督式的机器学习方法(supervised machine learning),一般用于二类问题(binary classificati ...

  4. foreach循环中为什么不要进行remove/add操作

    先来看一段代码,摘自阿里巴巴的java开发手册 List<String> a = new ArrayList<String>(); a.add("1"); ...

  5. SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

    "F:\program files (x86)\Python35\python.exe" "F:/program files (x86)/JetBrains/Seleni ...

  6. 翻译:MLAPP(2.1节)

    笔者:尝试翻译MLAPP(Machine Learning: a Probabilistic Perspective)一书,供机器学习的学者参考,如有错误理解之处请指出,不胜感激!(如需转载,请联系本 ...

  7. ch5-Class 与 Style 绑定(v-bind:class v-bind:style)

    数据绑定一个常见需求是操作元素的 class 列表和它的内联样式. 因为它们都是属性 ,我们可以用v-bind 处理它们:只需要计算出表达式最终的字符串. 不过,字符串拼接麻烦又易错.因此,在 v-b ...

  8. python apschedule安装使用与源码分析

    我们的项目中用apschedule作为核心定时调度模块.所以对apschedule进行了一些调查和源码级的分析. 1.为什么选择apschedule? 听信了一句话,apschedule之于pytho ...

  9. webpack-dev-server配置指南(使用webpack3.0)

    最近正在研究webpack,听说webpack可以自己搭建一个小型的服务器(使用过vue-cli的朋友应该都见识到过),所以迫不及待的想要尝试一下.不过,在实际操作中发现,用webpack搭建服务器仍 ...

  10. iOS 简单socket连接

    - (void)CreateSocket{ NSString *host = [self.realStreamDict objectForKey:@"StreamSeverIP"] ...