Victor and World(hdu 5418 旅行商问题)

Victor and World

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 262144/131072K (Java/Other)
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 $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 $u_i$, $v_i$ and $w_i$, describing a flight.$1\leq T\leq 20$.$1\leq n\leq 16$.$1\leq m\leq 100000$.$1\leq w_i\leq 100$.$1\leq u_i, v_i \leq 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
 
题意:有n个城市(1-n),已知城市之间的联通状况和距离,现在从城市1出发,走过每个城市,最后回到城市1,求最短距离。(旅行商问题变种)

思路


dp[i][j]:表示在i状态下,最后停留在j城市的所行最短距离。

dis[i][j]:表示从城市i到城市j的最短距离。

状态转移方程:dp[i|1<<k][k]=min(dp[i|1<<k][k] , dp[i][j]+dis[j][k]) ((1<<k)&i != 0)

算dp的时候可以先不用管最后有没有回到起点城市1,只要在最后dp算完,找最小值时每个dp[(1<<n)-1][i]减去dis[i][1]即可。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;

const int N=20;
const int INF =1e9+7;

int n,m;
int map[N][N]; //输入的城市之间的联通情况和距离
int dis[N][N]; //每两个城市间的最短距离
int dp[1<<17][N];

//初始化
void init()
{
    for(int i=0;i<=n;i++)
    {
        for(int j=0;j<=n;j++)
            map[i][j] = INF;
        map[i][i]=0;
    }
}

//算每两个城市之间的最短距离(计算dis[ ][ ])
void SPFA(int x)
{
    for(int i=1;i<=n;i++)
        dis[x][i]=INF;
    queue<int>q;
    q.push(x);
    dis[x][x]=0;
    while(!q.empty())
    {
        int now=q.front();
        q.pop();
        for(int i=1;i<=n;i++)
        {
            if(map[now][i]==INF)
                continue;
            if(dis[x][i]>dis[x][now]+map[now][i])
            {
                dis[x][i]=dis[x][now]+map[now][i];
                q.push(i);
            }
        }
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        init();
        for(int i=1;i<=m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            if(map[u][v]>w)
            {
                map[u][v]=map[v][u]=w;
            }
        }
        
        for(int i=1;i<=n;i++)
            SPFA(i);
            
        for(int i=0;i<(1<<n);i++)
            for(int j=1;j<=n;j++)
                dp[i][j]=INF;
                
        dp[1][1]=0; //一开始必须从城市1出发
        
        //若可以从任意城市出发:初始化:dp[1][*]=0;
        
        for(int i=1;i<(1<<n);i++)
        {
            for(int j=1;j<=n;j++)
            {
                // j必须在i状态中为1   
                if( !((1<<(j-1))&i) )
                    continue;
                for(int k=1;k<=n;k++)
                {
                    //k需在i状态中为0
                    if( !((1<<(k-1))&i) )
                     {
                        dp[i|(1<<(k-1))][k] = min(dp[i|(1<<(k-1))][k] , dp[i][j]+dis[j][k]);
                     }
                }
            }
        }
        int ans=INF;
        for(int i=1;i<=n;i++)
        {
            //加上终点i到城市1的距离
            ans=min(ans, dp[(1<<n)-1][i]+dis[i][1]);
        }
        printf("%d\n",ans);
    }
    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值