Travelling

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.

InputThere 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.OutputOutput 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 题意:每个点最多去两次,给出边权,求从任意一点开始经过所有点的最短路。 经典TSP问题,只不过最多一次的条件变为两次,这里可以用三进制思想解决。
three[n]表示n个地点的全部状态,0没去过,1去过一次,2去过两次,dig[i][j]记录在i状态下第j位的数字(0-2)。
#include<bits/stdc++.h>
#define MAX 12
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll; int a[MAX][MAX];
int three[MAX];
int dig[][MAX];
int dp[][MAX]; void init(){
three[]=;
for(int i=;i<=;i++){
three[i]=three[i-]*;
}
for(int i=;i<three[];i++){
int ii=i,c=-;
while(ii){
c++;
dig[i][c]=ii%;
ii/=;
}
}
}
int main()
{
int t,n,m,i,j,k;
int x,y,z;
init();
while(~scanf("%d%d",&n,&m)){
memset(a,INF,sizeof(a));
for(i=;i<m;i++){
scanf("%d%d%d",&x,&y,&z);
a[x][y]=a[y][x]=min(a[x][y],z);
}
memset(dp,INF,sizeof(dp));
for(i=;i<n;i++){
dp[three[i]][i]=;
}
for(i=;i<three[n];i++){
for(j=;j<n;j++){
if(dig[i][j]==) continue;
for(k=;k<n;k++){
if(j==k||dig[i][k]==) continue;
dp[i][j]=min(dp[i][j],dp[i-three[j]][k]+a[k+][j+]);
}
}
}
int ans=INF;
for(i=;i<three[n];i++){
int f=;
for(j=;j<n;j++){
if(dig[i][j]==){
f=;
break;
}
}
if(f==) continue;
for(j=;j<n;j++){
ans=min(ans,dp[i][j]);
}
}
if(ans==INF) printf("-1\n");
else printf("%d\n",ans);
}
return ;
}

HDU - 3001 Travelling(三进制状压dp)的更多相关文章

  1. Travelling (三进制+状压dp)

    题目链接 #include <bits/stdc++.h> using namespace std; typedef long long ll; inline ll read(){ ,f= ...

  2. hdu 3001(三进制状压)

    题目 解法 看到这道题,我们就会想到旅行商问题.但是这里每一个点可以经过最多两次,所以我们用三进制表示就好了. 代码 #include <iostream> #include <cs ...

  3. HDU 3001 三进制状压DP

    N个城市,M条道路,每条道路有其经过的代价,每一个城市最多能够到达两次,求走全然部城市最小代价,起点随意. 三进制状压.存储每一个状态下每一个城市经过的次数. 转移方程: dp[i+b[k]][k]= ...

  4. ZRDay6A. 萌新拆塔(三进制状压dp)

    题意 Sol 这好像是我第一次接触三进制状压 首先,每次打完怪之后吃宝石不一定是最优的,因为有模仿怪的存在,可能你吃完宝石和他打就GG了.. 因此我们需要维护的状态有三个 0:没打 1:打了怪物 没吃 ...

  5. hdu 3001 Travelling 经过所有点(最多两次)的最短路径 三进制状压dp

    题目链接 题意 给定一个\(N\)个点的无向图,求从任意一个点出发,经过所有点的最短路径长度(每个点至多可以经过两次). 思路 状态表示.转移及大体思路 与 poj 3311 Hie with the ...

  6. HDU 3001 三进制 状压dp

    Travelling Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  7. UVA 10817 - Headmaster's Headache(三进制状压dp)

    题目:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=20&pag ...

  8. 三进制状压 HDOJ 3001 Travelling

    题目传送门 题意:从某个点出发,所有点都走过且最多走两次,问最小花费 分析:数据量这么小应该是状压题,旅行商TSP的变形.dp[st][i]表示状态st,在i点时的最小花费,用三进制状压.以后任意进制 ...

  9. Codeforces Round #297 (Div. 2) [ 折半 + 三进制状压 + map ]

    传送门 E. Anya and Cubes time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

随机推荐

  1. 相比ICO,DAICO主要有这两方面优势

    都说ICO已死,很有一部分人对无币区块链持保留态度,自从V神提出DAICO一来,大家似乎看到了新的方向,不少项目围绕其展开.那对比ICO,DAICO有哪些优势呢?主要是以下两点: DAICO维护了投资 ...

  2. PAT 天梯赛 L2-028. 秀恩爱分得快 【数据处理】

    题目链接 https://www.patest.cn/contests/gplt/L2-028 思路 0.只处理被询问的情侣的亲密度,否则会超时 1.要注意输入数字要用字符串,还要标记性别 因为 输出 ...

  3. ubuntu下安装redis以及redis客户端在mac下的使用

    ubuntu下安装redis http://blog.fens.me/linux-redis-install/ 此方式利用brew安装包去获取最新的rdm客户端 资源失效了 https://www.j ...

  4. LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式 的解决方法

    一.案例1,及解决方案: "LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式." ...

  5. Zabbix的snmp监控一些snmp常用的一些OID (KEY)

    摘自: http://www.iyunv.com/thread-167287-1-1.html System Group sysDescr 1.3.6.1.2.1.1.1 sysObjectID 1. ...

  6. kvm初体验之二:安装

    Host: CentOS release 6.4 (Final) 1. 开启处理器的虚拟化功能 进入BIOS,使能虚拟化功能: 进入linux, grep -E "vmx|svm" ...

  7. phpunit 入门

    话说,使用phpunit还是处于好奇之心,为什么以前一直没使用呢?主要是嫌麻烦,小项目,前后端都是自己写,几个人就完成的工作,没必要弄的那么麻烦.好了,废话不多说,让我们一起来感受下phpunit吧! ...

  8. python-常用内置函数与装饰器

    1.常用的python函数 abs             求绝对值 all               判断迭代器中所有的数据是否为真或者可迭代数据为空,返回真,否则返回假 any          ...

  9. lucene内置的评分函数

    For multiterm queries, Lucene takes the Boolean model, TF/IDF, and the vector space model and combin ...

  10. SpringBoot_00_资源汇总贴

    一.精选 1.Spring Boot文档 二.参考资料 1.springboot-learning-example 2.Spring boot 那些事 3.Spring Boot干货系列 4.Spri ...