http://acm.hdu.edu.cn/showproblem.php?pid=3001

Travelling

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

Total Submission(s): 3864    Accepted Submission(s): 1217
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

题意:给出一个无向图,并给出路径及费用,问旅行完所有的节点需要的花费最少是多少(可以从任意一点出发,每个节点经过的次数不超过2次)

分析:对于只经过节点仅且一次的题目,很清楚用二进制dp,暴搜完所有的状态,而此题每个节点有三个状态,即没走过,走了一次,走了两次,所以用三进制表示,分别代表三种状态;用dp[i][j]表示经过节点j后到达状态i;当i状态满足所有位置没有0是,即每个点都至少经过了一次,就是一种情况,但不一定是最优情况所以要更新最小值;

程序:

#include"stdio.h"
#include"string.h"
#include"iostream"
#include"map"
#include"string"
#include"queue"
#include"stdlib.h"
#include"algorithm"
#include"math.h"
#define M 60001
#define eps 1e-10
#define inf 100000000
#define mod 100000000
#define INF 0x3f3f3f3f
using namespace std;
int dp[M][12],px[12],a[M][12],dis[12][12],path[M][12];
void init()
{
int i;
px[0]=1;
for(i=1;i<=10;i++)
px[i]=px[i-1]*3;
memset(a,0,sizeof(a));
for(i=0;i<px[10];i++)
{
int k=i,t=0;
while(k)
{
a[i][t++]=k%3;
k/=3;
}
}
}
int main()
{
init();
int n,m,i,j,k;
while(scanf("%d%d",&n,&m)!=-1)
{
memset(dis,INF,sizeof(dis));
for(i=1;i<=m;i++)
{
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
u--;
v--;
if(dis[u][v]>c)
dis[u][v]=dis[v][u]=c;
}
memset(dp,INF,sizeof(dp));
memset(path,-1,sizeof(path));
for(i=0;i<n;i++)
{
dp[px[i]][i]=0;
path[px[i]][i]=-1;
}
int ans=INF;
int I,J;
for(i=1;i<px[n];i++)
{
int flag=1;
for(j=0;j<n;j++)
{
if(a[i][j]==0)
{
flag=0;continue;
}
int cur=i-px[j];
for(k=0;k<n;k++)
{
if(dp[i][j]>dp[cur][k]+dis[k][j])
{
dp[i][j]=dp[cur][k]+dis[k][j];
path[i][j]=k;
} }
}
if(flag)
{
for(j=0;j<n;j++)
{
if(ans>dp[i][j])
{
I=i;
J=j;
ans=dp[i][j];
} }
}
}
/*********路径**********/
/*printf("%d->",J+1);
for(k=path[I][J];k!=-1;k=path[I][k])
{
printf("%d->",k+1);
I=I-px[J];
J=k;
}
printf("\n");*/
/***********************/
if(ans<INF)
printf("%d\n",ans);
else
printf("-1\n");
}
return 0;
}

三进制状态压缩DP(旅行商问题TSP)HDU3001的更多相关文章

  1. hdu-3001 三进制状态压缩+dp

    用dp来求最短路,虽然效率低,但是状态的概念方便解决最短路问题中的很多限制,也便于压缩以保存更多信息. 本题要求访问全图,且每个节点不能访问两次以上.所以用一个三进制数保存全图的访问状态(3^10,空 ...

  2. HDU 3001 Travelling (三进制状态压缩 DP)

    题意:有 n 个city,能够选择任一城市作为起点,每一个城市不能訪问超过2次, 城市之间有权值,问訪问所有n个城市须要的最小权值. 思路:由于每一个城市能够訪问最多两次,所以用三进制表示訪问的状态. ...

  3. HDOJ-3001(TSP+三进制状态压缩)

    Traving HDOJ-3001 这题考察的是状态压缩dp和tsp问题的改编 需要和传统tsp问题区分的事,这题每个点最多可以经过两次故状态有3种:0,1,2 这里可以模仿tsp问题的二进制压缩方法 ...

  4. BZOJ4479 [JSOI2013] 吃货jyy 解题报告(三进制状态压缩+欧拉回路)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4479 Description [故事背景]作为JSOI的著名吃货,JYY的理想之一就是吃 ...

  5. hdu4064 三进制状态压缩 好题!

    还不太会做这类题,总之感觉有点难啊. 用深搜代替打表求出一行所有的可行状态,注意要进行剪枝 这是自己理解的代码,但是tle了 #include<bits/stdc++.h> using n ...

  6. 最短路+状态压缩dp(旅行商问题)hdu-4568-Hunter

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4568 题目大意: 给一个矩阵 n*m (n m<=200),方格里如果是0~9表示通过它时要花 ...

  7. poj 2688 状态压缩dp解tsp

    题意: 裸的tsp. 分析: 用bfs求出随意两点之间的距离后能够暴搜也能够用next_permutation水,但效率肯定不如状压dp.dp[s][u]表示从0出发訪问过s集合中的点.眼下在点u走过 ...

  8. HDU 3001【状态压缩DP】

    题意: 给n个点m条无向边. 要求每个点最多走两次,要访问所有的点给出要求路线中边的权值总和最小. 思路: 三进制状态压缩DP,0代表走了0次,1,2类推. 第一次弄三进制状态压缩DP,感觉重点是对数 ...

  9. HDU 3001 Travelling(状态压缩DP+三进制)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 题目大意:有n个城市,m条路,每条路都有一定的花费,可以从任意城市出发,每个城市不能经过两次以上 ...

随机推荐

  1. CMM已经落伍了,敏捷才是王道

    首先强调一下,敏捷和有没有文档一点关系都没有.我只是对于CMM的那些文档感觉有些浪费. 看看那些文档,看看那些流程.想想那些伟大的软件作品,哪个是用CMM开发出来的? 作为测试工程师,程序员的你在CM ...

  2. C++ c++与C语言的区别(struct类型的加强,函数-变量类型加强,bool类型)

    //区别④:struct类型的加强(C++版本) #include<iostream> using namespace std; //C++中的struct是一个新类型的定义声明 //c+ ...

  3. YARN : Architecture of Next Generation Apache Hadoop MapReduceFramework

    转自:http://blog.csdn.net/colorant/article/details/9146201 == 目标问题 == 下一代的Hadoop框架,支持10,000+节点规模的Hadoo ...

  4. 【转】【MFC】 StretchBlt绘图图像失真

    MFC中位图伸缩函数StretchBlt在对图片进行缩放时会造成严重的图片失真. 在了解解决方法前先巩固下StretchBlt的用法: StretchBlt 函数功能:函数从源矩形中复制一个位图到目标 ...

  5. bson.errors.InvalidStringData: strings in documents must be valid UTF-8

    场景: pymongo 查询数据库的时候报错. for gscode in GSList_StockPool_Mongo_MktStop: self._collection_flash.find({& ...

  6. bt开源的客户端——xbt client

    我部署好了bt tracker, 用bitcomet可以下载. 但xbt client下载不来.torrent资源.

  7. centos 6.5配置samba

    Samba简介 Samba是一个能让Linux系统应用Microsoft网络通讯协议的软件,而SMB是Server Message Block的缩写,即为服务器消息块 ,SMB主要是作为Microso ...

  8. 【Java面试题】39 Set里的元素是不能重复的,那么用什么方法来区分重复与否呢? 是用==还是equals()? 它们有何区别?

    1.什么是Set?(what) Set是Collection容器的一个子接口,它不允许出现重复元素,当然也只允许有一个null对象. 2.如何来区分重复与否呢?(how) “ 用 iterator() ...

  9. 关于NaN(Not a Number)的问题

    在游戏运行时,代码若写得不安全很容易出现NAN的异常.一旦NAN出现整个游戏不崩溃也坏死掉了,游戏上了则是要被直接打回来的节奏,更是一个开发及测试人员每人都要扣3000块的大BUG.   一般表现为: ...

  10. usb摄像头的检测

    下面写一下过程: 如果你能在http://www.ideasonboard.org/uvc/找到你的摄像头的ID,即UVC支持的,那么就可以在linux下使用了.至于从哪个版本开始内核支持UVC,官方 ...