题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5418

Victor and World

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 958    Accepted Submission(s): 431

Problem Description
After trying hard for many years, Victor has finally received a pilot license. To have a celebration, he intends to buy himself an airplane and fly around the world. There are n countries on the earth, which are numbered from 1 to n. They are connected by m undirected flights, detailedly the i-th flight connects the ui-th and the vi-th country, and it will cost Victor's airplane wi L fuel if Victor flies through it. And it is possible for him to fly to every country from the first country.

Victor now is at the country whose number is 1, he wants to know the minimal amount of fuel for him to visit every country at least once and finally return to the first country.

 
Input
The first line of the input contains an integer T, denoting the number of test cases.
In every test case, there are two integers n and m in the first line, denoting the number of the countries and the number of the flights.

Then there are m lines, each line contains three integers ui, vi and wi, describing a flight.

1≤T≤20.

1≤n≤16.

1≤m≤100000.

1≤wi≤100.

1≤ui,vi≤n.

 
Output
Your program should print T lines : the i-th of these should contain a single integer, denoting the minimal amount of fuel for Victor to finish the travel.
 
Sample Input
1
3 2
1 2 2
1 3 3
 
Sample Output
10
 
Source

题意: 给一个地图,从其中一个点开始走,遍历完所有的点后最后再回到这个点,求最短路径

题解: 注意数据范围给的是16个点,又是求汉密顿环路问题,就想到了状态压缩dp,这个题可以学习一个很好地思想就是spfa的思想,将其用到dp中,取出队首的状态,看这个状态停在哪个点,用这个点更新所有的可以更新的状态,因为每一个点可以多次的遍历,所以每次更新都要从0到n依次遍历,新更新的状态如果之前没有访问过,则说明这个状态有更新其他状态的潜力,所以将其压入队列中,通过这种方式可以更新所有的状态。

简单说一下状态压缩dp,用一个二进制的数表示某个点是否在集合内,全集为(1<<n)-1 ; j集合中加入一个元素k是 j|(1<<k) ; 在j集合中去掉一个元素k是 j^(1<<k) ;

dp[j][k] 表示走过集合j中的所有元素最后停留在k点的最短路径

转移方程: dp[s|(1<<i)][i] = min(dp[s|(1<<i)][i] , dp[s][u]+mp[u][i])

注意事项: 这里的边是双向边,在输入边的时候会有很多的无效边,要取最小的。

      用qair<int , int> 相当于一个struct{ int a, int b};

代码:

 #include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std; int dp[<<][];
int mp[][];
int dis[];
bool vis[<<][];
int n; int main()
{
int T , m ;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
memset(mp,0x7f,sizeof(mp));
for(int i = ; i < n ;i++) mp[i][i] = ;
for(int i = ; i < m ;i++) {
int u , v , d;
scanf("%d%d%d",&u,&v,&d);
u--,v--;
mp[u][v] = mp[v][u] = min(mp[u][v],d);
}
memset(dp,0x7f,sizeof(dp));
memset(vis,,sizeof(vis));
dp[][] = ; vis[][] = ;
queue<pair<int,int> > q;
q.push(make_pair(,));
while(!q.empty())
{
int s = q.front().first;
int u = q.front().second;
q.pop();
vis[s][u] = ;//可能被二次更新
for(int i = ; i < n ; i++)
{
int ss = s|(<<i);
if(dp[ss][i]>dp[s][u] + mp[u][i]){
dp[ss][i] = dp[s][u] + mp[u][i];
if(vis[ss][i] == ){
vis[ss][i] = ;
q.push(make_pair(ss,i));
}
}
}
}
printf("%d\n",dp[(<<n)-][]);
}
return ;
}

Victor and World(spfa+状态压缩dp)的更多相关文章

  1. Travelling(spfa+状态压缩dp)

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

  2. HDU 5418 Victor and World (状态压缩dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5418 题目大意:有n个结点m条边(有边权)组成的一张连通图(n <16, m<100000 ...

  3. HDU 4085 Peach Blossom Spring 斯坦纳树 状态压缩DP+SPFA

    状态压缩dp+spfa解斯坦纳树 枚举子树的形态 dp[i][j] = min(dp[i][j], dp[i][k]+dp[i][l]) 当中k和l是对j的一个划分 依照边进行松弛 dp[i][j]  ...

  4. [转]状态压缩dp(状压dp)

    状态压缩动态规划(简称状压dp)是另一类非常典型的动态规划,通常使用在NP问题的小规模求解中,虽然是指数级别的复杂度,但速度比搜索快,其思想非常值得借鉴. 为了更好的理解状压dp,首先介绍位运算相关的 ...

  5. BZOJ1294 洛谷P2566 状态压缩DP 围豆豆

    传送门 题目描述 是不是平时在手机里玩吃豆豆游戏玩腻了呢?最近MOKIA手机上推出了一种新的围豆豆游戏,大家一起来试一试吧游戏的规则非常简单,在一个N×M的矩阵方格内分布着D颗豆子,每颗豆有不同的分值 ...

  6. hoj2662 状态压缩dp

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

  7. POJ 3254 Corn Fields(状态压缩DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 Descr ...

  8. [知识点]状态压缩DP

    // 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...

  9. HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP

    题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...

随机推荐

  1. SQL Server 文章目录

    SQL Server系列: 高可用方案 SQL Server Alwayson概念总结 SQL Server AlwaysOn搭建 SQL Server2016 Alwayson新增功能 SQL Se ...

  2. [数据结构]C语言二叉树的实现

    树和图是数据结构中比较麻烦的东西,里面涉及的概念比较多,也最有用, 就比如一般树广泛应用于人工智能的博弈上,而基于图的广度优先和深度优先搜索也广泛应用于人工智能寻路上面 首先我们要把树进行分类: &g ...

  3. 通过自动回复机器人学Mybatis 笔记:接口式编程

    [接口式编程]尚未遇见Spring --> 代码量反而增加 1.增加约定,减少犯错的可能(不用直接去写字符串 修改点1:命名空间 修改点2:增加接口,方法名与配置文件中的id对应 package ...

  4. js 深入理解原型模式

    我们创建每一个函数都有一个prototype(原型)属性,这个属性是一个指针,指向一个对象.使用原型的好处是可以让所有对象共享它所包含的属性和方法. function Person(){ } Pers ...

  5. ActiveMQ (二) 常用配置简介

    ActiveMQ的主要配置文件 ActiveMQ的一些常用的属性很多可以在对应的配置文件中进行配置的.比如访问web console的管理端的端口,用户名密码,连接MQ时的用户名和密码,持久化设置,是 ...

  6. 文档对象模型(DOM),你只需知道这些就够了!

    官方定义--应用程序编程接口(API) 文档对象模型是用于HTML和XML文档的应用程序编程接口,它定义文档的逻辑结构,以及访问和操作文档的方式. "The Document Object ...

  7. qt中线程的使用方法

    QT中使用线程可以提高工作效率. 要使用线程要经过一下四个步骤: (1)先创建一个c++ class文件,记得继承Thread,创建步骤如下: a.第一步 b.第二步 (2)自定义一个run函数,以后 ...

  8. Linux的编码及编码转换

    如果你需要在Linux中操作windows下的文件,那么你可能会经常遇到文件编码转换的问题.Windows中默认的文件格式是GBK(gb2312),而Linux一般都是UTF-8.下面介绍一下,在Li ...

  9. Maven打包排除某个资源或者目录

    最近在spark streaming本地调试的时候,引入了一些资源文件,打包的时候需要给排除掉.所以就考虑使用maven的方式 详细参考官方文档:https://maven.apache.org/pl ...

  10. JavaScript的DOM编程--03--读写属性节点

    读写属性节点: 1)可以直接通过 cityNode.id 这样的方式来获取和设置属性节点的值 2)通过元素节点的 getAttributeNode 方法来获取属性节点, 然后在通过 nodeValue ...