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. struts2的分页标签

    1.准备tld文件 <?xml version="1.0" encoding="UTF-8" standalone="no"?> ...

  2. Python爬虫--Requests库

    Requests Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库,requests是python实现的最简单易用的HTTP库, ...

  3. Python爬虫一些操作headers与cookies的便捷工具

    本篇文章主要是爬虫中常用的便捷处理方法整理,转载请注明出处 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2018-08-14 13: ...

  4. datetime-local设置初始值

    //全局变量 var format = ""; //构造符合datetime-local格式的当前日期 function getFormat(){ format = "& ...

  5. keras:Exception: Error when checking model target

    问题: 用keras的functional API搭建多输入多输出模型时,报错某个输出层对应的类标数组大小与模型中不一致. 解决办法:升级keras到最新版(doge脸)keras迭代太快了啊摔,总有 ...

  6. 使用expect实现shell自动交互

    expect 是一个自动交互功能的工具.expect 是开了一个子进程,通过 spawn 来执行 shell 脚本,监测到脚本的返回结果,通过 expect 判断要进行的交互输入内容. expect ...

  7. VLAN(虚拟局域网)划分

    VLAN根据不同的需求,可以有多种划分方式: 一:静态划分 基于端口             按VLAN交换机上的物理端口和内部的PVC(永久虚电路)端口来划分 静态划分安全.可靠,易于配置与维护 二 ...

  8. 时尚设计div+css免费模板

    时尚设计div+css免费网页模板,时尚设计,div+css. http://www.huiyi8.com/moban/

  9. DenseNet算法详解——思路就是highway,DneseNet在训练时十分消耗内存

    论文笔记:Densely Connected Convolutional Networks(DenseNet模型详解) 2017年09月28日 11:58:49 阅读数:1814 [ 转载自http: ...

  10. L89

    His voice was hoarse after several hours' speech.Attributive adjectives precede the noun.I gave the ...