题意:有 n 个city,能够选择任一城市作为起点,每一个城市不能訪问超过2次,

城市之间有权值,问訪问所有n个城市须要的最小权值。

思路:由于每一个城市能够訪问最多两次,所以用三进制表示訪问的状态。

具体见代码凝视!

!!!

#include<cstdio>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<map>
#include<cmath>
#include<iostream>
#include <queue>
#include <stack>
#include<algorithm>
#include<set>
using namespace std;
#define INF 1e8
#define inf 0x3f3f3f3f
#define eps 1e-8
#define LL long long
#define N 100001
#define mol 100000000 int dp[60000][15];//dp[i][j] 表示在状态 i 的情况訪问到 j 的最小值
int g[15][15];
int n,m;
int st[60000][11];//dp[i][j] i 状态的第 j 个城市能够訪问几次
int bit[12];//初始所有可能状态
int main()
{
bit[0]=0;bit[1]=1;
for(int i=2;i<=11;i++)
bit[i]=3*bit[i-1];
for(int i=0;i<59050;i++)
{
int t=i;
for(int j=1;j<=10&&t;j++)
{
st[i][j]=t%3;
t/=3;
}
}
while(~scanf("%d%d",&n,&m))
{
int u,v,c;
memset(dp,0x3f,sizeof(dp));
memset(g,0x3f,sizeof(g));
for(int i=0;i<=n;i++)
dp[bit[i]][i]=0;//初始起点 为0
while(m--)
{
scanf("%d%d%d",&u,&v,&c);
if(c<g[u][v])
{
g[u][v]=c;
g[v][u]=c;
}
}
int ans=inf;
for(int i=0;i<bit[n+1];i++)
{
int flag=1;
for(int j=1;j<=n;j++)
{
if(!st[i][j])//i 状态不经过 j城市
{
flag=0;
continue;
}
if(i==j) continue;//i 状态仅仅经过 j 城市(j 城市为起点)
for(int k=1;k<=n;k++)
{
int l=i-bit[j];//i 状态在经过j城市之前的状态(到达j城市之前的状态)
if(st[i][k]==0) continue;//假设i 状态不经过 k 城市,则不能利用k城市缩小到达j城市的值
dp[i][j]=min(dp[i][j],dp[l][k]+g[k][j]);//l 状态经过j城市之后则变为i状态
}
}
if(flag)//i 状态经过所有的n个城市
for(int j=1;j<=n;j++)
ans=min(ans,dp[i][j]);
}
if(ans!=inf)
printf("%d\n",ans);
else printf("-1\n");
}
return 0;
}

HDU 3001 Travelling (三进制状态压缩 DP)的更多相关文章

  1. 三进制状态压缩DP(旅行商问题TSP)HDU3001

    http://acm.hdu.edu.cn/showproblem.php?pid=3001 Travelling Time Limit: 6000/3000 MS (Java/Others)     ...

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

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

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

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

  4. hdu 5025 Saving Tang Monk 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...

  5. HDU 3681 Prison Break(状态压缩dp + BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 前些天花时间看到的题目,但写出不来,弱弱的放弃了.没想到现在学弟居然写出这种代码来,大吃一惊附加 ...

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

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

  7. HDU 6125 Free from square 状态压缩DP + 分组背包

    Free from square Problem Description There is a set including all positive integers that are not mor ...

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

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

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

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

随机推荐

  1. ibatis.net:第一天,什么是 mybatis.net ?

    ibatis.net 是一个“数据映射框架”,它使得面向对应的应用程序非常的方面使用关系数据.ibatis.net 通过使用 xml 或 attribute 来解耦对象和SQL或存储过程.简单是 ib ...

  2. Linux学习7-tomcat部署多个项目(多个端口)

    前言 前面已经在tomcat上搭建了jenkins的环境,如果我们有多个项目需要部署的话,如何在一个tomcat下部署多个项目呢? 前面是直接在:8080/jenkins访问的,如果有其它项目部署的话 ...

  3. SharePoint Online 创建文档库

    前言 本文介绍如何在Office 365中创建文档库,以及文档库的一些基本设置. 正文 通过登录地址登录到Office 365的SharePoint Online站点中,我们可以在右上角的设置菜单中, ...

  4. 迷宫问题的C语言求解

    1 .Preface /** * There have been many data to introduce the algorithm. So I will try to simply expla ...

  5. CoreDNS介绍

    本文介绍 CoreDNS 相关配置以及验证方法,实验环境为 Kubernetes 1.11,搭建方法参考kubeadm安装kubernetes V1.11.1 集群 busybox 的槽点 开始之前先 ...

  6. MAPI错误0x80040107

    MAPI错误0x80040107  的解决方案: The MAPI error means there's an "invalid entry" within the contac ...

  7. Maven 搭建spring boot多模块项目(附源码),亲测可以,感谢原创

    原创地址:https://segmentfault.com/a/1190000005020589 我的DEMO码云地址,持续添加新功能: https://gitee.com/itbase/Spring ...

  8. jQuery事件命名空间多事件绑定自定义事件js 命名空间 javascript命名空间

    http://blog.csdn.net/pigpigpig4587/article/details/24727791 jQuery事件命名空间 jQuery支持事件命名空间,以方便事件管理.例如,在 ...

  9. CentOS6.4 xen4.2 虚拟机 桥接网络设置

    安装好xen后,可以使用virt-manager来配置虚拟网络 virbr0,以及创建虚拟机 都使用virt-manager. [虽说可以使用xm create XX命令行根据脚本创建,但是从来都不知 ...

  10. [leetcode]Best Time to Buy and Sell Stock III @ Python

    原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 题意: Say you have an array ...