博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4568 Hunter 最短路+TSP
阅读量:5301 次
发布时间:2019-06-14

本文共 4220 字,大约阅读时间需要 14 分钟。

题目链接:

Hunter

Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)

问题描述

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 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.

输出

For each test case, you should output only a number means the minimum cost.

样例输入

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

样例输出

8

11

题意

给你一个n*m的迷宫,每个方格上标的是花费,现在有些坐标上有宝藏,你可以从边界的方格进入,最后需要从边界处离开,问你获得所有宝藏的最小花费,每个点可以重复走。

题解

构造一个超级节点,点权为0,与所有的边界相连。然后对所有的宝藏的点跑最短路。

重新构造只有宝藏和超级节点的图,任意两个点之间的路径为上面处理出来的最短路。
对于新图跑下TSP就ok啦。

代码

#include#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define X first#define Y second#define mkp make_pair#define lson (o<<1)#define rson ((o<<1)|1)#define mid (l+(r-l)/2)#define sz() size()#define pb(v) push_back(v)#define all(o) (o).begin(),(o).end()#define clr(a,v) memset(a,v,sizeof(a))#define bug(a) cout<<#a<<" = "<
<
VI;typedef pair
PII;typedef vector
> VPII;const int INF=1000000000;const LL INFL=0x3f3f3f3f3f3f3f3fLL;const double eps=1e-9;const double PI = acos(-1.0);//start----------------------------------------------------------------------const int maxn=222;int mat[maxn][maxn];int G[22][22];PII arr[22];int n,m;int dp[1<<15][15];struct HeapNode{ int x,y,d; HeapNode(int x,int y,int d):x(x),y(y),d(d){} bool operator < (const HeapNode& tmp) const { return d>tmp.d; }};int d[22][maxn][maxn];bool done[maxn][maxn];const int dx[]={-1,1,0,0};const int dy[]={0,0,-1,1};void dij(int id,int sx,int sy){ priority_queue
Q; rep(i,0,n) rep(j,0,m) d[id][i][j]=INF; d[id][sx][sy]=0; clr(done,0); Q.push(HeapNode(sx,sy,0)); while(!Q.empty()){ HeapNode u=Q.top(); Q.pop(); int x=u.x,y=u.y; if(done[x][y]) continue; if(x==0||y==0||x==n-1||y==m-1){ d[0][sx][sy]=min(d[0][sx][sy],d[id][x][y]+mat[x][y]-mat[sx][sy]); } done[x][y]=true; for(int i=0;i<4;i++){ int nx=x+dx[i],ny=y+dy[i]; if(nx<0||nx>=n||ny<0||ny>=m) continue; if(mat[nx][ny]==-1) continue; if(d[id][nx][ny]>d[id][x][y]+mat[x][y]){ d[id][nx][ny]=d[id][x][y]+mat[x][y]; Q.push(HeapNode(nx,ny,d[id][nx][ny])); } } }}void init(){ rep(i,0,n) rep(j,0,m) d[0][i][j]=INF;}int main() { int tc,kase=0; scf("%d",&tc); while(tc--) { scf("%d%d",&n,&m); init(); rep(i,0,n) rep(j,0,m){ scf("%d",&mat[i][j]); } int q; scf("%d",&q); for(int i=1;i<=q;i++) scf("%d%d",&arr[i].X,&arr[i].Y); ///dijkstra for(int i=1;i<=q;i++){ dij(i,arr[i].X,arr[i].Y); } ///TSP ///build rep(i,0,22) rep(j,0,22) G[i][j]=INF; rep(i,0,22) G[i][i]=0; for(int i=0;i<=q;i++){ for(int j=0;j<=q;j++){ if(j==i) continue; if(j==0){ G[i][j]=G[j][i]+mat[arr[i].X][arr[i].Y]; continue; } G[i][j]=d[i][arr[j].X][arr[j].Y]; } } ///init rep(i,0,(1<<15)) rep(j,0,15) dp[i][j]=INF; dp[1][0]=0; ///通路 q++; for(int i=2;i<(1<
=INF) prf("0\n"); else prf("%d\n",ans); } return 0;}//end-----------------------------------------------------------------------

转载于:https://www.cnblogs.com/fenice/p/5999935.html

你可能感兴趣的文章
微信小程序之随笔
查看>>
每秒处理10万高并发订单的乐视集团支付系统架构分享
查看>>
Lua_02
查看>>
ios蓝牙详解
查看>>
安装MySQL5.7.18遇到的坑
查看>>
React Native在Android平台运行gif的解决方法转载
查看>>
Mybatis RowBounds 是逻辑分页
查看>>
Nginx缩略图和Fastdfs整合以及image_filter配置,7点经验结论和5个参考资料
查看>>
hdu 3341(ac自动机+状态压缩)
查看>>
hdu 1565(状态压缩基础题)
查看>>
51单片机之蓝牙遥控小车_效果展示+单片机知识+完整蓝牙电车代码
查看>>
使用WNMP时报的错
查看>>
扩展Django内置的auth模块代码示例
查看>>
Sql Server中REPLACE函数的使用
查看>>
hdu 5614
查看>>
SqlServerl的行转列
查看>>
《信息安全系统设计基础》第三周问题总结
查看>>
nextInt()和nextLine()一起使用时的注意点
查看>>
java如何获取一个对象的大小【转】
查看>>
MobilePhone正则表达式
查看>>