HDU 4568 Hunter (TSP + state compression)

Source: Internet
Author: User
Hunter

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 529 accepted submission (s): 153

Problem description one day, a hunter named James went to a mysterious area to find the treasures. James wanted to research the area and brought all treasures that he cocould.
The area can be represented as a n * m rectangle. any points of the rectangle is a number means the cost of research it,-1 means James can't cross it, James can start at any place out of the rectangle, and need E point next by next. he will move in the rectangle
And bring out all treasures he can take. of course, he will end at any border to go out of rectangle (James will research every point at anytime he cross because he can't remember whether the point is researched or not ).
Now give you a map of the area, you must calculate the least cost that James bring out all treasures he can take (one point up to only one treasure ). also, if nothing James can get, please output 0.

Input the input consists of T test cases. the number of test cases T is given in the first line of the input. each test case begins with a line containing 2 integers n m, (1 <= n, m <= 200), that represents the rectangle. each of the following
N lines contains M numbers (0 ~ 9), represent the cost of each point. next is K (1 <= k <= 13), and next K lines, each line contains 2 integers x y means the position of the treasures, X means row and start from 0, Y means column start from 0 too.

Output for each test case, You shocould output only a number means the minimum cost.

Sample Input

23 33 2 35 4 31 4 211 13 33 2 35 4 31 4 221 12 2

Sample output

811

Source2013
ACM-ICPC Changsha Division national invitational Competition -- reproduction of questions

Recommendzhoujiaqi2010Question:There is a maze with treasure in some grids. Each grid has a value, which indicates the cost of accessing the grid. If it is-1, it indicatesCannot be accessed. It can be obtained and output from any grid. Ask the minimum cost of bringing out all the treasures. If he cannot get anything,Output-1.Each grid can be accessed for any number of times, because he does not remember whether he has walked through.
Analysis:It is to use the relaxation operation to find out the minimum cost between all the treasures and the minimum price to get the boundary from each treasure.Then, use dynamic planning to update the status and find the specified status.
Thoughts:Although I typed it out, I still had a vague understanding of the update process of DIS [u ....Although this question is quite similar to that of poj 3229, it is hard to understand. Because there is the shortest distance between the treasures and the boundary.
Code:

# Include <cstdio> # include <iostream> # include <algorithm> # include <cstring> # include <queue> # define INF 1e9using namespace STD; struct node {int ID, DIS; node () {} node (int A, int B) {id = A, Dis = B;} bool operator <(const node & DD) const {return dis> DD. dis ;}} T1; int map [230] [230], G [32] [32], DP [20] [1 <16]; int DX [4] = {-,}; int dy [4] = {,-}; int node [32], sum, tre [232] [232]; int n, m, cos [32], vis [210*210], DIS [2 10*210]; void dij (INT St, int POS) {int I; memset (VIS, 0, sizeof (VIS); priority_queue <node> q; for (I = 0; I <sum; I ++) dis [I] = inf; DIS [st] = 0; q. push (node (St, 0); node T1; while (! Q. empty () {T1 = Q. top (); q. pop (); int u = t1.id; If (vis [u]) continue; vis [u] = 1; int x = u/m, y = u % m; if (Tre [x] [Y]! =-1) // if it is another treasure, update the distance from the current treasure to this treasure. g [POS] [tre [x] [Y] = dis [u]; if (x = n-1 | x = 0 | Y = S-1 | Y = 0) // if it is a boundary, update the distance from the treasure to the boundary {cos [POS] = min (cos [POS], DIS [u]);} For (INT I = 0; I <4; I ++) {int xx = x + dx [I]; int YY = Y + dy [I]; If (Map [XX] [YY] =-1) continue; If (XX <0 | XX> = n | YY <0 | YY> = m) continue; int UU = XX * m + YY; if (DIS [UU]> dis [u] + map [XX] [YY]) {dis [UU] = dis [u] + map [XX] [YY]; q. push (node (Uu, DIS [UU]) ;}}} int main () {int T, I, J, K, P, ANS, X, Y; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M ); sum = N * m; memset (Tre,-1, sizeof (Tre); memset (DP, 0x3f, sizeof (DP); for (I = 0; I <n; I ++) {for (j = 0; j <m; j ++) scanf ("% d ", & map [I] [J]);} scanf ("% d", & K); for (I = 0; I <K; I ++) {scanf ("% d", & X, & Y); tre [x] [Y] = I; node [I] = x * m + Y ;} memset (G, 0x3f, sizeof (g); for (I = 0; I <K; I ++) {G [I] [I] = 0; cos [I] = inf; dij (node [I], I) ;}for (I = 0; I <K; I ++) {int X1 = node [I]/m; int y 1 = node [I] % m; DP [I] [1 <I] = map [X1] [Y1] + cos [I]; // The cost of directly accessing the I point and exiting the boundary} For (j = 0; j <(1 <k); j ++) {for (I = 0; I <K; I ++) {If (J & (1 <I) & J! = (1 <I) {for (P = 0; P <K; P ++) {If (J & (1 <p) & J! = (1 <p) & I! = P) {DP [I] [J] = min (DP [I] [J], DP [p] [J-(1 <I)] + G [p] [I]) ;}}} int ans = inf; for (I = 0; I <K; I ++) {ans = min (ANS, DP [I] [(1 <k)-1] + cos [I]);} printf ("% d \ n ", ans);} return 0 ;} // AC // 140 ms/* tre [] [] record treasure No. G [] [] record the distance between two treasure cos [I] record the boundary from the I treasure cost node [I] records the relative position of the treasure to the entire matrix */

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.