hdu5418 bestcoder Round #52 (Div.2) Victor and World (floyd+ pressure DP)

Source: Internet
Author: User

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 it WI ll 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 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 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 number 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 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)

Topic meaning. All the cities have to go once, the last point 1, to find the most good oil quantity.

#include <cstdio>#include <cmath>#include <cstring>#include <iostream>#include <algorithm>#include <queue>#include <vector>#include <map>#include <stack>#pragma COMMENT (linker, "/stack:102400000,102400000")#define PI ACOs ( -1.0)#define EPS 1e-6#define INF (1<<24)#define MOD 1000000007#define INF 0x1f1f1f1fusing namespace STD;intN,m;intcost[ -][ -];//Any two-point dosageintdp[1<< -][ -];//Status X,, last to the cityintMain () {intTscanf("%d", &t); while(t--) {scanf("%d%d", &n,&m);intI,j,k; for(i=1; i<=n;i++) {cost[i][i]=0; for(j=0; j<=n;j++) {if(i!=j) Cost[i][j]=inf; }        }intA,b,c; for(i=1; i<=m;i++) {scanf(" %d%d%d", &a,&b,&c);if(cost[a][b]>c) Cost[a][b]=cost[b][a]=c; } for(k=1; k<=n;k++) for(i=1; i<=n;i++) for(j=1; j<=n;j++) Cost[i][j]=min (Cost[i][j],cost[i][k]+cost[k][j]);//Small amount between any two points        intZhuang= (1<<n)-1;memset(Dp,inf,sizeof(DP)); dp[1][1]=0; for(i=1; i<=zhuang;i++) for(k=1; k<=n;k++)//Enumerate the last cities to reach{if(i& (1<< (K-1))) for(j=1; j<=n;j++)//Next place to arrive{if(I==J)Continue;if(i& (1<< (J-1)))Continue;ElseDp[i| (1<< (J-1))][j]=min (dp[i| (1<< (J-1)) (][j],dp[i][k]+cost[k][j]); }        }intOut=inf; for(i=1; i<=n;i++) Out=min (out,dp[zhuang][i]+cost[i][1]);printf("%d\n", out); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdu5418 bestcoder Round #52 (Div.2) Victor and World (floyd+ pressure DP)

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.