Click here~~
Victor andWorld***problem Descriptionafter trying hard forMany years, Victor has finally received a pilot license. To has a celebration, he intends toBuy himself an airplane andFlyaround theWorld. There is n countries on theEarth, which is numbered from 1 toN. They is connected byM undirected flights, detailedly theI-th Flight connects theUi-th and theVi-th Country, and itWould cost Victor ' s airplane wi L fuelifVictor Fliesthrough it. andit isPossible forHim toFly to everyCountry from the FirstCountry. Victor now is at theCountrywhose Number is 1, he wants toKnow theMinimal amount ofFuel forHim toVisiteveryCountry atLeast once andFinallyreturn to the FirstCountry. Inputthe FirstLine of theInputcontainsAnintegerT, denoting the Number ofTest cases. IncheveryTest case, there is integers n andMinch the FirstLine, denoting the Number of theCountries and the Number of theFlights. Then there is m lines, each linecontainsThree integers UI, vi andWI, describing a flight.1≤t≤.1≤n≤.1≤m≤100000.1≤wi≤.1≤ui,vi≤n. Outputyour program should print T lines: theI-th ofThese shouldcontainA singleinteger, denoting theMinimal amount ofFuel forVictor toFinish theTravel. Sample Input13 21 2 21 3 3Sample OutputTen
After years of hard work, Victor finally got a flying license. To celebrate the event, he decided to buy himself a plane and travel around the world. He would fly a plane along the prescribed route. On Earth, a total of nn countries, numbering from 11 to NN, each country through the MM two-way route connection, section II route connection U_iu
? i
?? Countries and Section V_iv
? i
?? Countries, this route requires consumption of W_IW
? i
?? Oil, and from 11th countries can directly or indirectly reach 22 to any country in NN.
Victor begins in country 11th and wants to know the minimum amount of total oil consumed by countries at least once and finally returning to country 11th from country 11th.
Problem Solving Ideas:
Floyd algorithm and state DP
On the code:
/*2015-8-29 Noon Author:itak Today I want to go beyond yesterday's I, tomorrow I will surpass today's me, to create better code for the goal, constantly surpass themselves. */#include <cstdio>#include <iostream>using namespace STD;#define INF 1e9+7Const intSizet = (1<< -);intd[ -][ -];intdp[sizet][ -];intN,m;intMain () {intTscanf("%d", &t); while(t--) {scanf("%d%d", &n,&m); for(intI=0; i<n; ++i) for(intj=0; j<n; ++J) {if(i = = j) D[i][j] =0;ElseD[I][J] = INF; }intU,v,w; for(intI=0; i<m; ++i) {scanf("%d%d%d", &u,&v,&w);if(d[u-1][v-1] > W) {d[u-1][v-1] = W; d[v-1][u-1] = W; } } for(intk=0; k<n; k++) for(intI=0; i<n; i++) for(intj=0; j<n; J + +) D[i][j] = min (d[i][j],d[i][k]+d[k][j]); for(intI=0; i<sizet; ++i) for(intj=0; j<n; ++J) Dp[i][j] = INF; dp[1][0] =0; for(intk=1; k<sizet; ++K) for(intI=0; i<n; ++i) for(intj=0; j<n; ++J) dp[k| (1<<i) | (1<<J)][i] = min (dp[k| (1<<i) | (1<<J)][i],d[j][i]+dp[k| (1<<J)][j]);intMinx=inf;inttt = (1<<n)-1; for(intI=0; i<n; i++)if(Minx > dp[tt][i]+d[i][0]) Minx = dp[tt][i]+d[i][0];printf("%d\n", Minx); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 5418 Victor and World