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. c++标准库比较

    1 GNU standard c++ library debian发行版中使用的c++标准库是GNU standard c++标准库. 2 Boost debian发行版中也是用了boost库,但是不 ...

  2. mac sublime text 3 add ctags plugin

    https://www.smslit.top/2015/11/14/macSTctags-Develop/ ctags插件for sublime text项目和ctags源码项目都在github上.

  3. Go 语言中的数组是一种 值类型(不像 C/C++ 中是指向首元素的指针)

    the-way-to-go_ZH_CN/07.1.md at master · Unknwon/the-way-to-go_ZH_CN https://github.com/Unknwon/the-w ...

  4. [TroubleShooting]Neither the partner nor the witness server instance for database is availble

    Problem: You are trying to setup a mirroring on a Database called xxxDB(SQL server 2012). You are ge ...

  5. WINFROM中自定义控件之绑定数据即时更新

    相信在WINFROM中写自定义控件或者用户控件,很多人都多多少少用过点 最近发现一个用户控件,绑定的数据源没办法自动更新,其实以前处理过这类的问题,可是这次遇到又花了1个多小时,所以决定记下来 在用户 ...

  6. ubuntu tomcat 配置及使用细节

    1.改端口号(两个) vi server.xml 一个是http协议端口  <Connector port="8091" protocol="HTTP/1.1&qu ...

  7. SDUT OJ 螺旋矩阵

    螺旋方阵 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 n×n的螺旋方阵当n=5和n=3时分别是如下的形式 请给出一个程序,对于 ...

  8. POJ3581 Sequence —— 后缀数组

    题目链接:https://vjudge.net/problem/POJ-3581 Sequence Time Limit: 5000MS   Memory Limit: 65536K Total Su ...

  9. Hadoop- Hadoop详解

    首先所有知识以官网为准,所有的内容在官网上都有展示,所有的变动与改进,新增内容都以官网为准.hadoop.apache.org Hadoop是一个开源的可拓展的分布式并行处理计算平台,利用服务器集群根 ...

  10. 深入理解JVM - 垃圾收集器与内存分配策略 - 第三章

    引用计数算法——判断对象是否存活的算法 很多教科书判断对象是否存活的算法是这样的:给对象添加一个引用计数器,每当一个地方引用它时,计数器值就加1:当引用失效时,计数器值就减1:任何时刻计数器为0的对象 ...