HDU 5418 Victor and World (shortest Hamiltonian circuit)

Source: Internet
Author: User

Give you n countries, M route: the distance between U_i and V_i w_i.

The output departs from country 1th at least once per country and back to country number 1th at the shortest distance.

"Official":

We first need to preprocess the shortest distance between any two countries, because the data range is very small, so the direct use of Floyd on the line.

After that, we use f[s][i] to indicate that the state of access to the country is S, the last country currently visited is the minimum total oil required for I, wherein the binary representation of S is recorded in the state of the country of access, s in the binary representation of the bit I (whether from left to right or from right to left). If 1 indicates that the first country has been visited, it would indicate that the first country has not been visited.

such as dp[13][3] (13=1101 means that the existing city 1, 3, 4) represents the shortest path from City 1 to City 3 (possibly through City 4).

Then the state transfer equation:

f[s| ( 1<<i)][i]=min (F[s][j]+f[i][j]),
wherein, s This state does not contain I city but contains J City. i.e. I and J meet s& (1<<J) =1 and s& (1<<i) = 0.

At first, except for the f[1][1] is 0, other cases are infinite, after the first enumeration of S, and then enumerate I (When I test the problem because of the anti-result WA), then the final answer is

Min (f[(1<<n) -1][i]+f[i][1]), where I∈[2,n].

Total complexity of O (n^3+n^2*2^n)

#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include < string> #include <stack> #include <queue> #include <set> #include <map>typedef long long ll; using namespace std;const int inf=0x3f3f3f3f;const int maxn=1e6+10;int m,n;int g[20][20];int dp[maxn][20];void Floyd () { for (int k=1;k<=n;++k) {for (int. i=1;i<=n;++i) {for (int j=1;j<=n;++j) {g[i][j]=min (g[i][j],g[i][k]+g[k][j]);}}}} int main () {int t;scanf ("%d", &t), while (t--) {memset (g,inf,sizeof g); scanf ("%d%d", &n,&m); int U,v,w;for ( int i=0;i<m;i++) {scanf ("%d%d%d", &u,&v,&w), if (G[u][v] > W) {g[u][v]=g[v][u]=w;}}              if (n = = 1) {cout<<0<<endl;          Continue } Floyd (); for (int i=1;i<=n;++i) G[i][i]=0;memset (dp,inf,sizeof dp);DP [1][1]=0;for (int s=1;s< (1<<n); ++s) {for (int i=1;i<=n;++i) {if (s& (1<< (i-1))) {//s contains IFOR (int j=1;j<=n;++j) {if (s& (1<< (j-1))) ==0) {//s not covered inContaining Jint tt= (s | (1<< (j-1))); /tt for S contains J's state dp[tt][j]=min (Dp[tt][j],dp[s][i]+g[i][j]);}}}}        printf ("%d\n", g[n][1]); int ans=inf;        for (int i=2;i<=n;i++) ans=min (ans,dp[(1<<n)-1][i]+g[i][1]); printf ("%d\n", ans);}}

  

HDU 5418 Victor and World (shortest Hamiltonian circuit)

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.