"Algorithmic Learning Notes" 62. State compression DP SJTU OJ 1088 postman small F

Source: Internet
Author: User

State compression, when our state too much can be considered with bit to store, binary to represent the set, with & to take the intersection, with ^ to different or.

The DP process is very simple, traversing all cases to take the shortest path, because the shortest Hamiltonian loop itself is an NPC problem, the efficiency is not high.

#include <vector>#include<iostream>using namespacestd;//NP-Complete problem of the shortest Hamiltonian loop ... intmap[ -][ -]={0};intn=0;Const intinf=768000;//3000*16*16intf[1<< -][ -]={0};//F[i][j] represents the shortest path length from the start point through all the points in I to JvoidInitialize () {cin>>N;  for(inti =0; I < n; ++i) {         for(intj=0; J < N; ++j) {cin>>map[i][j];//input Data        }    }} intbuild () {//The starting point is always 1 in the list, so don't think about the starting point.//DP state compression such as n=4 eg:1<<3---1000 from 0000 to 1000 determines all cases in the previous 3 locations     for(inti =0; I < (1<< (n1) ); ++i)//    {        //Note that the point labeled J in the input is the subscript for the j-1 abbreviation J point in I//DP calculation F[i][j]         for(intj =1; J < N; ++j) {            if(i = = (1<< (J-1) ))//If I had just a J-point at this point,F[I][J] = map[0][J];//direct from start to J            Else{//Otherwise you need DP                if(I & (1<< (J-1) ))//J points in I{F[i][j]= INF;//start looking for its minimum value                     for(intK =1;  K < n; ++k)if(K!=j and (I & (1<< (K-1) ) ) )                            //Find K K not J and in I//The state transition equation is actually just a relaxation operation. EdgeF[i][j] = min (f[i^ (1<< (J-1))][k] +Map[k][j], f[i][j]); }            }        }    }     intres =INF;  for(inti =1; I < n; ++i) {res= Min (res, f[(1<< (n1))-1][i] + map[i][0]); }    returnRes;}intMainintargcChar Const*argv[])    {Initialize (); if(n==1) cout<<2*map[0][0]<<Endl; Elsecout<<build () <<Endl; return 0;}

"Algorithmic Learning Notes" 62. State compression DP SJTU OJ 1088 postman small F

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.