Victor and World(spfa+状态压缩dp)

题目连接: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

BestCoder Round #52 (div.2)

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

题解: 注意数据范围给的是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};

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<queue>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 int dp[1<<17][17];
 8 int mp[17][17];
 9 int dis[17];
10 bool vis[1<<17][17];
11 int n;
12
13 int main()
14 {
15     int T , m ;
16     scanf("%d",&T);
17     while(T--)
18     {
19         scanf("%d%d",&n,&m);
20         memset(mp,0x7f,sizeof(mp));
21         for(int i =0 ; i < n ;i++) mp[i][i] = 0;
22         for(int i =0 ; i < m ;i++) {
23             int u , v , d;
24             scanf("%d%d%d",&u,&v,&d);
25             u--,v--;
26             mp[u][v] = mp[v][u] = min(mp[u][v],d);
27         }
28         memset(dp,0x7f,sizeof(dp));
29         memset(vis,0,sizeof(vis));
30         dp[0][0] = 0 ; vis[0][0] = 1;
31         queue<pair<int,int> > q;
32         q.push(make_pair(0,0));
33         while(!q.empty())
34         {
35             int s = q.front().first;
36             int u = q.front().second;
37             q.pop();
38             for(int i = 0 ; i < n ; i++)
39             {
40                 int ss = s|(1<<i);
41                 if(dp[ss][i]>dp[s][u] + mp[u][i]){
42                     dp[ss][i] = dp[s][u] + mp[u][i];
43                     if(vis[ss][i] == 0){
44                         vis[ss][i] = 1 ;
45                         q.push(make_pair(ss,i));
46                     }
47                 }
48             }
49         }
50         printf("%d\n",dp[(1<<n)-1][0]);
51     }
52     return 0 ;
53 }
时间: 2024-08-02 02:50:53

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

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5418 题目大意:有n个结点m条边(有边权)组成的一张连通图(n <16, m<100000).求从第一个点出发,经过每个点至少一次后回到原点的最小路径边权和. 分析:发现我还真是菜. n<16,很明显的状态压缩标记,先将所有点的编号减去1,使其从0开始编号.dp[i][j]表示从0号点出发,当前状态为i (二进制位为1表示对应点已走过,否则没走过), 当前位置为 j,  回到原点的最小代价,

HDU3768 Shopping(状态压缩DP+spfa)旅行商问题

Shopping Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 577    Accepted Submission(s): 197 Problem Description You have just moved into a new apartment and have a long list of items you need

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] = min(dp[i][j], dp[i'][j]+w[i][j])其中i和i'之间有边相连 #include <cstdio> #include <cstring> #include <queue> using namespace std; const int maxn

动态规划之状态压缩dp入门

状态压缩动态规划(简称状压dp)是另一类非常典型的动态规划,通常使用在NP问题的小规模求解中,虽然是指数级别的复杂度,但速度比搜索快,其思想非常值得借鉴. 为了更好的理解状压dp,首先介绍位运算相关的知识. 1.'&'符号,x&y,会将两个十进制数在二进制下进行与运算,然后返回其十进制下的值.例如3(11)&2(10)=2(10). 2.'|'符号,x|y,会将两个十进制数在二进制下进行或运算,然后返回其十进制下的值.例如3(11)|2(10)=3(11). 3.'^'符号,x^y

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

状态压缩动态规划(简称状压dp)是另一类非常典型的动态规划,通常使用在NP问题的小规模求解中,虽然是指数级别的复杂度,但速度比搜索快,其思想非常值得借鉴. 为了更好的理解状压dp,首先介绍位运算相关的知识. 1.'&'符号,x&y,会将两个十进制数在二进制下进行与运算,然后返回其十进制下的值.例如3(11)&2(10)=2(10). 2.'|'符号,x|y,会将两个十进制数在二进制下进行或运算,然后返回其十进制下的值.例如3(11)|2(10)=3(11). 3.'^'符号,x^y

POJ 3254 Corn Fields 状态压缩DP (C++/Java)

http://poj.org/problem?id=3254 题目大意: 一个农民有n行m列的地方,每个格子用1代表可以种草地,而0不可以.放牛只能在有草地的,但是相邻的草地不能同时放牛, 问总共有多少种方法. 思路: 状态压缩的DP. 可以用二进制数字来表示放牧情况并判断该状态是否满足条件. 这题的限制条件有两个: 1.草地限制. 2.相邻限制. 对于草地限制,因为输入的时候1是可以种草地的. 以"11110"草地分析,就只有最后一个是不可以种草的.取反后得00001  .(为啥取反

HDU1565(状态压缩dp)

方格取数(1) Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8170    Accepted Submission(s): 3095 Problem Description 给你一个n*n的格子的棋盘,每个格子里面有一个非负数.从中取出若干个数,使得任意的两个数所在的格子没有公共边,就是说所取的数所在的2个格子不能相邻,并且取出的数

HDU 3001【状态压缩DP】

题意: 给n个点m条无向边. 要求每个点最多走两次,要访问所有的点给出要求路线中边的权值总和最小. 思路: 三进制状态压缩DP,0代表走了0次,1,2类推. 第一次弄三进制状态压缩DP,感觉重点是对数据的预处理,利用数组分解各个位数,从而达到类似二进制的目的. 然后就是状态的表示,dp[s][i]表示状态s时到达i的最优值. 状态转移也一目了然,不废话. #include<stdio.h> #include<string.h> #include<algorithm> u

poj 3311 Hie with the Pie(状态压缩dp)

Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can afford to hire only one driver to do the deliveries. He will wait for 1 or more (up to 10) orders to be