码迷,mamicode.com
首页 > 其他好文 > 详细

hdu5418 BestCoder Round #52 (div.2) Victor and World ( floyd+状压dp)

时间:2015-08-26 22:37:57      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

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

Source
BestCoder Round #52 (div.2)

题目意思。所有的城市都得走一次,最后回道点1,求最下好油量。。


#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#pragma comment(linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define EPS 1e-6
#define INF (1<<24)
#define mod 1000000007
#define inf 0x1f1f1f1f
using namespace std;
int n,m;
int cost[20][20];  //任意两点的用量
int dp[1<<20][20];  //状态x下,,最后到的城市
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d",&n,&m);

        int i,j,k;
        for(i=1;i<=n;i++)
        {
            cost[i][i]=0;
            for(j=0;j<=n;j++)
            {
                if(i!=j) cost[i][j]=INF;
            }
        }
        int a,b,c;
        for(i=1;i<=m;i++)
        {
            scanf("%d %d %d",&a,&b,&c);
            if(cost[a][b]>c) cost[a][b]=cost[b][a]=c;
        }

        for(k=1;k<=n;k++)
            for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
            cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);  //任意两点之间的少用量

        int zhuang=(1<<n)-1;
        memset(dp,inf,sizeof(dp));
        dp[1][1]=0;
        for(i=1;i<=zhuang;i++)
        for(k=1;k<=n;k++)  //枚举最后到达的城市
        {
            if(i&(1<<(k-1)))
            for(j=1;j<=n;j++)  //下一个到达的地方
            {
                if(i==j) continue;
                if(i&(1<<(j-1))) continue;
                else dp[i|(1<<(j-1))][j]=min(dp[i|(1<<(j-1))][j],dp[i][k]+cost[k][j]);
            }
        }
        int out=INF;
        for(i=1;i<=n;i++) out=min(out,dp[zhuang][i]+cost[i][1]);
        printf("%d\n",out);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu5418 BestCoder Round #52 (div.2) Victor and World ( floyd+状压dp)

标签:

原文地址:http://blog.csdn.net/xtulollipop/article/details/48009489

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!