hdu-5418

hdu-5418
src:http://acm.hdu.edu.cn/showproblem.php?pid=5418
Victor and World
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 1913 Accepted Submission(s): 895

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.

1≤T≤20.

1≤n≤16.

1≤m≤100000.

1≤wi≤100.

1≤ui,vi≤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

//编号1-n 从1开始求最短哈密尔顿回路
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 17;
const int MAXM = 100010;
const int INF = 0x3f3f3f3f;
int maps[MAXN][MAXN],dp[1<<MAXN][MAXN];
int t,n,m,ans;
void floyd(){
    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(maps[i][k]<INF && maps[k][j]<INF)
                    maps[i][j]=min(maps[i][j],maps[i][k]+maps[k][j]);
}
void hamiton(){//非递归方式
    memset(dp,0x3f,sizeof(dp));
    dp[1][1]=0;
    //枚举所有状态,二进制s每位按序代表一个城市,回路从1开始必须包含城市1
    for(int s=3;s<=(1<<n)-1;s++)if(s&1){
        for(int i=2;i<=n;i++){
            if( s&(1<<(i-1)) ){ //状态s经过城市i
                if( s==(1<<(i-1)) ) //只过城市i,这是dp边界
                    dp[s][i]=maps[1][i];
                else{
                    for(int j=1;j<=n;j++)//枚举不是城市i的在状态s中的其他城市j,并以j为中间点,求出最小的dp[s][i],类似floyd思想
                        if( (s&(1<<(j-1))) && j!=i)
                            dp[s][i]=min( dp[s][i], dp[s^(1<<(i-1))][j]+maps[j][i] );
                }
            }
        }
    }
    ans=INF;
    for(int i=2;i<=n;i++)
        ans=min(ans,dp[(1<<n)-1][i]+maps[i][1]);
    if(ans==INF) ans=0;//n=1
}
//dfs(s,id)当前已访问的城市状态为s,上一次递归访问城市id
//dp[s][id]表示从1到id(不包括s中的城市)的最短路径长度
int dfs(int s, int id){//递归方式
    if(dp[s][id]>0) return dp[s][id];//如果dp[s][id]有值说明已最小
    int mi=INF;
    for(int i=1;i<=n;i++){
        if(i==1 && s==(1<<n)-1)//如果状态为11..1,说明到达递归边界
            mi=min(mi,maps[id][1]);
        if( s&(1<<(i-1)) ) continue;
        //遍历所有未访问城市i,得到所有最短路中的最小值
        mi=min( mi, dfs(s|(1<<(i-1)),i)+maps[i][id] );
    }
    return dp[s][id]=mi;//dp[s][id]更新即最小
}

int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d %d",&n,&m);
        memset(maps,0x3f,sizeof(maps));
        for(int i=0;i<=n;i++) maps[i][i]=0;
        int a1,a2,a3;
        for(int i=0;i<m;i++){
            scanf("%d %d %d",&a1,&a2,&a3);
            if(maps[a1][a2]>a3)
                maps[a1][a2]=maps[a2][a1]=a3;
        }
        floyd();
        hamiton();printf("%d\n",ans);//使用非递归方式
        //memset(dp,0,sizeof(dp));printf("%d\n",dfs(1,1));//使用递归方式
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值