Hdu 4568 Hunter "spfa shortest way tsp State compression"

Source: Internet
Author: User
Tags cas

Description one day, a hunter named James went to a mysterious area to find the treasures. James wanted to the area and brought all treasures that he could.
The area can be represented as a n*m rectangle. Any points of the rectangle are a number means the cost of the I it,-1 means James can ' t cross it, James can start at an Y place out of the rectangle, and explore point next by next. He'll move in the rectangle and bring out all treasures he can take. Of course, he'll end at any border to go out of rectangle (James'll, every point at anytime he cross because he Can ' t remember whether the point is researched or not).
Now give your a map of the area, you must calculate the least cost that James bring out all treasures he can take 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 on the first line of the input. Each test case is begins with a line containing 2 integers N M, (1<=n,m<=200), which 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 should output is a number means the minimum cost.

Sample Input

2 3 3 3 2 3 5 4 3 1 4 2 1 1 1 3 3 3 2 3 5 4 3 1 4 2 2 1 1 2 2

Sample Output

8 11

It's too much trouble. = = especially the TSP did not learn the case where Dp[i][j]: Starting from J, through the state I on the shortest distance of all points

    #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using n
    Amespace std;
    #define MAX 205 #define INF 99999999 int n,m; struct pos{int x, y;}
    Tr_pos[max];
    int Map[max * Max],tr,val[14],dis[max * MAX];
    int dir[4][2]={0,1,0,-1,-1,0,1,0};
    int g[20][20],dp[1<<14][14];
        int get_id (int x,int y) {if (x>=0 && x<n && y>=0 && y<m) return x*m+y;
    return n*m; } void Spfa (int x) {for (int i = 0; I <= n*m; i++) dis[i] = Inf;//dis array is the shortest distance to the outside of the current treasure's location, each
        Initialize dis[x] = (map[x]==-1?inf:map[x]);
        BOOL inq[205*205];
        memset (inq,0,sizeof (INQ));
        queue<int>q; while (!
        Q.empty ()) Q.pop ();
        Q.push (x);
        INQ[X] = 1;
        int now; while (!
            Q.empty ()) {now = Q.front ();
            Q.pop ();
            Inq[now] = 0;
            if (now = = n*m) continue; INT r = now/m;
            int c = now%m;
            int Nr,nc,nid;
                for (int i = 0; i < 4; i++) {nr = R + dir[i][0];
                NC = c + dir[i][1];
                Nid = get_id (NR,NC);
                if (map[nid] = =-1) continue;
                Updates the shortest distance from the treasure point to the boundary.
                    if (Dis[nid] > Dis[now] + map[nid]) {Dis[nid] = Dis[now] + Map[nid];
                    if (!inq[nid]) Q.push (NID);
                Inq[nid] = 1; }}}} int do () {for (int i = 0; i < tr; i++) {SPFA (get_id (
            TR_POS[I].X,TR_POS[I].Y));
                for (int j = 0; J < tr; J + +) {int jid = get_id (TR_POS[J].X,TR_POS[J].Y);
                G[I][J] = Dis[jid];
            if (g[i][j] = = inf) return 0;
        } G[I][TR] = dis[n*m];//The n*m point as a treasure point that costs 0.
    } return 1; } int Solve () {if (tr = = 0) return 0;
        int full = 1<<tr;
        for (int i = 0;i<full;i++) for (int j=0;j<tr;j++) dp[i][j] = inf;
        for (int i = 0; i < tr; i++) dp[1<<i][i] = g[i][tr]; for (int i = 1, i < full; ++i) {//The current state is the I state (in binary notation which treasure points were passed) for (int j = 0; j < tr; ++j) {if (i
        & (1<<j)) {//Ensure that the first J Treasure point is in the I state. for (int k = 0; k < tr; ++k) {if (I & (1<<k) && J! = k) {//k also to be in, and I. = J if (dp[i^ (1<<k)][j]! = INF) dp[i][k] = min (Dp[i][k], dp[i^ (1<<k)][j] + g[j
            ][K]-val[j]);
        }}}}} int res = INF;
            for (int i = 0; i < tr; i++) {res = min (Res,dp[full-1][i] + g[i][tr]-val[i]);
    FULL-1 represents the state that all treasure points contain, g[][] represents the shortest distance between two treasure points, because Val of I is recorded two times, so minus} return res; } int main () {inT cas,i,j;
        scanf ("%d", &cas);
            while (cas--) {scanf ("%d%d", &n,&m);
            for (i = 0;i < n;i++) for (j = 0;j < m;j++) scanf ("%d", &map[i*m+j]);
            scanf ("%d", &tr);
                for (i = 0;i < tr;i++) {scanf ("%d%d", &tr_pos[i].x,&tr_pos[i].y);
                int id = get_id (TR_POS[I].X,TR_POS[I].Y);
            Val[i] = Map[id];

            } Map[n*m] = 0;//enters from anywhere outside.
            if (done ()) printf ("%d\n", Solve ());
        else printf (" -1\n");
    } return 0;
 }


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.