HDU 3001 travelling (shaped pressure dp,3)

Source: Internet
Author: User

Test instructions

The n<=10 point is given, and there is an no-direction graph with M-edge. Q: Can you start at any point and go through the same point 2 times and traverse the minimum cost of all points?

Ideas:

The main thing is to card your memory, because at most can go through the same point 2 times, so can only be used 3 binary to represent, 3 binary can first hit the table out. In the walk when the attention can only walk 2 times, the other and ordinary TSP pressure dp is the same. Note: Heavy side, self-ring and so on, old terrier.

1 //#include <bits/stdc++.h>2#include <iostream>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <map>7#include <algorithm>8#include <vector>9#include <iostream>Ten #definePII pair<int,int> One #defineINF 0x3f3f3f3f A #defineLL Long Long - #defineULL unsigned long Long - using namespacestd; the Const DoublePI = ACOs (-1.0); - Const intn= One; - intG[n][n], dp[60000][n], up[n], bit[n]; -  + intDecodeintS//decoding the state s - { +     intCnt=0; A      for(intI=1; S i++) at     { -bit[i]=s%3; -S/=3; -         if(Bit[i]) cnt++; -     } -     returnCNT; in } -  to intCalintN) + { -Memset (DP,0x3f,sizeof(DP)); the     intans=INF; *      for(intI=1; i<=n; i++) dp[up[i-1]][i]=0;//set each starting point to 0 without affecting the result $      for(ints=1; s<=up[n]; s++)Panax Notoginseng     { -         intCnt=decode (s);//CNT indicates the number of points that have been traversed the          for(intI=1; i<=n; i++)//Enumerate intermediate points +         { A             if(bit[i]>0)//Make sure you've traversed the I point the             { +                  for(intj=1; j<=n; J + +)//Enumerate Endpoints -                 { $                     if(bit[j]==2)Continue;//I 've been through it 2 times. $                     BOOLFlag=cnt==n?true:false;//in order to update the answer -                     if(bit[j]==0&&cnt+1==n) flag=true; -                     int&q=dp[s+up[j-1]][j]; the                     if(Q>dp[s][i]+g[i][j]) q=dp[s][i]+G[i][j]; -                     if(flag) ans=min (ans, q);Wuyi                 } the             } -         } Wu     } -     returnans; About } $  - voidInit ()//3 binary first hit the table - { -     intA=3; Aup[0]=1; +      for(intI=1; i<n; i++) the     { -up[i]=A; $a*=3; the     } the } the  the  - intMain () in { the     //freopen ("Input.txt", "R", stdin); the init (); About     intN, M, A, B, C; the      while(~SCANF ("%d%d", &n,&m))//up to 2 times per point the     { theMemset (G,0x3f,sizeof(g)); +          for(intI=1; i<=n; i++) g[i][i]=0; -          for(intI=0; i<m; i++) the         {Bayiscanf"%d%d%d",&a,&b,&c); theg[a][b]=g[b][a]=min (g[a][b],c); the         } -         if(n==1) -         { thePuts"0"); the             Continue; the         } the         intans=cal (n); -printf"%d\n", ans==inf?-1: ans); the     } the     return 0; the}
AC Code

HDU 3001 travelling (shaped pressure dp,3)

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.