HDU 5418 Victor and world (state compression DP)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5418

The main topic: a connected graph (n <16, m<100000) consisting of n nodes M-bar (with Benquan). From the first point, after each point at least once back to the origin of the minimum path edge right and.

Analysis: Found that I was really food.

N<16, it is obvious that the status of the compression tag, first the number of all points minus 1, so that it starts numbering from 0. DP[I][J] means starting from point No. 0, the current state is I (Bits 1 indicates that the corresponding point has traversed, otherwise did not pass), the current position is J, back to the origin of the minimum cost, then dp[(1<<n) -1][0] for the solution, with a similar SPFA method can be updated to all situations.

Reference code:

#include <cstdio>#include<cstring>#include<queue>#include<algorithm>using namespacestd;intdp[1<< -][ -];intmp[ -][ -];intdis[ -];BOOLvis[1<< -][ -];intN;intMain () {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;  for(inti =0; I < m; i++) {            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)); memset (Vis,0,sizeof(VIS)); dp[0][0] =0, vis[0][0] =1; Queue<pair<int,int> >Q; Q.push (Make_pair (0,0));  while(!Q.empty ()) {            ints =Q.front (). First; intU =Q.front (). Second;            Q.pop ();  for(inti =0; I < n; i++)            {                intSS = S | (1<<i); if(Dp[ss][i] > Dp[s][u] +Mp[u][i]) {Dp[ss][i]= Dp[s][u] +Mp[u][i]; if(Vis[ss][i] = =0) {Vis[ss][i]=1;                    Q.push (Make_pair (SS, I)); }                }            }        }        //for (int i = 0; l < (1<<n); i++) for (int j = 0; J < N; j + +) printf ("%d%d:%d\n", I, J, Dp[i][j]);printf"%d\n", dp[(1<<n)-1][0]); }    return 0;}

HDU 5418 Victor and world (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.