hdu 5418 Victor and World 状态压缩dp spfa最短路 floyed最短路

Victor and World

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


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
BestCoder Round #52 (div.2)
 

Recommend
hujie   |   We have carefully selected several similar problems for you:   5421  5420  5417  5416  5415 
题意,要求从1开始起,经过所有的点,最终回到1,所要的最小值。

状态压缩,16位每位上为1表示已走,0表示没有走过,直接用位运算,表示所有的状态。

dp[i][j] 状态为i,最后一次是j的最小值,则由dp[i][j] + land[k][j] 推出dp[t][k] ,t表示,i把第k位置1的数。有2 ^ 16 * n个状态,n种转移,所以复杂度为o(2^16 * n * n);

第一种方法用spaf来解。

#define N 20
#define M 100005
#define maxn 205
#define MOD 1000000007
int n,land[N][N],T,m,a,b,c,dp[M][N],all,t;
queue q;
bool vis[M][N];
int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
     while(S(T)!=EOF)
    {
        while(T--){
            S2(n,m);
            FI(n+1) FJ(n+1) land[i][j] = MOD,vis[i][j] = false;
            FI(n+1) land[i][i] = 0;
            all = 1<

第二种方法,用Floyed来解,由于n很小,可以很快处理出来。

#define N 20
#define M 100005
#define maxn 205
#define MOD 1000000007
int n,land[N][N],T,m,a,b,c,dp[M][N],all,t;
queue q;
bool vis[M][N];
int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
     while(S(T)!=EOF)
    {
        while(T--){
            S2(n,m);
            FI(n+1) FJ(n+1) land[i][j] = MOD,vis[i][j] = false;
            FI(n+1) land[i][i] = 0;
            all = 1<


你可能感兴趣的:(图论,dp,hdu,模板)