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

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;
}

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

时间: 2024-08-04 23:53:33

hdu5418 BestCoder Round #52 (div.2) Victor and World ( floyd+状压dp)的相关文章

Codeforces Round #321 (Div. 2) D. Kefa and Dishes (状压dp,再A一发)

题意: 有n个菜,需要m个菜,k个规则  每个菜吃下去的满足感为 a1......an 每个规则: 吃完第u个菜后吃第v个菜满足感+c; 问:怎么吃才能幸福感最大? dp[1<<18][20]:一维表示吃了的菜(1表示吃了,0表吃没吃),第二维表示最后一个吃的菜 dp的初始化: dp[1<<i][i] = saty[i]; 状态转移:dp[i|(1 << k)][k] = max(dp[i][j] + rule[j][k] + safy[k],dp[i|(1 <&

Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)

F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行,最后从上到下,从左到右形成一个序列s1,s2.....snm,满足对于任意相邻的两个数,它们差的绝对值的最大值为k. 现在问怎么交换行与行,可以使得最后的这个k最大. 题解: 人生中第一道状压dp~其实还是参考了这篇博客:https://blog.csdn.net/CSDNjiangshan/art

BestCoder Round #52 (div.2) HDU 5418 Victor and World (DP+状态压缩)

[题目链接]:click here~~ [题目大意]: 问题描写叙述 经过多年的努力,Victor最终考到了飞行驾照. 为了庆祝这件事,他决定给自己买一架飞机然后环游世界. 他会驾驶一架飞机沿着规定的航线飞行.在地球上一共同拥有nn个国家,编号从11到nn.各个国家之间通过mm条双向航线连接,第ii条航线连接第u_iu?i??个国家与第v_iv?i??个国家,通过这条航线须要消耗w_iw?i??升油.且从11号国家能够直接或间接到达22到nn中随意一个国家. Victor一開始位于11号国家.他

HDOJ 5418 Victor and World 状压DP

水状压DP Victor and World Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others) Total Submission(s): 407    Accepted Submission(s): 164 Problem Description After trying hard for many years, Victor has finally received a p

DP BestCoder Round #50 (div.2) 1003 The mook jong

题目传送门 1 /* 2 DP:这题赤裸裸的dp,dp[i][1/0]表示第i块板放木桩和不放木桩的方案数.状态转移方程: 3 dp[i][1] = dp[i-3][1] + dp[i-3][0] + 1; dp[i][0] = dp[i-1][1] + dp[i-1][0]; 4 比赛时二维dp写搓了,主要是边界情况的判断出错,比如dp[3][1] = 1,因为第3块放了木桩,其他地方不能再放,dp[3][0]同理 5 解释一下dp[i][1]的三种情况,可能是前面相隔2个放的方案或者是不放的

计算几何(水)BestCoder Round #50 (div.2) 1002 Run

题目传送门 1 /* 2 好吧,我不是地球人,这题只要判断正方形就行了,正三角形和正五边形和正六边形都不可能(点是整数). 3 但是,如果不是整数,那么该怎么做呢?是否就此开启计算几何专题了呢 4 */ 5 /************************************************ 6 * Author :Running_Time 7 * Created Time :2015-8-8 19:54:14 8 * File Name :B.cpp 9 ************

BestCoder Round #11 (Div. 2) 前三题题解

题目链接: huangjing hdu5054 Alice and Bob 思路: 就是(x,y)在两个參考系中的表示演全然一样.那么仅仅可能在这个矩形的中点.. 题目: Alice and Bob Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 216    Accepted Submission(s): 166 Problem De

BestCoder Round #11 (Div. 2) 题解

HDOJ5054 Alice and Bob Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 302    Accepted Submission(s): 229 Problem Description Bob and Alice got separated in the Square, they agreed that if they

HDU 5651 xiaoxin juju needs help(BestCoder Round #77 (div.1)1001)

传送门 xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 861    Accepted Submission(s): 243 Problem Description As we all known, xiaoxin is a brilliant coder. He knew **palin