Poj 3311 HIE with the pie [traveling salesman + origin retrieval]

Source: Internet
Author: User

Title: poj 3311 HIE with the pie


The question is: the batch worker will send the batch, and then give you the distance and direction of each point, and then let you go back to the source of the shortest.


Analysis: Because the dense graph is provided, it is necessary to handle the shortest path, Floyd

Then, the TSP is good.

Enumerate each status. For each vertices in the current status that have passed, enumeration comes from that point, with the shortest path updated.

Status: DP [st] [I]: the shortest path to vertex I in the St state

Transition equation: DP [st] [I] = min (DP [st &~ (1 <I)] [J] + MP [J] [I], DP [st] [I]);

Then, add the back distance. Note that we need to update and. Here we wa it once.


AC code:

# Include <cstdio> # include <algorithm> # include <cstring> # include <string> # include <iostream> # include <vector> using namespace STD; const int INF = 0x3f3f3f3f; const int n = 12; int MP [N] [N]; int N; int DP [1 <n] [N]; int main () {// freopen ("input.txt", "r", stdin); While (~ Scanf ("% d", & N) {n ++; For (INT I = 0; I <n; I ++) {for (Int J = 0; j <n; j ++) scanf ("% d", & MP [I] [J]);} for (int K = 0; k <n; k ++) {for (INT I = 0; I <n; I ++) {for (Int J = 0; j <n; j ++) MP [I] [J] = min (MP [I] [J], MP [I] [k] + MP [k] [J]) ;}}for (INT ST = 0; ST <(1 <n); ST ++) {for (INT I = 0; I <n; I ++) {If (st & (1 <I) = 0) // 0 continue; if (ST = (1 <I) {DP [st] [I] = MP [0] [I]; continue ;} DP [st] [I] = inf; For (Int J = 0; j <n; j ++) {If (st & (1 <j ))&& I! = J) // 1 {DP [st] [I] = min (DP [st &~ (1 <I)] [J] + MP [J] [I], DP [st] [I]) ;}}} int ans = inf; for (INT I = 0; I <n; I ++) {ans = min (ANS, DP [(1 <n) -1] [I] + MP [I] [0]);} printf ("% d \ n", ANS);} return 0 ;}


Poj 3311 HIE with the pie [traveling salesman + origin retrieval]

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.