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. UVA1515 Pool construction (最小割模型)

    如果不允许转化'#'和'.'的话,那么可以直接在'#'和'.'之间连容量为b的边,把所有'#'和一个源点连接, 所有'.'和一个汇点连接,流量不限,那么割就是建围栏(分割'#'和'.')的花费. 问题 ...

  2. mysql 存在更新,不存在插入

    String sql = "insert into wb_result " + "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? ...

  3. “流”的思维—Workflowy

    3.“流”的思维—Workflowy是我最喜欢的”流“的工具(WorkFlowy - Organize your brain.)我觉得,让发散性的思维更具实施性,必须分步操作,必须有先后,必须单线程. ...

  4. win10搭建Java环境

    一.下载地址    jdk和jre官方网址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 根据你的系统选择你需要 ...

  5. TCP的三次握手与四次挥手详解

    TCP的三次握手与四次挥手是TCP创建连接和关闭连接的核心流程,我们就从一个TCP结构图开始探究中的奥秘  序列号seq:占4个字节,用来标记数据段的顺序,TCP把连接中发送的所有数据字节都编上一个序 ...

  6. Broadcast BCM94322 用ubuntu修改ID

    1.按这个教程的6楼做的http://bbs.pcbeta.com/viewthread-1324168-1-1.html.注意我先下载 的是ubuntu9.05版本,做U盘启动进live 模式,43 ...

  7. 51nod 1264 线段相交——计算几何

    题目链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1264 检查点的位置就行了,具体见注释. /* (a-c)×(d-c)*(d ...

  8. [CODEVS] 2193 数字三角形WW

    数字三角形必须经过某一个点,使之走的路程和最大 从必须经过的点,向上向下分别DP两次的和即为答案. 还有一种思路是把和必须经过点同一行的设为-INF,这样就一定(大概)不会选择它们了. //Write ...

  9. 【Java_基础】Java的访问权限控制

    1.类成员的访问权限控制 Java中类成员的访问权限分为四类:private,无(默认情况下),protected和public.其权限控制如下表所示: 修饰词 本类 同一个包的类 继承类 其他类 p ...

  10. Perl学习之四:语句(续)

    循环控制:1.last 退出标签的语句块2.next 3.redo不推荐,循环次数不可控 4.goto不推荐.***************************************标签: 先 ...