HDU-3001 travelling (state compression)

Source: Internet
Author: User

A person wants to travel, he wants to go all over the city, but the same city does not want to stroll more than 2 times. Now given the traffic tolls between cities, he can choose any point as the starting point. What is the minimum fare for all the cities?

Problem solving: This problem is similar to POJ-3311 Hie with the pie
The status mark here is represented by a three-digit number, which indicates the number of times each city has been gone.
Set Dp[i][state] for the current city in I, stroll the city state for the minimum toll
State transition equation:
Dp[i][s + I city mark] = min (dp[i][s + i city Mark], Dp[j][s] + cost[j][i])
Pay attention to the side of the sentence
I used the queue again ...

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace STD;#define MAXN#define INF 0x3f3f3f3f#define MAXS 60000structDP {intnum, State;} START[MAXN];intN, M;intCOST[MAXN][MAXN];intDP[MAXN][MAXS];intMOD[MAXN];voidInit () {intX, y, Z;memset(Cost,0x3f,sizeof(cost)); for(inti =0; I < m; i++) {scanf("%d%d%d", &x, &y, &z); cost[x-1][y-1] = cost[y-1][x-1] = min (cost[x-1][y-1], z); }memset(DP,0x3f,sizeof(DP)); for(inti =0; I < n; i++) {Dp[i][mod[i]] =0;        Start[i].num = i;    Start[i].state = Mod[i]; }}voidSolve () { queue<DP>Q for(inti =0; I < n; i++) Q.push (Start[i]);intans = INF; while(!q.empty ())        {DP T = Q.front (); Q.pop (); for(inti =0; I < n; i++) {if(i = = T.num | | cost[i][t.num] = = INF)Continue;if(!i && t.state%3==2)Continue;if(I && (t.state% mod[i+1])-(t.state% mod[i]))/mod[i] = =2)Continue;if(Dp[i][t.state + mod[i]] > Dp[t.num][t.state] + cost[t.num][i])                {dp[i][t.state + mod[i]] = Dp[t.num][t.state] + cost[t.num][i];                DP tt;                Tt.num = i;                Tt.state = T.state + mod[i]; Q.push (TT);BOOLFlag =false;intTMP = T.state + mod[i];if(Dp[i][tmp] < ans) { for(intj =0; J < N; J + +)if(tmp%3==0) {flag =true; Break; }Else{tmp/=3; }if(!flag) ans = dp[i][t.state + mod[i]]; }            }        }    }if(ans = = INF) ans =-1;printf("%d\n", ans);}voidBegin () {mod[0] =1; for(inti =1; I < A; i++) Mod[i] = mod[i-1] *3;}intMain () {begin (); while(scanf("%d%d", &n, &m)! = EOF) {init ();    Solve (); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU-3001 travelling (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.