HDU - 5418 Victor and World(最短哈密顿回路)

Victor and World

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


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.

1T20.

1n16.

1m100000.

1wi100.

1ui,vin.
 

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
 

Recommend
hujie
 


题意:点可重复经过,求经过所有点并回到原点的最短路径。

解题思路:哈密顿回路模板题。状压DP解决。dp[i][j]表示当前状态为i,最后经过的点为j的最短路径。


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 50;
const int INF = 0x3f3f3f3f;

int G[MAXN][MAXN];

int dp[(1<<16)+10][MAXN];

int n,M;

int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&M);
        int u,v,w;

        memset(G,0x3f,sizeof(G));
        memset(dp,0x3f,sizeof(dp));

        for(int i=0;i<=n;i++)
            G[i][i]=0;

        for(int i=0;i<M;i++){
            scanf("%d%d%d",&u,&v,&w);
            u--;
            v--;
            if(w<G[u][v]){
                G[u][v]=w;
                G[v][u]=w;
            }
        }

        for(int k=0;k<n;k++)
            for(int i=0;i<n;i++)
                for(int j=0;j<n;j++)
                    G[i][j]=min(G[i][j],G[i][k]+G[k][j]);
//这种顺推写法慢了200s
        //        dp[1][0]=0;
        //        for(int s=1;s<(1<<N);s++){
        //            for(int i=0;i<N;i++){
        //                if(s&(1<<i)){
        //                    for(int j=0;j<N;j++)
        //                        if((s&(1<<j))==0)
        //                            dp[s | (1 << j)][j] = min(dp[s | (1 << j)][j], dp[s][i] + G[i][j]);
        //                }
        //            }
        //        }

        for(int s=0;s<(1<<(n-1));s++)
            for(int i=1;i<n;i++)
                if(s&(1<<(i-1))){
                    if(s==(1<<(i-1))) dp[s][i]=G[0][i];
                    else{
                        dp[s][i]=INF;
                        for(int j=1;j<n;j++)
                            if(s&(1<<(j-1))&&j!=i)
                                dp[s][i]=min(dp[s][i],dp[s^(1<<(i-1))][j]+G[j][i]);
                    }
                }

        int ans=dp[(1<<(n-1))-1][1]+G[1][0];
        for(int i=2;i<n;i++)
            ans=min(ans,dp[(1<<(n-1))-1][i]+G[i][0]);

        if (n== 1)
            printf("0\n");
        else
            printf("%d\n", ans);

    }

    return 0;
}




猜你喜欢

转载自blog.csdn.net/lzc504603913/article/details/80068038