Acm:hdu 5418 Victor and World-floyd algorithm +DP State compression

Source: Internet
Author: User

HDU 5418Victor and WorldTime limit:2000MS Memory Limit:131072KB 64bit IO Format:%i64d &%i6 4u 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 isCountries on the Earth, which is numbered fromTo. They is connected byundirected flights, detailedly the-th Flight connects the-th and the-th country, and it'll cost Victor ' s airplaneL fuel If Victor flies through it. And it is possible for him to fly to every country from the first country.

Victor now are at the country whose number are, he wants to know the minimal amount of fuel in him to visit E Very country at least once and finally return to the first country.

Input

The first line of the input contains an integer, denoting the number of test cases.
In every test case, there is and integersandIn the first line, denoting the number of the countries and the number of the flights.

Then there isLines, each line contains three integers,and, describing a flight.

.

.

.

.

.

Output

Your program should print lines:the -th of these should contain a single integer, Denotin G The minimal amount of fuel for Victor to finish the travel.

Sample Input

13 21 2 21 3 3

Sample Output

10

/* /At the beginning of the topic did not read carefully, thought is a minimum tree, the second WA a pitch; later I want to find this has a ring, it is not the smallest tree, searched a bit is floyd+dp-shaped pressure. After writing has been found that the output is inf=0x3f3f3f3f, looking for a half-day finally think of binary flag status here, maps tagged with 0 0 will be more comfortable. It was right to mend it. The topic is very interesting. AC Code:/*
#include "algorithm" #include "iostream" #include "CString" #include "Cstdlib" #include "string" #include "Cstdio" # Include "vector" #include "cmath" #include "queue" using namespace std;typedef long long LL; #define MEMSET (x, y) memset (x, Y, sizeof (x)) #define MEMCPY (x, y) memcpy (x,y,sizeof) #define MX 401#define INF 0x3f3f3f3fint maps[20][20];int dp[200000] [20],vis[20];void init () {memset (maps,0x3f); memset (vis,0x3f); memset (DP, 0x3f);} int main () {int t;scanf ("%d", &t), while (t--) {init (); Int. n,m,u,v,w;scanf ("%d%d", &n,&m); for (int i=0; i<m ; i++) {scanf ("%d%d%d", &u,&v,&w), if (Maps[--u][--v] > W)//state compression starting from 0 would be good to write some maps[v][u]=maps[u][v]= W;} for (int i=0; i<n; i++) maps[i][i]=0; Mark yourself to a distance of 0//The back will be added to this number. for (int k=0, k<n; k++) {for (int i=0, i<n; i++) {for (int j=0; j<n; J + +) {maps[i][j]=min (maps[i][j],maps[i][k]+maps K [j]); Floyd calculates the minimum distance to a certain point}}}dp[1][0]=0;vis[0]=0;m=1<<n;for (int i=1; i<m; i++) {for (int j=0; j<n; J + +) {if (dp[i ][j]==inf) continue;for (int k=0; k<n; k++) {if (i& (1<<k) | |  Maps[j][k]==inf) continue; Binary 1 means that the point has passed, 0 means not traversed, and the second dimension represents the point where it is now. Compressed state if (dp[i| ( 1<<k) (][k]>dp[i][j]+maps[j][k]) {dp[i| ( 1<<k)][k]=dp[i][j]+maps[j][k];vis[k]=min (vis[k],dp[i| ( 1<<k)][k]); To compare, go to the next point required by the minimum road}}}}int minn=1e9+100; for (int i=0; i<n; i++) {minn=min (minn,dp[m-1][i]+vis[i]);} printf ("%d\n", Minn);} return 0;}

  

Acm:hdu 5418 Victor and World-floyd algorithm +DP State compression

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.