codevs2800 Delivery of Takeaway

Source: Internet
Author: User

  • Title Description Description
    There was a delivery, he had n orders on his hand, and he had to deliver n parts to the hands of n different clients. N different customers are in 1~n numbered cities. Delivery from the city of No. 0, and then N cities to walk once (a city can go many times), and finally back to 0 points (his unit), ask the shortest time is how much. The time for direct access to any two cities is now known.

  • Enter a description input Description
    First line a positive integer n (1<=n<=15)
    Next is a (n+1) * (n+1) matrix in which the number in the matrix is a positive integer that does not exceed 10000. The I row j column of the Matrix represents the time of the direct access between City I-1 and City j-1. Of course, the direct access time of city A to City B and City B to city A is not necessarily the same, that is, the road is one-way.

  • outputs description output Description
    A positive integer representing the minimum time spent

  • sample input to sample
    3
    0 1 10 10
    1 0 1 2
    10 1 0 10
    10 2 10 0

  • sample output Sample outputs
    8

  • Data Size & Hint
    1<=n<=15

  • Solving
    A see can go many times first want to use Floyd to find the shortest way, N only 15~
    Below is the pressure DP. (For the first time, it's not easy to think about all the states and to design a correct transfer equation.)
    We make F[i][lst] show the first state when I went to the first LST city to spend the minimum cost, wherein I converted to binary m, with an array of p[] records M 1 position, then I state is p[]-1 all cities have traversed and other cities have not traversed the state, so lst+ The position of 1 in M is 1 o'clock, the boundary condition F[0][0]=0, the remainder is the INF, and the final state is f[(1 << (n+1) -1][0].

  • Code

#include <cstdio>#include <algorithm>#include <cstring>using namespace STD;Const intMAXN = -, oo =1000000000;int Map[MAXN] [MAXN], f[1<< MAXN][MAXN], N;inline voidInit () {scanf("%d", &n); for(inti =0; I <= N; ++i) { for(intj =0; J <= N; ++J) {scanf("%d", &MapI        [j]); }    }//Below is Floyd     for(intK =0; K <= N; ++K) { for(inti =0; I <= N; ++i) { for(intj =0; J <= N; ++J) {MapI [j] = min (MapI [j],MapI [K] +MapK            [j]); }        }    }}inline voidWork () {memset(F,127/3,sizeof(f)); f[0][0] =0; for(inti =0; I < (1<< (n +1)); ++i) { for(intLST =0; LST <= N; ++LST) {if((I | (1<< lst)) = = i)//Last to the city in the city that has been visited, the state is legal{ for(intPre =0; Pre <= N; ++pre) {F[i][lst] = min (F[i][lst], F[i-(1<< lst)][pre] +Map[Pre] [LST]);//has not been to LST before, now through the pre to LSTF[i][lst] = min (F[i][lst], F[i][pre] +Map[Pre] [LST]);//Before the LST, now through the pre and to the LST}            }        }    }printf("%d", f[(1<< (n +1)) -1][0]);}intMain () {init (); Work ();return 0;}

codevs2800 Delivery of Takeaway

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.