HDU 4568 SPFA Shortest path algorithm + travel quotient problem

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=4568

Problem 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
23 33 2 35 4 31 4 211 13 33 2 35 4 31 4 221 12 2

Sample Output
811
/**hdu 4568 SPFA Shortest path algorithm + travel business problem topic: Given a n*m chessboard, each lattice has a value, representing the cost of the lattice, given the sum of the points of the treasure point coordinates, from any side of the board to enter the board, after all the treasure point in the need to get out of the board Minimum cost: The shortest distance between any two treasure points in the SPFA processing place (minimum cost) and the shortest distance from each treasure point and boundary. Then state compression: dp[s][i] means the state of the treasure point is S and the end point is the minimum cost of I */#include <stdio.h> #include <string.h> #include <algorithm > #include <iostream> #include <queue>using namespace std;const int maxn=205;int n,m;struct note{int x, y; } point[15];int dx[4][2]= {1,0,0,1,-1,0,0,-1};int Dis[maxn][maxn],a[maxn][maxn],dis_border[25],length[20][20];bool            Vis[maxn][maxn];int dp[1<<15][15];void SPFA (int s) {for (Int. i=0;i<n;i++) for (int j=0;j<m;j++)    dis[i][j]=0x3f3f3f3f;    memset (vis,0,sizeof (VIS));    Queue<pair<int,int> >q;    Q.push (Make_pair (POINT[S].X,POINT[S].Y));    Vis[point[s].x][point[s].y]=1;    dis[point[s].x][point[s].y]=0;        while (!q.empty ()) {int X=q.front (). First;        int Y=q.front (). Second;        Q.pop ();        vis[x][y]=0; if (x==0| | x==n-1| | y==0| |        y==m-1) Dis_border[s]=min (Dis_border[s],dis[x][y]);            for (int i=0; i<4; i++) {int xx=x+dx[i][0];            int yy=y+dx[i][1]; if (xx>=0&&xx<n&&yy>=0&&yy<m&&a[xx][yy]!=-1) {if (dis [XX]                    [Yy]>dis[x][y]+a[xx][yy]) {DIS[XX][YY]=DIS[X][Y]+A[XX][YY];                        if (vis[xx][yy]==0) {vis[xx][yy]=1;                    Q.push (Make_pair (XX,YY));    }}}}}}int main () {int T;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&m); for (int i=0; i<n; i++) {for (int j=0; j<m; J + +) {scanf ("%d", &a[i][j            ]);        }} int k;        scanf ("%d\n", &k); for (int i=0;i<k;i++) {scanf ("%d%d", &point[I].X,&AMP;POINT[I].Y];            }//=== preprocessing = = = = (int i=0;i<k;i++) {dis_border[i]=0x3f3f3f3f;                for (int j=0;j<k;j++) {if (i==j) length[i][j]=0;            else length[i][j]=0x3f3f3f3f;                }} for (int i=0;i< (1<<k), i++) {for (int j=0;j<k;j++) {            dp[i][j]=0x3f3f3f3f;            }}//=== the minimum distance between the points of the treasure and each treasure point and boundary = = = (int i=0; i<k; i++) {SPFA (i);                for (int j=0; j<k; J + +) {if (j==i) continue;            Length[i][j]=min (Dis[point[j].x][point[j].y],length[i][j]);        } dp[1<<i][i]=dis_border[i]+a[point[i].x][point[i].y];                 }///=== the optimal path = = = = (int s=0;s< (1<<k); s++) {for (int i=0;i<k;i++) { if (s& (1<<i) ==0) continue;                if (dp[s][i]==0x3f3f3f3f) continue;                    for (int j=0;j<k;j++) {if (s& (1<<j) ==1) continue; dp[s| ( 1&LT;&LT;J)][j]=min (dp[s| (                1&LT;&LT;J)][j],dp[s][i]+length[i][j]);        }}}///=== also return to the border = = int ans=0x3f3f3f3f;        for (int i=0;i<k;i++) {ans=min (ans,dp[(1<<k) -1][i]+dis_border[i]);    } printf ("%d\n", ans); } return 0;}


HDU 4568 SPFA Shortest path algorithm + travel quotient problem

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.