Skip this section if you want to use the AC...
Questions should be fully understood. However, I personally think this question is very bad and the meaning is too vague.
First, the number of incoming and outgoing requests can only be one time !! This was not clearly stated in the question, so I had no idea how to start when I saw the question.
Because I think of these data:
1 1 1 1 9 1-1-1-1
-1-1-1 9 9 9-1 1-1
1 1 1 1 9 1-1-1-1
Six treasures four corners are treasures in the middle
If the first data goes in and out twice, you can take away all six treasures and comply with the question "bring out all treasures he can take ".
Even if it is specified that only one entry or exit is allowed, what should this data be output? (If nothing James can get, please output 0)
If the second data goes in and out four times, you can take away all the treasures as quickly as possible.
The only treasure of the third data cannot be taken away.
However, the test data for this question does not contain the above example.
The above is personal YY
The following are the solutions:
Once in and out, find all the treasures that can be found, bare TSP problems.
First, select a favorite algorithm to create a graph. Understand the entire boundary as a point, and find the shortest distance between each two treasures and the shortest distance from the boundary point of each treasure.
Then there is the TSP, And the status is compressed to the DP Solution.
#include<cstdio>#include<iostream>#include<algorithm>#include<queue>#include<cstring>using namespace std;int n,m,tre;int ID[205][205];int map[205][205];int dis[20][20];bool vis[205][205];int dp[1<<16][20];struct node{ int x,y;}t[20];struct node2{ int x,y,dis; bool operator <(const node2 & f) const { return dis>f.dis; }};int dx[]={-1,1,0,0};int dy[]={0,0,-1,1};bool isok(int x,int y){ return x>=0&&x<n&&y>=0&&y<m&&map[x][y]!=-1;}void bfs(int k){ priority_queue<node2> Q; int v[20]={0}; memset(vis,0,sizeof(vis)); node2 f,r; r.x=t[k].x; r.y=t[k].y; r.dis=0; Q.push(r); v[k]=1; vis[r.x][r.y]=1; int tot=1,id; while(!Q.empty()) { f=Q.top(); Q.pop(); if(!v[0]&&(f.x==0||f.y==0||f.x==n-1||f.y==m-1)) { v[0]=1; dis[k][0]=f.dis; dis[0][k]=f.dis+map[t[k].x][t[k].y]; tot++; if(tot==tre+1) return; } id=ID[f.x][f.y]; if(id&&!v[id]) { tot++; v[id]=1; dis[k][id]=f.dis; if(tot==tre+1) return; } for(int d=0;d<4;d++) { r.x=f.x+dx[d]; r.y=f.y+dy[d]; r.dis=f.dis+map[r.x][r.y]; if(isok(r.x,r.y)&&!vis[r.x][r.y]) { vis[r.x][r.y]=1; Q.push(r); } } }}int main(){ int cas; scanf("%d",&cas); while(cas--) { memset(ID,0,sizeof(ID)); memset(dis,0x3f,sizeof(dis)); memset(dp,0x3f,sizeof(dp)); scanf("%d%d",&n,&m); for(int i=0;i<n;i++) for(int j=0;j<m;j++) scanf("%d",&map[i][j]); scanf("%d",&tre); for(int i=1;i<=tre;i++) { scanf("%d%d",&t[i].x,&t[i].y); ID[t[i].x][t[i].y]=i; } for(int i=1;i<=tre;i++) { bfs(i); } dis[0][0]=0; if(!tre) {printf("0");continue;} for(int i=0;i<=tre;i++) { dp[1<<i][i]=dis[0][i]; } int end=(1<<(tre+1)); for(int i=0;i<end;i++) { for(int j=0;j<=tre;j++) { if((i>>j)&1) { for(int k=0;k<=tre;k++) { if((i>>k)&1) { dp[i][j]=min(dp[i][j],dp[i&(~(1<<j))][k]+dis[k][j]); } } } } } printf("%d\n",dp[end-1][0]); } return 0;}