hdu 5418 Victor and World

click here~~

                                        ***Victor and World***

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

题目大意:经过多年的努力,Victor终于考到了飞行驾照。为了庆祝这件事,他决定给自己买一架飞机然后环游世界。他会驾驶一架飞机沿着规定的航线飞行。在地球上一共有nn个国家,编号从11到nn,各个国家之间通过mm条双向航线连接,第ii条航线连接第u_iu

?i

?? 个国家与第v_iv

?i

?? 个国家,通过这条航线需要消耗w_iw

?i

?? 升油,且从11号国家可以直接或间接到达22到nn中任意一个国家。

Victor一开始位于11号国家,他想知道从11号国家出发,经过各个国家至少一次并最后回到11号国家消耗的总油量的最小值是多少。

解题思路:

Floyd算法和状态dp

上代码:

/*
2015 - 8 - 29 中午
Author: ITAK

今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
*/
#include <cstdio>
#include <iostream>
using namespace std;
#define INF 1e9+7
const int sizet = (1<<16);
int d[16][16];
int dp[sizet][16];
int n,m;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0; i<n; ++i)
            for(int j=0; j<n; ++j)
            {
                if(i == j) d[i][j] = 0;
                else d[i][j] = INF;
            }
        int u,v,w;
        for(int i=0; i<m; ++i)
        {
            scanf("%d%d%d",&u,&v,&w);
            if(d[u-1][v-1] > w)
            {
                d[u-1][v-1] = w;
                d[v-1][u-1] = w;
            }
        }
        for(int k=0; k<n; k++)
            for(int i=0; i<n; i++)
                for(int j=0; j<n; j++)
                    d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
        for(int i=0; i<sizet; ++i)
            for(int j=0; j<n; ++j)
                dp[i][j] = INF;
        dp[1][0] = 0;
        for(int k=1; k<sizet; ++k)
            for(int i=0; i<n; ++i)
                for(int j=0; j<n; ++j)
                    dp[k|(1<<i)|(1<<j)][i] = min(dp[k|(1<<i)|(1<<j)][i],d[j][i]+dp[k|(1<<j)][j]);
        int minx=INF;
        int tt = (1<<n)-1;
        for(int i=0; i<n; i++)
            if(minx > dp[tt][i]+d[i][0])
                minx = dp[tt][i]+d[i][0];
        printf("%d\n",minx);
    }
    return 0;
}

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

时间: 2024-10-10 10:29:24

hdu 5418 Victor and World的相关文章

ACM: HDU 5418 Victor and World - Floyd算法+dp状态压缩

HDU 5418 Victor and World Time Limit:2000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u 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 fl

HDU 5418 Victor and World 允许多次经过的TSP

题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5418 bestcoder(中文): http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=619&pid=1002 Victor and World Accepts: 99 Submissions: 644 Time Limit: 4000/2000 MS (Java/Others) Memory Limi

HDU 5418 Victor and World (状态压缩dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5418 题目大意:有n个结点m条边(有边权)组成的一张连通图(n <16, m<100000).求从第一个点出发,经过每个点至少一次后回到原点的最小路径边权和. 分析:发现我还真是菜. n<16,很明显的状态压缩标记,先将所有点的编号减去1,使其从0开始编号.dp[i][j]表示从0号点出发,当前状态为i (二进制位为1表示对应点已走过,否则没走过), 当前位置为 j,  回到原点的最小代价,

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号国家.他

HDU 5418——Victor and World——————【状态压缩+floyd】

Victor and World Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 891    Accepted Submission(s): 399 Problem Description After trying hard for many years, Victor has finally received a pilot li

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

给你n个国家,m条路线:u_i与v_i之间的距离w_i. 输出从1号国家出发经过每个国家至少一次再回到1号国家的最短距离. [官方题解]: 我们首先需要预处理出任意两个国家之间的最短距离,因为数据范围很小,所以直接用Floyd就行了. 之后,我们用f[S][i]表示访问国家的情况为S,当前最后访问的一个国家是i所需要的最小总油量,其中,S的二进制表示记录了访问国家的情况,S在二进制表示下的第i位(不管是从左往右还是从右往左都可以).如果是1则表示第i个国家被访问过了,否则表示第i个国家没有被访问

HDU 5417 Victor and Machine

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5417 Problem Description Victor has a machine. When the machine starts up, it will pop out a ball immediately. After that, the machine will pop out a ball every w seconds. However, the machine has some f

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

HDU 5419——Victor and Toys——————【线段树|差分前缀和】

Victor and Toys Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 654    Accepted Submission(s): 219 Problem Description Victor has n toys, numbered from 1 to n. The beauty of the i-th toy is wi