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

 

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值