hdu 5418 Victor and World 狀態壓縮dp,旅行商問題

來源:互聯網
上載者:User

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 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   

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;}/*334101 2 11 3 11 4 102 3 12 4 103 4 11 3 101 4 101 2 101 3 103 21 2 21 3 3*/


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.