POJ 3311-Shape pressure DP

Source: Internet
Author: User

Classic TSP variants


Learn: 1, Floyd O (n^3) processing random two points of the shortest

2, the collection of bits, I will in the final summary of the written out. Note the state of the bits must be designed before writing the code. In the subject, the No. 0 to the nth place represents the first city, 1 is already traversed, 0 did not walk

So DP equation: dp[s][i]--At present in the city I. The state is S (s) stored by those cities that have traveled


3, the final request to form a circuit, then is min (dp[1<< (n+1) -1][i],dp[0][i])


#include <cstdio> #include <cstring> #include <algorithm> #include <string> #include < iostream> #include <cmath> #include <map> #include <queue>using namespace std; #define LS (RT) rt*2# Define RS (RT) Rt*2+1#define ll Long Long#define Rep (i,s,e) for (int. i=s;i<e;i++) #define REPE (i,s,e) for (int i=s;i<=e ; i++) #define CL (A, B) memset (A,b,sizeof (a)) #define in (s) freopen (S, "R", stdin) const int MAXN = 12;int Dis[maxn][maxn];int dp[1<<maxn][maxn];const int INF = 1e9+10;int n;void Floyd () {Rep (k,0,n+1) Rep (i,0,n+1) Rep (j,0, n+1) Dis[i][j]=min (Dis[i][k]+dis[k][j],dis[i][j]);}    int main () {//in ("poj3311.txt");    int Len; while (~SCANF ("%d", &n) && N) {rep (i,0,n+1) Rep (j,0,n+1) Dp[i][j]=dis[i][j]        =inf;                Rep (i,0,n+1) Rep (j,0,n+1) {scanf ("%d", &len);            Dis[i][j]=min (Dis[i][j],len); } Floyd ();Find out the random distance of two points int s=1<< (n+1);            Rep (i,0,s) Rep (j,0,n+1) {dp[i][j]=inf;                    } for (int s=0;s<s;s++)//Enumerate All state Rep (i,0,n+1) {if (s& (1<< (i)))                        {if (s== (1<<i)) dp[s][i]=dis[0][i];                            Else Rep (j,0,n+1) if (s& (1<<j) && i!=j)                            {Dp[s][i]=min (dp[s^ (1<<i)][j]+dis[j][i],dp[s][i]);        }}} int ans=inf;        for (int i=0;i<n+1;i++) ans=min (ans,dp[(S-1)][i]+dis[i][0]);    printf ("%d\n", ans); } return 0;}


POJ 3311-Shape 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.