HDU 5418 Victor and World(状压dp、floy最短路)

本文介绍了一种算法解决旅行者Victor环球飞行的问题,通过计算最小燃料消耗来访问所有国家并返回起点。使用Floyd算法预处理最短路径,并通过状态压缩DP找到最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Victor and World Time Limit:2000MS    Memory Limit:131072KB    64bit IO Format:%I64d & %I64u

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   countries on the earth, which are numbered from   to   . They are connected by   undirected flights, detailedly the   -th flight connects the     -th and the     -th country, and it will cost Victor's airplane     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   , 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   and   in the first line, denoting the number of the countries and the number of the flights.

Then there are   lines, each line contains three integers     ,     and     , describing a flight.

  .

  .

  .

    .

      .

Output

Your program should print   lines : the   -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

首先用Folyd求出任意两点间的最短路。

然后令dp[i][S]表示访问情况为S,最后访问i国家的最小花费。

转移方程为dp[i][S|(1<<i-1)]=min(dp[j][S]+mp[j][i])(对于所有的j)(i,j满足集合S包含j不包含i,S是用二进制表示的集合)

mp[j][i]表示从j到i的最短路。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
int map[17][17];
int dp[17][1<<17];
int main()
{
    int t,n,m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m);
        int x,y,d;
        memset(map,0x3f,sizeof(map));
        for(int i=0;i<m;i++){
            scanf("%d %d %d",&x,&y,&d);
            map[y][x]=map[x][y]=min(map[x][y],d);
        }
        for(int i=0;i<=n;i++) map[i][i]=0;
        for(int k=1;k<=n;k++)
            for(int i=1;i<=n;i++)
              for(int j=1;j<=n;j++)
                map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
        memset(dp,0x3f,sizeof(dp));
        dp[1][1]=0;
        for(int i=1;i<=((1<<n)-1);i++)///zhuang tai
        {
            for(int j=1;j<=n;j++)///qi dian
            {
                if(i&(1<<(j-1)))
                    for(int k=1;k<=n;k++)///zhong dian
                    {
                        if((i &(1 <<(k -1))) == 0)
                            dp[k][i|(1<<(k-1))]=min(dp[k][i|(1<<(k-1))],dp[j][i]+map[j][k]);
                    }
            }
        }
        int ans=0x3f3f3f3f;
        for(int i=2;i<=n;i++)
            ans=min(ans,dp[i][(1<<n)-1]+map[i][1]);
        if(n==1) ans=0;
        printf("%d\n",ans);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值