zoukankan      html  css  js  c++  java
  • HDU 5418 Victor and World(状压DP+Floyed预处理)

    Victor and World

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

    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
     

    题目链接:HDU 5418

    状态转移方程:dp[s'][v]=min(dp[s'][v], dp[s][u]+dis[u][v]),dp[a][b]表示当前已经到达过点的状态为a,且最后到达的点是b,那么显然一开始没有到达过任何点,但一开始所在点为1,因此dp[0][0]=0(把点的标号均减去1方便运算),然后枚举每一个s中的方案,s'为s到s'所要经过的点v,距离显然就要增加上dis[u][v],其中u为s中的点,v为s'中的点。

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define LC(x) (x<<1)
    #define RC(x) ((x<<1)+1)
    #define MID(x,y) ((x+y)>>1)
    #define CLR(arr,val) memset(arr,val,sizeof(arr))
    #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
    typedef pair<int, int> pii;
    typedef long long LL;
    const double PI = acos(-1.0);
    const int N = 17;
    int E[N][N], dp[1 << N][N];
    
    void init()
    {
        CLR(E, INF);
        CLR(dp, INF);
        for (int i = 0; i < N; ++i)
            E[i][i] = 0;
    }
    int main(void)
    {
        int tcase, n, m, i;
        scanf("%d", &tcase);
        while (tcase--)
        {
            init();
            scanf("%d%d", &n, &m);
            for (i = 0; i < m; ++i)
            {
                int a, b, w;
                scanf("%d%d%d", &a, &b, &w);
                --a;
                --b;
                E[a][b] = E[b][a] = min(E[a][b], w);
            }
            for (int k = 0; k < n; ++k)
                for (int i = 0; i < n; ++i)
                    for (int j = 0; j < n; ++j)
                        E[i][j] = min(E[i][j], E[i][k] + E[k][j]);
            dp[0][0] = 0;
            int st_cnt = 1 << n;
            for (int s = 0; s < st_cnt; ++s) //已经走过的点的状态
            {
                for (int u = 0; u < n; ++u)
                {
                    if (dp[s][u] != INF) //选取其中实际可行的方案
                    {
                        for (int v = 0; v < n; ++v) //枚举下一步走的方案
                        {
                            if ((s & (1 << v)))//v如果已经走过了就没有必要再走
                                continue;
                            int news = s | (1 << v);
                            dp[news][v] = min(dp[news][v], dp[s][u] + E[u][v]);
                        }
                    }
                }
            }
            printf("%d
    ", dp[(1 << n) - 1][0]);
        }
        return 0;
    }
  • 相关阅读:
    ThinkPHP 3.2.2 实现持久登录 ( 记住我 )
    Java实现 LeetCode 20 有效的括号
    Java实现 LeetCode 20 有效的括号
    Java实现 LeetCode 19删除链表的倒数第N个节点
    Java实现 LeetCode 19删除链表的倒数第N个节点
    Java实现 LeetCode 19删除链表的倒数第N个节点
    Java实现 LeetCode 18 四数之和
    Java实现 LeetCode 18 四数之和
    Java实现 LeetCode 18 四数之和
    Java实现 LeetCode 17 电话号码的字母组合
  • 原文地址:https://www.cnblogs.com/Blackops/p/6936288.html
Copyright © 2011-2022 走看看