HDU 5418 Victor and World State compression DP, travel business issue

Source: Internet
Author: User
Tags min

Victor and World Time limit:4000/2000 MS (java/others) Memory limit:262144/131072 K (java/others)
Total submission (s): 385 Accepted Submission (s): 160


Problem Description After trying-many years, Victor has finally received a pilot license. To has a celebration, he intends to buy himself an airplane and fly around the world. There is n countries on the Earth, which is numbered from 1 to N. They is connected by M undirected flights, detailedly the i-th flight connects the ui-th and the vi-th country, and I T would 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 was at the country whose number was 1, he wants to know the minimal amount of fuel for him to visit every countr Y 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 the test cases.
In every test case, there is, integers n and m in the first line, denoting the number of the countries and the Numbe R of the flights.

Then there is 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 Amoun T 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)


#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace
Std
int map[20][20];

int dp[1<<17][20];
    int main () {int t,n,m,w,u,v;
    scanf ("%d", &t);
        while (t--) {scanf ("%d%d", &n,&m);
        memset (map,0x3f,sizeof (map));
        for (int i = 0;i < n; i++) map[i][i] = 0;
            for (int i = 0;i < m; i++) {scanf ("%d%d%d", &u,&v,&w);
            U--, v--;
        MAP[U][V] = map[v][u] = min (map[u][v],w); } for (int i = 0;i < n;i++) for (int j = 0;j < N; j + +) for (int k = 0;k < n; k +
        +) Map[j][k] = map[k][j] = min (map[j][k],map[j][i]+map[i][k]);
        Memset (Dp,0x3f,sizeof (DP));
        Dp[1][0] = 0;
                    for (int i = 1;i < (1<<n), i++) {for (int j = 0;j < N; j + +) {if (i& (1<<j)) {
            for (int k = 0;k < n; k++) {            if ((i& (1<<k)) ==0 && J! = k) {dp[i| ( 1<<k)][k] = min (dp[i| (
                        1<<k)][k],dp[i][j]+map[j][k]);
        }}}}} int ans = dp[0][0];
        for (int i = 0;i < n; i++) {ans = min (ans,dp[(1<<n) -1][i]+map[i][0]);
    } printf ("%d\n", ans);
} return 0;




















 }/* 33 4 10 1 2 1 1 3 1 1 4 10 2 3 1 2 4 10 3 4 1 1 3 10 1 4 10 1 2 10 1 3 10 3 2 1 2 2 1 3 3 * *


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.