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. 前端几个笔试题及答案(bd)

    1.   行内元素.块级元素和空元素(void)举例. 块级元素:<address>.<caption>.<dd>.<div>.<dl>.& ...

  2. 搭建vps***

    快速搭建ShadowSocks wget --no-check-certificate -O shadowsocks.sh https://raw.githubusercontent.com/tedd ...

  3. Android Weekly Notes Issue #318

    Android Weekly Issue #318 July 15th, 2018 Android Weekly Issue #318 本期内容包括: Android Navigation Compo ...

  4. American Heritage usaco

    基础题,主要思路是找到根,然后分别递归即可: #include<iostream> #include<cstring> #include<string> #incl ...

  5. debian支持的系统架构介绍

    debian系统支持类型有armel.armhf.i386.amd64.mips.mipsel, powerpc.sparc.s390.s390x等. 详细对比文章见https://www.debia ...

  6. 【转载】帧缓冲驱动程序分析及其在BSP上的添加

    原文地址:(四)帧缓冲驱动程序分析及其在BSP上的添加 作者:gfvvz 一.BSP修改及其分析   1. BSP中直接配置的四个寄存器 S3C6410数据手册的第14.5部分是显示控制器的编程模型部 ...

  7. 郝健: Linux内存管理学习笔记-第1节课【转】

    本文转载自:https://blog.csdn.net/juS3Ve/article/details/80035751 摘要 MMU与分页机制 内存区域(内存分ZONE) LinuxBuddy分配算法 ...

  8. 对C++指针的一个比喻

    假如你身在上海,你的电脑出了一点问题,你解决不了,这时你想起了你的远在北京的朋友小D,此时小D打开了他的心爱的笔记本... c++中函数的参数传指针就像你用teamviewer与你的朋友小D建立连接, ...

  9. openfire调试环境

    导入工程: File->New->project: 选择“Java project from existing ant buildfile” 再从菜单windows->show vi ...

  10. 勤于思考:IE10不支持检测IE6的代码

    这句话 var isIE6 = isIE && ([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 6); 在IE6~9都没问题 ...