题目连接: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. Servlet小总结

    Servlet Servlet(服务器端小程序)是使用Java语言编写的服务器端程序,像JSP一样,生成动态的Web页.Servlet主要运行在服务器端,并由服务器调用执行. Servlet处理的基本 ...

  2. 4.Nginx的URL重写应用

    Nginx的URL重写应用 nginx的URL重写模块是用得比较多的模块之一,所以我们需要好好地掌握运用.常用的URL重写模块命令有if,rewrite,set,break等. if命令 if用于判断 ...

  3. Oracle 启动参数

    查看数据库的SID DB_NAME SERVICE_NAME

  4. raspberrypi 3代B 配置摄像头

    raspberrypi 3代B 配置摄像头 硬件环境: 树莓派3B (element 14版) 树莓派3夜视摄像头800万像素(element 14版) 金士顿 64GB TF Class 10 UH ...

  5. GIT命令一页纸

    ,配置用户名和邮箱 $ git config --global user.name "Your Name" $ git config --global user.email &qu ...

  6. Go 语言编写单元测试

    吾尝终日而思矣,不如须臾之所学也:吾尝跂而望矣,不如登高之博见也.登高而招,臂非加长也,而见者远:顺风而呼,声非加疾也,而闻者彰.假舆马者,非利足也,而致千里:假舟楫者,非能水也,而绝江河.君子生非异 ...

  7. 2018年手机应用UI设计趋势预测

    用户需求瞬息万变,而手机软件UI设计为适应变化的用户需求,也相应的发生着变化.但是,这并不意味着用户需求和UI设计趋势就是无迹可寻的.事实上,根据前几年的手机app界面设计变化的特点,尤其是2017年 ...

  8. vmvare centos 7.0 root密码忘记后重置及总结

    今天遇到了一个比较尴尬的事情,我centos 7.0的虚拟机密码忘了.....里面还有我配置好的环境呢.于是我就上网上搜索各种方法,最后经我验证下面这个方法比较靠谱: 使用光盘修复Centos: ht ...

  9. Markdown 常用语法

    1.标题 # 一级标题 ## 二级标题 ### 三级标题 2.字体 *斜体* **粗体** ~~删除线~~<u>下划线</u> 3.引用 > 引用文字 4.无序列表 - ...

  10. adb 获取Android手机信息命令(1)

    @set version=1.7.2Normal @echo ============================================================= @echo I ...