Victor and World

题目链接: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): 2455    Accepted Submission(s): 1123


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 uivi 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
 
题目大意:找一条最短路径,从1开始,经过每个城市一次,最后回到1.
思路:状压dp入门题
看代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stack>
#include<map>
#include<queue>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
#define sc1(a) scanf("%lld",&a)
#define pf1(a) printf("%lld
",a)
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const LL INF=1e18;
const ull base=2333;
const int maxn=1e1+50;
const int maxm=1e2+50;
const int maxv=1e6+5;
const int mod=1e9+7;
const int ba=3e5;
LL N,M;
LL cost[maxn][maxn];
LL dp[maxn][1<<20];
void Init()
{
    for(int i=1;i<=N;i++)
    {
        for(int j=1;j<=N;j++)
        {
            cost[i][j]=INF;
        }
        cost[i][i]=0;//
    }
    for(int i=1;i<=N;i++)
    {
        for(int j=0;j<(1<<N);j++) dp[i][j]=INF;
    }
}
void floyed()
{
    for(int i=1;i<=N;i++)
    {
        for(int j=1;j<=N;j++)
        {
            for(int k=1;k<=N;k++)
            {
                cost[j][k]=min(cost[j][k],cost[j][i]+cost[i][k]);
            }
        }
    }
}
void solve()
{
    //首先在1的位置
    /**
    dp[j][s]表示当前在j位置 走过的集合为s  最短的路径是多少
    dp[j][s|(1<<(j-1))]=min(dp[j][s|(1<<(j-1))],dp[i][s]+cost[i][j])  从i走到j
    */
    dp[1][1]=0;
    for(int s=0;s<(1<<N);s++)//所有的可能情况
    {
        for(int i=1;i<=N;i++)//从这里来 枚举i-j
        {
            if(s&(1<<(i-1))) //有这一位才能从这里到j
            {
                for(int j=1;j<=N;j++)
                {
                    if(!(s&(1<<(j-1)))&&cost[i][j]!=INF) //没有走过当前的j 并且i能够到j
                    dp[j][s|(1<<(j-1))]=min(dp[j][s|(1<<(j-1))],dp[i][s]+cost[i][j]);
                }
            }
        }
    }
    LL ans=INF;
    for(int i=1;i<=N;i++)
    {
        ans=min(ans,dp[i][(1<<N)-1]+cost[i][1]);
    }
    pf1(ans);
}
int main()
{
    LL T;sc1(T);
    while(T--)
    {
        sc1(N);sc1(M);
        Init();
        LL u,v,w;
        for(int i=1;i<=M;i++)
        {
            sc1(u);sc1(v);sc1(w);
            cost[u][v]=min(cost[u][v],w);
            cost[v][u]=min(cost[v][u],w);
        }
        floyed();
        solve();
    }
    return 0;
}
/**

*/
当初的梦想实现了吗,事到如今只好放弃吗~
原文地址:https://www.cnblogs.com/caijiaming/p/12312328.html