Victor and World
Time limit:4000/2000 MS (java/others) Memory limit:262144/131072 K (java/others)
Total submission (s): 891 Accepted Submission (s): 399
Problem Descriptionafter trying hard for many years, Victor have finally received a pilot license. To has a celebration, he intends to buy himself an airplane and fly around the world. There isNCountries on the Earth, which is numbered from1ToN. They is connected bymundirected flights, detailedly theI-th Flight connects theuI -th and thevi -th country, and it'll cost Victor ' s airplaneWi 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 are 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.
Inputthe first line of the input contains an integerT, denoting the number of test cases.
In every test case, there is and integersNandmIn the first line, denoting the number of the countries and the number of the flights.
Then there ismLines, each line contains three integersuI ,vi andWi , describing a flight.
1≤T≤ .
1≤n≤ .
1≤m≤100000 .
1≤wi≤ .
1≤ui,vi≤n.
Outputyour 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 Input13 21 2 21 3 3
Sample Output10
Sourcebestcoder Round #52 (Div.2) The main idea: to give you a T group of data, each group of data given N and M, respectively, the N cities, M route, and then there will be M-line u,v,w respectively indicate the beginning and end of the route and the amount of fuel needed to leap this route, Ask you the minimum amount of fuel you have to fly back to the city at least 1 times from the City 1 through all the other cities. Problem-solving ideas: First go to the heavy side, and then run Floyd calculate the shortest path between all cities dis, pay attention to d[i][i]=0. Because the number of n is very small, it can be seen that the problem of the state compression. We use dp[s][i] to indicate that the current city access status is S, and the last city to arrive is the minimum fuel consumption for I. s indicates the state, that is, the binary indicates whether the city has been. The state transition equation is dp[s| ( 1<< (i-1))][i]=min (dp[s| ( 1<< (i-1))][i],dp[s][j]+dis[j][i]) I and J should meet s& (1<< (i-1)) ==0&& s& (1<< (j-1))!=0. Indicates that S state has not yet reached I city, S state has been to the J City. Finally, the minimum value is obtained by enumerating all end points as 1.
#include <bits/stdc++.h>using namespace Std;const int inf=0x3f3f3f3f;int d[20][20],dp[(1<<20)][20];int Main () {int t,n,m,a,b,c; scanf ("%d", &t); while (t--) {memset (d,inf,sizeof (d)); Memset (Dp,inf,sizeof (DP)); scanf ("%d%d", &n,&m); for (int i=0;i<m;i++) {scanf ("%d%d%d", &a,&b,&c); if (d[a][b]>c) {d[a][b]=d[b][a]=c; }} for (int i=1;i<=n;i++) d[i][i]=0; for (int k=1;k<=n;k++) {for (int. i=1;i<=n;i++) {for (int j=1;j<=n;j++) { if (d[i][k]<inf&&d[k][j]<inf) d[i][j] = d[i][j]< (D[i][k]+d[k][j])? D[I][J]:(D[i][k]+d[k ][J]); }}} dp[1][1]=0; for (int s=1;s< (1<<n), s++) {for (int. i=1;i<=n;i++) {for (int j=1;j<=n;j++) { if (((s& (1<< (i-1)) ==0) && ( (s& (1<< (j-1)))) dp[s| ( 1<< (i-1))][i]=min (dp[s| ( 1<< (i-1))][i],dp[s][j]+d[j][i]); }}} int ans=inf; for (int i=1;i<=n;i++) {ans=min (ans,dp[(1<<n) -1][i]+d[i][1]); } printf ("%d\n", ans); } return 0;}
HDU 5418--victor and World —————— "state compression +floyd"