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.
 
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.
 
题目大意:一个n*m的格子,每个格子有一个花费cost,如果花费是-1代表格子不能走。现在格子里有K个宝藏,问拿齐K个宝藏的最小花费,只能进出一次。
思路:首先,注意到K很小,还是一个很奇葩的数字。所以,其实这题的路径就是要走遍这K个点的最短路,这就是经典的TSP问题,位DP解决。需要预处理出每个点到其他点的最短路径,把矩阵外看成一个点(在我的代码里标号为K),对每个点做单源最短路径即可。
PS:这题有说,如果拿不到任何宝藏输出0,其实这条件是没用的……这题的描述有一点坑爹,如果你有一些宝藏能拿到,有一些宝藏不能拿到(被一堆-1围着),那我要输出啥?经测试根本就没有这种东西……因为都是有答案的所以根本就不需要考虑宝藏所在点是-1和点点之间不连通这种坑爹的问题了。
PS2:我做这题的时候没看到-1,然后Dijkstra+heap不断MLE,后来加了个vis判是否出队的问题,就WA了……之前一直没想明白为什么会出现MLE的情况,看来是-1加了进去有无穷多的数进队导致……
 
代码(171MS):
 #include <cstdio>
#include <queue>
#include <utility>
#include <iostream>
#include <cstring>
using namespace std;
typedef pair<int, int> PII; const int MAXN = ; int mat[MAXN][MAXN], tx[], ty[];
int dis[MAXN][MAXN], di[][];
bool ist[MAXN][MAXN];
int post[MAXN][MAXN];
int n, m, k; #define pos(x, y) (x*MAXN+y) int fx[] = {-,,,};
int fy[] = {,,,-}; void min_path(int st_x, int st_y, int now) {
priority_queue<PII> que;
que.push(make_pair(-mat[st_x][st_y], pos(st_x, st_y)));
memset(dis, 0x3f, sizeof(dis));
dis[st_x][st_y] = mat[st_x][st_y];
while(!que.empty()) {
int abc = -que.top().first, tmp = que.top().second; que.pop();
int x = tmp / MAXN, y = tmp % MAXN;
if(abc != dis[x][y]) continue;
for(int i = ; i < ; ++i) {
int newx = x + fx[i], newy = y + fy[i];
if( <= newx && newx < n && <= newy && newy < m) {
if(mat[newx][newy] == -) continue;
if(dis[newx][newy] > mat[newx][newy] + dis[x][y]) {
dis[newx][newy] = mat[newx][newy] + dis[x][y];
que.push(make_pair(-dis[newx][newy], pos(newx, newy)));
if(ist[newx][newy] && dis[newx][newy] < di[now][post[newx][newy]])
di[now][post[newx][newy]] = dis[newx][newy];
}
}
else if(dis[x][y] < di[now][k]) di[now][k] = dis[x][y];
}
}
} int dp[][];
int ans, sum; int dfs(int u, int use) {
if(dp[u][use] < 0x3f3f3f3f) return dp[u][use];
if(use == ) {
return dp[u][use] = di[u][k];
}
for(int i = ; i < k; ++i) {
if(use & ( << i))
dp[u][use] = min(dp[u][use], di[u][i] + dfs(i, use ^ ( << i)));
}
return dp[u][use];
} void solve() {
memset(dp, 0x3f, sizeof(dp));
ans = 0x7fff7fff;
for(int i = ; i < k; ++i)
ans = min(ans, di[i][k] + dfs(i, ( << i) ^ (( << k) - )));
} void printdi() {
for(int i = ; i < k; ++i) {
for(int j = ; j <= k; ++j) printf("%d ", di[i][j]);
printf("\n");
}
} int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
for(int i = ; i < n; ++i)
for(int j = ; j < m; ++j) scanf("%d", &mat[i][j]);
scanf("%d", &k);
memset(ist, , sizeof(ist));
sum = ;
for(int i = ; i < k; ++i) {
scanf("%d%d", &tx[i], &ty[i]);
ist[tx[i]][ty[i]] = true;
post[tx[i]][ty[i]] = i;
sum += mat[tx[i]][ty[i]];
}
memset(di, 0x3f, sizeof(di));
for(int i = ; i < k; ++i) min_path(tx[i], ty[i], i);
//printdi();
solve();
printf("%d\n", ans - sum);
}
}

HDU 4568 Hunter(最短路径+DP)(2013 ACM-ICPC长沙赛区全国邀请赛)的更多相关文章

  1. HDU 4571 Travel in time ★(2013 ACM/ICPC长沙邀请赛)

    [题意]给定N个点,每个点有一个停留所需的时间Ci,和停留能够获得的满意度Si,有M条边,每条边代表着两个点走动所需的时间ti,现在问在规定的T时间内从指定的一点S到E能够获得的最大的满意度是多少?要 ...

  2. HDU 4758——Walk Through Squares——2013 ACM/ICPC Asia Regional Nanjing Online

    与其说这是一次重温AC自动机+dp,倒不如说这是个坑,而且把队友给深坑了. 这个题目都没A得出来,我只觉得我以前的AC自动机的题目都白刷了——深坑啊. 题目的意思是给你两个串,每个串只含有R或者D,要 ...

  3. HDU 4571 Travel in time(最短路径+DP)(2013 ACM-ICPC长沙赛区全国邀请赛)

    Problem Description Bob gets tired of playing games, leaves Alice, and travels to Changsha alone. Yu ...

  4. hdu 4751 Divide Groups bfs (2013 ACM/ICPC Asia Regional Nanjing Online 1004)

    SDUST的训练赛 当时死磕这个水题3个小时,也无心去搞其他的 按照题意,转换成无向图,预处理去掉单向的边,然后判断剩下的图能否构成两个无向完全图(ps一个完全图也行或是一个完全图+一个孤点) 代码是 ...

  5. 2013 ACM/ICPC 长沙网络赛J题

    题意:一个数列,给出这个数列中的某些位置的数,给出所有相邻的三个数字的和,数列头和尾处给出相邻两个数字的和.有若干次询问,每次问某一位置的数字的最大值. 分析:设数列为a1-an.首先通过相邻三个数字 ...

  6. 2013 ACM/ICPC 长沙现场赛 A题 - Alice's Print Service (ZOJ 3726)

    Alice's Print Service Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice is providing print ser ...

  7. 2013 ACM/ICPC 长沙现场赛 C题 - Collision (ZOJ 3728)

    Collision Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge There's a round medal ...

  8. HDU 4573 Throw the Stones(动态三维凸包)(2013 ACM-ICPC长沙赛区全国邀请赛)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4573 Problem Description Remember our childhood? A fe ...

  9. HDU 4569 Special equations(枚举+数论)(2013 ACM-ICPC长沙赛区全国邀请赛)

    Problem Description Let f(x) = anxn +...+ a1x +a0, in which ai (0 <= i <= n) are all known int ...

随机推荐

  1. Jquery知识点总结(一)

    JQuery遍历1 传统的for   2 通过each对象调用callback函数 callback回调函数 /*    * JQ提供的技术,实现遍历    * JQ对象函数调用 each(参数 ca ...

  2. kafka topic制定规则

    kafka topic的制定,我们要考虑的问题有很多,比如生产环境中用几备份.partition数目多少合适.用几台机器支撑数据量,这些方面如何去考量?笔者根据实际的维护经验,写一些思考,希望大家指正 ...

  3. acm--1004

    问题描述 再次比赛时间!看到气球在四周漂浮,多么兴奋.但要告诉你一个秘密,评委最喜欢的时间是猜测最流行的问题.比赛结束后,他们会统计每种颜色的气球并找出结果. 今年,他们决定离开这个可爱的工作给你. ...

  4. 将图片写入二进制文件,再从二进制文件还原图片(c++)

    #include "string" #include "iostream" #include "fstream" using namespa ...

  5. 用Jquery控制元素的上下移动 实现排序功能

    在页面上,控制元素上下移动,进行排序是我们比较常用的功能,今天我用jQuery 写个 简单方便,功能齐全的实现方式. 话不多说,直接上代码,下面是基础的引入jq和html元素部分: <scrip ...

  6. 编写radware的负载配置

    radware如何添加负载服务? 笔者在新添加radware的新负载服务的时候,是习惯去看下上一个负载服务的ID 和 节点服务的ID 号 分别是多少,主要是避免ID冲突,把其他服务顶替下去,同时以后这 ...

  7. OrientDB部署

    1. 环境准备 操作系统: Centos6.8 内存: 8G(分布式部署时建议4G及以上,否则需要手动修改JVM配置) JDK: 建议jdk8版本(3.0版本要求jdk8) 环境变量:需配置JAVA_ ...

  8. git 之忽略文件 gitignore 创建和使用规则

    1..gitignore文件的创建:首先要强调一点,这个文件的完整文件名就是“.gitignore”,注意最前面有个“.”.这样没有扩展名的文件在Windows下不太好创建,这里给出win7的创建方法 ...

  9. Drill-On-YARN

    1. Drill-On-YARN介绍 功能 启动 停止 扩容 缩容 failover 启动流程 下载drill的社区包,进行必要的配置,执行drill-on-yarn.sh start命令,启动dri ...

  10. 使用Goland同步远程代码

    新版本的goland貌似已经有了Deployment功能,故本篇文章描述的方法也不推荐使用了 以前写php时候习惯使用phpstorm这个编译器,除去本身功能强大不说,比较方便的是其自身带的Deplo ...