首页 > 其他分享 >HDU 5418 Victor and World 状压DP的TSP问题

HDU 5418 Victor and World 状压DP的TSP问题

时间:2023-02-07 18:07:24浏览次数:91  
标签:HDU int 状压 5418 ++ mp -- include dp


 

Victor and World

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

 

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 u i  -th and the v i  -th country, and it will cost Victor's airplane w i  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  , 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 u i  , v i  and w i  , describing a flight.

1≤T≤20  .

1≤n≤16  .

1≤m≤100000  .

1≤w i ≤100  .

1≤u i ,v i ≤n  .

 

 

Output

Your program should print  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) ​

 

 

Recommend

hujie   |   We have carefully selected several similar problems for you:  ​​6437​​​ ​​6436​​​ ​​6435​​​ ​​6434​​​ ​​6433​​ 

算法分析:

题意:

每个点都可以走多次的TSP问题:有n个点(n<=16),从点1出发,经过其他所有点至少1次,并回到原点1,使得路程最短。

分析:

本题和经典TSP旅行商问题应用,

模板一样套。

思路:s表示已经经过的城市的集合,v表示现在正处在的城市。定义dp[s][v]为从v出发访问所有剩余的城市,再返回起点所需要的最短的路径。mp[i][j] 表示 i 到 j 的最短路。

V为所有顶点的集合。初始化dp[V][0] = 0

状态转移方程:

dp[s][v]=min(dp[s|{u}][u] +mp[u][v]) (u不属于s)

两个方法实现:记忆化搜索和递推的状态压缩DP

代码实现:

记忆化搜索:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define M 20
#define INF 0x3f3f3f3f
int n,m;
int mp[M][M];
int dp[1<<M][M];
void floyd()
{
for(int k = 0;k < n;k++)
for(int i = 0;i < n;i++)
for(int j = 0;j < n;j++)
mp[i][j] = min(mp[i][j],mp[i][k]+mp[k][j]);
}
int slove(int s,int v) //记忆化搜索
{
if(dp[s][v] >= 0) return dp[s][v]; //已经有结果
if(s == (1<<n)-1 && v == 0) return dp[s][v] = 0; //访问完所有城市
int ret = INF;
for(int u = 0;u < n;u++)//从u到v
{
if(!((s >> u) & 1)) //判断是否访问过,如果u这一位是0,即没有访问过
ret = min(ret,slove(s|(1<<u),u)+mp[v][u]);//状态转移
}
return dp[s][v] = ret; //记录结果
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
for(int i = 0;i < M;i++) fill(mp[i],mp[i]+n,INF);
for(int i = 0;i < m;i++)
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
a--;b--;
mp[a][b] = min(mp[a][b],c); //注意转化
mp[b][a] = mp[a][b];
}
for(int i = 0;i < n;i++)mp[i][i] = 0; //初始化0

floyd();
int ans = INF;
for(int j = 0;j < (1<<n);j++) fill(dp[j],dp[j]+n,INF);

floyd();
memset(dp,-1,sizeof(dp));
printf("%d\n",slove(0,0));
}
return 0;
}

递推:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define M 20
#define INF 0x3f3f3f3f
int n,m;
int mp[M][M];
int dp[1<<M][M];
void floyd()
{
for(int k = 0;k < n;k++)
for(int i = 0;i < n;i++)
for(int j = 0;j < n;j++)
mp[i][j] = min(mp[i][j],mp[i][k]+mp[k][j]);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
for(int i = 0;i < M;i++) fill(mp[i],mp[i]+n,INF);
for(int i = 0;i < m;i++)
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
a--;b--;
mp[a][b] = min(mp[a][b],c); //注意转化
mp[b][a] = mp[a][b];
}
for(int i = 0;i < n;i++)mp[i][i] = 0; //初始化0

floyd();
int ans = INF;
for(int j = 0;j < (1<<n);j++) fill(dp[j],dp[j]+n,INF);

dp[(1<<n)-1][0] = 0;
for(int s = (1<<n)-2;s >= 0;s--)
{
for(int v = 0;v < n;v++)
{
for(int u = 0;u < n;u++)
if(!(s >> u &1))
dp[s][v] = min(dp[s][v],dp[s|(1<<u)][u]+mp[v][u]);
}
}
printf("%d\n",dp[0][0]);
}
return 0;
}

 

标签:HDU,int,状压,5418,++,mp,--,include,dp
From: https://blog.51cto.com/u_14932227/6042553

相关文章