Victor and World (spfa+ State compression DP)

Source: Internet
Author: User

Topic Connection: http://acm.hdu.edu.cn/showproblem.php?pid=5418Victor and World

Time limit:4000/2000 MS (java/others) Memory limit:262144/131072 K (java/others)
Total submission (s): 958 Accepted Submission (s): 431


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)

Test instructions: Give a map, start at one point, walk through all the points, and then go back to this point to find the shortest path

The problem: Pay attention to the data range is 16 points, but also to seek Hamilton loop problems, think of the state compression DP, the problem can learn a good idea is the idea of SPFA, will be used in DP, take out the state of the team head, see this state stop at which point, with this point update all can update the state, Because each point can be traversed multiple times, so each update from 0 to N in turn, the state of the new update if not previously accessed, it indicates that the state has the potential to update other states, so it is pressed into the queue, in this way can update all states.

Briefly state compression DP, using a binary number to indicate whether a point is in the set, the complete is (1<<n)-1; J The set is added with an element k is j| ( 1<<K); Removing an element from J set K is j^ (1<<k);

Dp[j][k] represents the shortest path that passes all elements in the set J and finally stays at the K point

Transfer equation: dp[s| ( 1<<i)][i] = min (dp[s| ( 1<<i)][i], Dp[s][u]+mp[u][i])

Note: Here the side is two-way edge, when the input edge will have a lot of invalid edges, to take the smallest.

With Qair<int, int> is equivalent to a struct{int a, int b};

Code:

1#include <cstdio>2#include <cstring>3#include <queue>4#include <algorithm>5 using namespacestd;6 7 intdp[1<< -][ -];8 intmp[ -][ -];9 intdis[ -];Ten BOOLvis[1<< -][ -]; One intN; A  - intMain () - { the     intT, M; -scanf"%d",&T); -      while(t--) -     { +scanf"%d%d",&n,&m); -Memset (MP,0x7f,sizeof(MP)); +          for(inti =0; i < n; i++) Mp[i][i] =0; A          for(inti =0; i < m; i++) { at             intu, V, D; -scanf"%d%d%d",&u,&v,&d); -U--, v--; -MP[U][V] = Mp[v][u] =min (mp[u][v],d); -         } -Memset (DP,0x7f,sizeof(DP)); inmemset (Vis,0,sizeof(Vis)); -dp[0][0] =0; vis[0][0] =1; toqueue<pair<int,int> >Q; +Q.push (Make_pair (0,0)); -          while(!q.empty ()) the         { *             ints =Q.front (). First; $             intU =Q.front (). Second;Panax Notoginseng Q.pop (); -              for(inti =0; I < n; i++) the             { +                 intSS = s| (1<<i); A                 if(Dp[ss][i]>dp[s][u] +Mp[u][i]) { theDp[ss][i] = Dp[s][u] +Mp[u][i]; +                     if(Vis[ss][i] = =0){ -Vis[ss][i] =1 ; $ Q.push (Make_pair (ss,i)); $                     } -                 } -             } the         } -printf"%d\n", dp[(1<<n)-1][0]);Wuyi     } the     return 0 ; -}

Victor and World (spfa+ State compression 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.