Hdu 3001 travelling TSP deformed three-step pressure DP

Source: Internet
Author: User

Hdu 3001 tsp problem deformation//this time to each point up to two times, so you can use the three-way analogy//dp[s][u] to indicate that the current U-point access State is the minimum cost//using the Brush table method, that is, using the current state to launch the state it can transfer//dp[s][u] The available status is Dp[s+state[v]][v] (dist[u][v]!=inf)//DP[S+STATE[V]][V] = max (dp[s+state[v]][v],dp[s][u]+dist[u][v]);// Where each point accesses up to 2 times//The skill is to represent all States first visit[s][u];//represents the current access state of U when the U-point access State is S. Which we ask is the last point has visited the state of the dp[s][v]{v<n};////this question began to completely want to set up a bare tsp template, the results of the sample are not over//Ah, found himself has been very water, continue to refuel it ... And, made a very serious mistake, resulting in a tune-up of 2 hours of bug//ay ... Come on, but eventually it's AC.//const int maxn = 15;//int dp[1<<maxn][maxn];//int mp[maxn][maxn];//int n,m;//void init () {//int A, B, C;//memset (Mp,0,sizeof (MP)),//memset (Dp,0x3f,sizeof (DP)),//for (int i=0;i<m;i++) {//scanf ("%d%d%d", &a, &AMP;B,&AMP;C);//mp[a][b] = Mp[b][a] = c;//}//for (int i=0;i< (1<<n); i++)//dp[i][0] = mp[0][i];//}////void Print () {//for (int i=0;i< (1<<n); i++) {//for (int j=0;j<n;j++)//printf ("%d", dp[i][j]);//puts ("");//}//}/ void Solve () {//for (int s=0; s< (1<<n); s++) {//for (int u=1;u<=n;u++)//for (int v=1;v<=n;v++) {//if ((s>>v) &1) {//dp[s][u] = min (dp[s][u],dp[s^ (1<<v)][v]+mp[v][u]);//}//}//}//print ();//int ans = 0;//for (int i=0;i<n;i++ //ans = Max (ans,dp[(1<<n) -1][i])//printf ("%d\n", ans);//}////int main () {//Freopen ("G:\\code\\1.txt", "R", stdin);//while (scanf ("%d%d", &n,&m)!=eof) {//init ();//solve ();//}//return 0;//} #include <algorithm># Include <bitset> #include <cassert> #include <cctype> #include <cfloat> #include <climits > #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include < cstring> #include <ctime> #include <deque> #include <functional> #include <iostream># Include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <vector> #define CEIL (a B) (((a) + (b)-1)/(b)) #define Endl ' \ n ' #define GCD __gcd#define Highbit (x) (1ull<< (63-__builtin_clzll (x))) #define Popcount __builtin_pOpcountlltypedef Long long ll;using namespace Std;const int MOD = 1000000007;const long Double PI = ACOs ( -1.L); template&lt Class t> inline T LCM (const t& A, const t& b) {return A/GCD (A, b) *b;} Template<class t> Inline T lowbit (const t& x) {return x&-x;} Template<class t> inline T maximize (t& A, const t& b) {return a=a<b?b:a;} Template<class t> inline T Minimize (t& A, const t& b) {return a=a<b?a:b;} const int MAXN = 12;const int maxm = 60000;int visit[maxm][maxn];int dp[maxm][maxn];int state[maxn];int Mp[maxn][maxn];con St int inf = 0x3f3f3f3f;int n,m;void print () {for (int. i=0;i<n;i++) {for (int j=0;j<n;j++) printf ("%d", Mp[i][j]);p UTS ("");}} void Init () {memset (Mp,inf,sizeof (MP)), Memset (Dp,inf,sizeof (DP)), for (int i=0;i<n;i++) {Dp[state[i]][i] = 0;} int a,b,c;for (int i=0;i<m;i++) {scanf ("%d%d%d", &a,&b,&c); a--, b--;mp[a][b] = mp[b][a] = min (mp[a][b],c) ;} for (int i=0;i<n;i++)//mp[i][i] = 0;//print ();} void Solve () {int ans = inf;for (int s=0; s<state[n]; s++) {BOOL flag = false;for (int u=0;u<n;u++) {if (visit[s][u] = = 0) flag = true;if (Dp[s][u] = = INF) continue;for (int v=0 ; v<n;v++) if (v!=u) {if (visit[s][v]>=2) continue;if (mp[u][v] = = inf) continue;dp[s+state[v]][v] = min (dp[s+state[ V]][V],DP[S][U]+MP[U][V]);}} if (!flag) {for (int u=0;u<n;u++) ans = min (ans,dp[s][u]);}} if (ans = = inf) ans = -1;printf ("%d\n", ans);} void Get () {state[0]=1;for (int i=1;i<=10;i++) state[i] = state[i-1] * 3;for (int s=0; s<=state[10]; s++) {int x = s;for (int i=0;i<=10;i++) {Visit[s][i] = x%3;x/=3;}}} int main () {//freopen ("G:\\code\\1.txt", "R", stdin), get (), while (scanf ("%d%d", &n,&m)!=eof) {init (); Solve ();} return 0;}

Hdu 3001 travelling TSP deformed three-step pressure 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.