http://acm.hdu.edu.cn/showproblem.php?pid=4568
Meaning of the questions:
There are several grid graph treasures, explore each square has a corresponding price
Lattice each pathway should be explored
Tell you where the treasure
Q exceptionally began to explore the treasures from all parties to return to the starting point and the minimum cost
spfa particularly to obtain each slave treasure minimum cost, and the minimum cost between each two treasure
Then is the classic traveling salesman problem
For details, see https://www.cnblogs.com/TheRoadToTheGold/p/12384542.html
#include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define N 201 #define M 14 int n,m,tot; int a[N][N]; int tx[M],ty[M]; int dis[M][M]; struct node { int x,y,d; }now,nxt; queue<node>q; int f[N][N]; bool vis[N][N]; int dx[4]={-1,1,0,0}; int dy[4]={0,0,-1,1}; int dp[1<<M][M]; void bfs(int s) { for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) f[i][j]=1e9; now.d=0; if(!s) { now.x=1; for(int i=1;i<=m;++i) if(!vis[1][i] && a[1][i]!=-1) { now.y=i; q.push(now); vis[1][i]=true; f[1][i]=a[1][i]; } now.x=n; for(int i=1;i<=m;++i) if(!vis[n][i] && a[n][i]!=-1) { now.y=i; q.push(now); vis[n][i]=true; f[n][i]=a[n][i]; } now.y=1; for(int i=1;i<=n;++i) if(!vis[i][1] && a[i][1]!=-1) { now.x=i; q.push(now); vis[i][1]=true; f[i][1]=a[i][1]; } now.y=m; for(int i=1;i<=n;++i) if(!vis[i][m] && a[i][m]!=-1) { now.x=i; q.push(now); vis[i][m]=true; f[i][m]=a[i][m]; } } else { now.x=tx[s]; now.y=ty[s]; q.push(now); vis[now.x][now.y]=true; f[now.x][now.y]=0; } while(!q.empty()) { now=q.front(); q.pop(); vis[now.x][now.y]=false; for(int i=0;i<4;++i) { nxt.x=now.x+dx[i]; nxt.y=now.y+dy[i]; if(nxt.x && nxt.x<=n && nxt.y && nxt.y<=m && a[nxt.x][nxt.y]!=-1 && f[nxt.x][nxt.y]>f[now.x][now.y]+a[nxt.x][nxt.y]) { f[nxt.x][nxt.y]=f[now.x][now.y]+a[nxt.x][nxt.y]; if(!vis[nxt.x][nxt.y]) { q.push(nxt); vis [nxt.x] [nxt.y] = true ; } } } } for(int i=1;i<=tot;++i) dis[s][i]=f[tx[i]][ty[i]]; } int main () { int T,S; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) scanf("%d",&a[i][j]); scanf("%d",&tot); for(int i=1;i<=tot;++i) { scanf("%d%d",&tx[i],&ty[i]); tx[i]++; ty [i] ++ ; } bfs(0); for(int i=1;i<=tot;++i) bfs(i); S=(1<<tot+1)-1; for(int i=1;i<=S;++i) for(int j=0;j<=tot;++j) dp[i][j]=1e9; for(int i=1;i<=tot;++i) dp[0][i]=dis[0][i]-a[tx[i]][ty[i]]; for(int i=1;i<S;++i) for(int j=0;j<=tot;++j) if(!(i&1<<j)) for(int k=1;k<=tot;++k) if(i&1<<k) dp[i][j]=min(dp[i][j],dis[j][k]+dp[i^1<<k][k]); printf("%d\n",dp[S-1][0]); } }
Hunter
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2804 Accepted Submission(s): 899
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 could.
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 explore 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 are 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.
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 explore 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 are 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 should output only 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
Source
Recommend