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

从任何一个点出发,去到达所有的点,但每个点只能到达2次,使用的经费最小。三进制

Travelling

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3541    Accepted Submission(s): 1106

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次,使用的经费最小。三进制。0,1,2.0代表0次,1,1次,2,2次。
dp[i][j]表示i状态到达j城市。
time[i][j]表示i状态到达j城市的次数,就像十进制求余求个位所得。
首先枚举所有的i状态,到达j个城市,在到达下一个城市。
 dp[p][k]=min(dp[p][k],dp[i][j]+dis[j][k]);p是一个城市的状态,i+s[k]所得,s[k]是成倍数所以加起来就是下一个的状态。k是下一个城市。flag去验证是否状态已经是每个城市都到达。
注意要排除重边的情况。
#include<iostream>
#include<cstring>
#include<cstdio>
#define inf 1<<27
using namespace std;
int dis[][],dp[][],s[];
int n,time[][];
void init()
{
int temp,i,j;
s[]=;
for(i=; i<=n+; i++)
s[i]=s[i-]*;
for(i=; i<=s[n+]; i++)
{
temp=i;
for(j=; j<=n; j++)
{
time[i][j]=temp%;//模拟十进制取余求个数的方法。
temp=temp/;
}
}
}
int main()
{
int i,a,b,c,k,m,j,ans,flag;
while(~scanf("%d%d",&n,&m))
{
ans=inf;
for(i=; i<; i++)
{
for(j=; j<; j++)
dp[i][j]=inf;
}
memset(s,,sizeof(s));
memset(time,,sizeof(time));
for(i=; i<=n; i++)
for(j=; j<=n; j++)
{
if(i==j)
dis[i][j]=;
else
dis[i][j]=inf;
}
for(i=; i<=m; i++)
{
scanf("%d%d%d",&a,&b,&c);
dis[a][b]=dis[b][a]=min(dis[a][b],c);//防止有重边。
}
init();
for(i=; i<=n; i++)
dp[s[i]][i]=;//起点位置初始化为0.
for(i=; i<s[n+]; i++)
{
flag=;//各点是否全部已经到了。
for(j=; j<=n; j++)//当前到的这个城市。
{
if(time[i][j]==)//该状态不可能都这个城市。
{
flag=;
continue;
}
for(k=; k<=n; k++)//到下一个城市。
{
if(time[i][k]==||j==k)//如果等于2,就不可以。
continue;
int p=i+s[k];//到达下个城市的状态。
dp[p][k]=min(dp[p][k],dp[i][j]+dis[j][k]);
}
}
if(flag)
{ for(j=; j<=n; j++)
{
ans=min(ans,dp[i][j]);
}
} }
if(ans==inf)
printf("-1\n");
else
printf("%d\n",ans);
}
return ;
}
/*
4 5
1 4 1
1 2 1
1 3 2
4 3 10
2 3 10
*/
 

HDU-3001 Travelling的更多相关文章

  1. HDU 3001 Travelling:TSP(旅行商)【节点最多经过2次】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 题意: 有n个城市,m条双向道路,每条道路走一次需要花费路费v.你可以将任意一个城市作为起点出发 ...

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

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

  3. hdu 3001 Travelling (TSP问题 )

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

  4. hdu 3001 Travelling(状态压缩 三进制)

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

  5. HDU - 3001 Travelling(三进制状压dp)

    Travelling After coding so many days,Mr Acmer wants to have a good rest.So travelling is the best ch ...

  6. HDU 3001 Travelling 3进制状压dp

    题意:10个点,若干条边,边有花费,每个点最多走两次,求走过所有点,花费最少 分析:因为每个点最多走两次,所以联想到3进制,然后枚举状态,就行了(我也是照着网上大神的代码写的) #include &l ...

  7. HDU - 3001 Travelling 状压dp + 三进制 [kuangbin带你飞]专题二

    终于刷完搜索专题了. 题意:给定n个城市,每个城市参观不能超过两次,两个城市之间有道路通过需要花费X,求通过能所有城市的最小花费. 思路:每个城市有三个状态0,1,2,可用三进制存储所有城市的访问状态 ...

  8. Hdu 3001 Travelling 状态DP

    题目大意 一次旅游,经过所有城市至少一次,并且任何一座城市访问的次数不能超过两次,求最小费用 每个城市最多访问两次,用状态0,1,2标识访问次数 把城市1~N的状态按照次序连接在一起,就组成了一个三进 ...

  9. hdu 3001 Travelling (三进制)【状压dp】

    <题目链接> 题目大意: 给出n个点和m条边,求经过所有点所需的最小花费,每个点最多经过两次. 解题分析: TSP问题类型,由于此题每个点有三种状态,所以采用三进制状态压缩,0.1.2 分 ...

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

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

随机推荐

  1. POJ2676Sudoku

    http://poj.org/problem?id=2676 题意 : 这个是我最喜欢玩的数独了,就是一个9乘9的宫格,填上1到9九个数字,每行每列每个宫格之内不能有重复的数字,给出的九宫格中,0是待 ...

  2. 可持久化trie 学习总结

    QAQ 以前一直觉得可持久化trie很难,今天强行写了一发觉得还是蛮简单的嘛 自己的模板是自己手写的,写了几道题目并没有出过错误 THUSC的第二题的解法五貌似就是可持久化trie,时间复杂度O(60 ...

  3. SpringMVC学习总结(二)——DispatcherServlet详解

    摘要: DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,而且负责职责的分派,而且与Spring IoC容器无缝集成,从而可以获得Spring ...

  4. React表单组件自定义-可控及不可控组件

    一.可控组件 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset=" ...

  5. C#操作.csv文件Demo

    1.使用OleDB操作.csv文件,比较费时 public static DataTable GetDataTableFromCsv(string path,bool isFirstRowHeader ...

  6. [iOS]iPhone利用<极光推送>实现远程推送

    准备: 1. 一个Xcode工程 2. 开发者账号 3. 真机 (重要,模拟器无法进行远程推送,因为模拟器没有UDID) 第一步:绑定工程的Bundle Identifer 首先当然要登录https: ...

  7. 59. Spiral Matrix II

    题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...

  8. Lua for windows中SciTe开启支持python的方法

    打开Options-Open Global Options #import python的#去掉即可

  9. The type xxx cannot be resolved. It is indirectly referenced from required .class files

    项目A中引入一个jar包B,在项目A中调用项目B,出现如下错误提示:   大致意思是:这上面所需的包是间接引用的,即A项目调用B项目,B项目又引用了另外一个包C,而这个包现在不在你的A项目的引用中. ...

  10. git stash 暂存当前修改

    当我们在开发项目的时候,突然来一个变更需要修改,我们除了将当前项目提交(commit)后切换(checkout) 到其他分支外,我们还可以先将当前的修改暂存(stash)起来,然后再切换(checko ...