Travelling

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

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
 
Source
 
Recommend
gaojie
 

题意:

ACMer 想要游玩n个城市,告诉我们每个城市间的旅行费用,并且要求每个城市最多走两遍!问最小花费是多少 ?!

思路:

典型的TSP问题,唯一的变化就是走两遍!

解法:

利用三进制将边点j 在点集i 出现的次数表示成 tir[i][j];

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<vector>
#include<set> #define N 1005
#define M 100000
#define inf 1000000007
#define mod 1000000007
#define mod2 100000000
#define ll long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n;
int m;
int tri[] ={,,,,,,,,,,,};
int dig[][]; //dig[state][k_dig] 状态state的第k位是多少
int dp[][];
int d[][];
int ans;
int flag; void ini1()
{
int o,j,t;
for(o=;o<;o++){
t=o;
for(j=;j<;j++){
dig[o][j]=t%;
t/=;
}
}
} void ini()
{
int a,b;
int c;
int i;
ans=-;
memset(dp,-,sizeof(dp));
memset(d,-,sizeof(d));
while(m--){
scanf("%d%d%d",&a,&b,&c);
if(d[a][b]==-){
d[a][b]=c;
d[b][a]=c;
}
else{
d[a][b]=min(d[a][b],c);
d[b][a]=min(d[b][a],c);
}
} for(i=;i<=n;i++){
dp[ tri[i] ][i-]=;
}
} void solve()
{
int o,j,te,k;
for(o=;o<tri[n+];o++){
flag=;
for(j=;j<n;j++){
if(dig[o][j]==){
flag=;continue;
}
te=o-tri[j+];
for(k=;k<n;k++){
if(dig[te][k]==) continue;
if(d[k+][j+]==-) continue;
if(dp[te][k]==-) continue;
if(dp[o][j]==-){
dp[o][j]=dp[te][k]+d[k+][j+];
}
else{
dp[o][j]=min(dp[o][j],dp[te][k]+d[k+][j+]);
}
}
} // printf(" o=%d flag=%d\n",o,flag);
if(flag==) continue;
for(j=;j<n;j++){
if(dp[o][j]==-) continue;
if(ans==-){
ans=dp[o][j];
}
else{
ans=min(ans,dp[o][j]);
}
} }
} void out()
{
//for(int o=0;o<tri[n+1];o++){
// for(int j=0;j<n;j++) printf(" o=%d j=%d dp=%d\n",o,j,dp[o][j]);
// }
printf("%d\n",ans);
} int main()
{
ini1();
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
//scanf("%d",&T);
// for(int cnt=1;cnt<=T;cnt++)
// while(T--)
while(scanf("%d%d",&n,&m)!=EOF)
{
ini();
solve();
out();
}
return ;
}

HDU 3001 三进制 状压dp的更多相关文章

  1. HDU 3001 三进制状压DP

    N个城市,M条道路,每条道路有其经过的代价,每一个城市最多能够到达两次,求走全然部城市最小代价,起点随意. 三进制状压.存储每一个状态下每一个城市经过的次数. 转移方程: dp[i+b[k]][k]= ...

  2. hdu 3001 三进制状压

    题意:tsp问题,但是每个点可以最多走两次 链接:点我 转移方程见代码 #include<iostream> #include<cstdio> #include<cstr ...

  3. ZRDay6A. 萌新拆塔(三进制状压dp)

    题意 Sol 这好像是我第一次接触三进制状压 首先,每次打完怪之后吃宝石不一定是最优的,因为有模仿怪的存在,可能你吃完宝石和他打就GG了.. 因此我们需要维护的状态有三个 0:没打 1:打了怪物 没吃 ...

  4. hdu 3001 Travelling 经过所有点(最多两次)的最短路径 三进制状压dp

    题目链接 题意 给定一个\(N\)个点的无向图,求从任意一个点出发,经过所有点的最短路径长度(每个点至多可以经过两次). 思路 状态表示.转移及大体思路 与 poj 3311 Hie with the ...

  5. HDU 3001 Travelling (状压DP,3进制)

    题意: 给出n<=10个点,有m条边的无向图.问:可以从任意点出发,至多经过同一个点2次,遍历所有点的最小费用? 思路: 本题就是要卡你的内存,由于至多可经过同一个点2次,所以只能用3进制来表示 ...

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

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

  7. HDU 3001 Traveling(状压DP)

    题目大意:10个点的TSP问题,但是要求每个点最多走两边,不是只可以走一次,所以要用三进制的状态压缩解决这个问题.可以预处理每个状态的第k位是什么. 原代码链接:http://blog.csdn.ne ...

  8. Travelling (三进制+状压dp)

    题目链接 #include <bits/stdc++.h> using namespace std; typedef long long ll; inline ll read(){ ,f= ...

  9. UVA 10817 - Headmaster's Headache(三进制状压dp)

    题目:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=20&pag ...

随机推荐

  1. KTU Programming Camp (Winter Training Day 1)

    A.B.C(By musashiheart) 0216个人赛前三道题解 E(By ggg) Gym - 100735E Restore H(by pipixia) Gym - 100735H

  2. tp5 -- 微信公众号支付

    近来期间比较忙, 忙完之后发现最近有挺多的东西没有整理,于是乎.就将以前用到的一些小东西整理了一下. 如果对您有帮助,则是我最大的幸运. 本篇主要是说了一下整合TP5的微信公众号支付. 不过由于最近T ...

  3. 【整理】C#文件操作大全

    文件与文件夹操作主要用到以下几个类: 1.File类: 提供用于创建.复制.删除.移动和打开文件的静态方法,并协助创建 FileStream 对象. msdn:http://msdn.microsof ...

  4. shell脚本,awk替换{}里面的内容

    如何将oxo{axbxc}oxo{dxexf}oxo里面的{}里面的x 替换为; 用awk实现 [root@localhost 09-30]# echo 'oxo{axbxc}oxo{dxexf}ox ...

  5. CF-1072-C. Cram Time(贪心,数学)

    CF-1072-C. Cram Time http://codeforces.com/contest/1072/problem/C 题意: 第一天有 a 小时,第二天有 b 小时.第 k 个任务需要 ...

  6. codevs 数字三角形集结

    添在前面的一句话:初学DP,若有错误,请指出,不能误人子弟,欢迎大家提出意见.水平不高,博客写的比较粗糙,代码也挺丑,请见谅. 最原始的数字三角形: 1220 数字三角形  时间限制: 1 s  空间 ...

  7. Centos忘记密码解决方法

    centos6.8忘记root密码解决方法 重启系统后出现GRUB界面在引导装载程序菜单上,用上下方向键选择你忘记密码的那个系统键入"e" 来进入编辑模式. 接下来你可以看到如下图 ...

  8. perl学习之进程管理

    系统函数 == 最简单的系统调用  system "date"; # Perl会将 date 命令传递给unix的shell并获取返回值和error信息等   == 带有系统参数的 ...

  9. PHPMailer中文乱码问题的解决方法

    之前用PHPMailer帮人家开发了用于发邮件的网站,由于是英文客户,所以中文没怎么测试,最近反馈说 中文乱码! 其实,之前是有发现标题中会出现中文了乱码,已经通过相应的代码解决. 收到反馈之后,查看 ...

  10. Could not connect to Redis at IP No route to host

    这个问题是在用远程去访问redis出现的 原因:是服务器新装系统  iptables这个的问题 解决办法: sudo iptables -F 轻松解决