Hunter

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2014    Accepted Submission(s): 615

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.
 
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
/*
hdu 4568 Hunter 最短路+dp problem:
给你一个n*m的地图,每走一个格子会有一定的花费.求拿到所有宝藏的最小花费 solve:
想了很久感觉没什么思路TAT
后来看别人题解可以把这个问题转换一下.
因为总共只有13个宝藏,所以我们可以处理出每个宝藏之间的最短路(即最小花费).
在弄出每个宝藏到边界的最小距离. 那么就可以看成13个点的图.找出遍历所有点的最小花费
而13个点的状态可以用二进制保存,所以dp就好. hhh-2016-08-26 21:43:38
*/
#pragma comment(linker,"/STACK:124000000,124000000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <math.h>
#include <queue>
#include <map>
#define lson i<<1
#define rson i<<1|1
#define ll long long
#define clr(a,b) memset(a,b,sizeof(a))
#define scanfi(a) scanf("%d",&a)
#define scanfl(a) scanf("%I64d",&a)
#define key_val ch[ch[root][1]][0]
#define inf 0x3f3f3f3f
using namespace std;
const ll mod = 1e9+7;
const int maxn = 220; struct NODE
{
int id,len;
NODE() {}
NODE(int i,int d):id(i),len(d) {}
bool operator <(const NODE &a) const
{
return len > a.len;
}
};
int n,m;
int dx[4]= {-1,1,0,0};
int dy[4]= {0,0,-1,1};
int from[maxn][maxn];
int vis[maxn*maxn],tmap[maxn][maxn],tp[maxn][maxn],out[maxn];
int pos[maxn*maxn],dis[maxn*maxn];
void dij(int st,int fp)
{
memset(vis,0,sizeof(vis));
memset(dis,inf,sizeof(dis));
priority_queue<NODE> q;
q.push(NODE(st,0));
dis[st] = 0;
while(!q.empty())
{
NODE t = q.top();
q.pop();
int cur = t.id;
if(vis[cur]) continue;
vis[cur] = 1;
int x = cur/m,y = cur%m;
if(tp[x][y] != -1)
from[fp][tp[x][y]] = dis[cur];
if(x == n-1 || x == 0 || y == m-1 || y == 0)
{
out[fp]= min(out[fp],dis[cur]);
}
for(int i = 0; i < 4; i++)
{
int tx = x + dx[i];
int ty = y + dy[i];
if(tmap[tx][ty] == -1) continue;
if(tx >= n || tx < 0 || ty >= m || ty < 0)
continue;
if(dis[tx*m + ty] > dis[cur] + tmap[tx][ty])
{
dis[tx*m + ty] = dis[cur] + tmap[tx][ty];
q.push(NODE(tx*m + ty, dis[tx*m + ty] ));
}
}
}
return ;
} int dp[20][1 << 14];
void init()
{
memset(from,0,sizeof(from));
memset(out,inf,sizeof(out));
memset(tp,-1,sizeof(tp));
memset(dp,inf,sizeof(dp));
} int main()
{
// freopen("in.txt","r",stdin);
int T,x,y;
scanf("%d",&T);
while(T--)
{
init();
scanfi(n),scanfi(m);
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
scanfi(tmap[i][j]);
}
int q;
scanfi(q);
for(int i = 0; i < q; i++)
{
scanfi(x),scanfi(y);
tp[x][y] = i;
pos[i] = x*m + y;
}
for(int i = 0; i < q; i++)
{
from[i][i] = 0;
dij(pos[i],i);
}
for(int i = 0; i < q; i++)
{
dp[i][1 << i] = out[i]+tmap[pos[i]/m][pos[i]%m];
}
// for(int i = 0;i < q;i++)
// {
// cout << "i"<<i<<":" <<out[i] <<endl;
// for(int j = i;j < q;j++)
// cout << i <<"----"<<j<<" :" <<from[i][j]<<endl;
// }
// cout <<endl;
for(int i = 0; i < (1 << q); i++)
{
for(int j = 0; j < q; j++)
{
if( ((1 << j) & i) && i != (1 << j))
{
for(int k = 0; k < q; k++)
{
if( (i & (1 << k)) && j != k && i != (1<< k))
dp[j][i] = min(dp[k][i-(1 << j)]+from[k][j],dp[j][i]);
}
}
}
}
int ans = inf;
for(int i = 0; i < q; i++)
{
ans = min(ans,dp[i][(1<<q)-1] + out[i]);
// cout << ans <<endl;
}
printf("%d\n",ans);
}
return 0;
}

  

hdu 4568 Hunter 最短路+dp的更多相关文章

  1. HDU 4568 Hunter 最短路+TSP

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4568 Hunter Time Limit: 2000/1000 MS (Java/Others)Me ...

  2. HDU 4568 Hunter 最短路+状压DP

    题意:给一个n*m的格子,格子中有一些数,如果是正整数则为到此格子的花费,如果为-1表示此格子不可到,现在给k个宝藏的地点(k<=13),求一个人从边界外一点进入整个棋盘,然后拿走所有能拿走的宝 ...

  3. HDU 4568 Hunter(最短路径+DP)(2013 ACM-ICPC长沙赛区全国邀请赛)

    Problem Description One day, a hunter named James went to a mysterious area to find the treasures. J ...

  4. hdu 4568 Hunter(spfa预处理 + 状压dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4568 思路:首先spfa预处理出每对宝藏之间的最短距离以及宝藏到边界的最短距离,然后dp[state] ...

  5. hdu 4568 Hunter bfs建图+TSP状压DP

    想AC的人请跳过这一段... 题目应该都能读懂.但是个人觉得这题出的很烂,意思太模糊了. 首先,进出次数只能是一次!!这个居然在题目中没有明确说明,让我在当时看到题目的时候无从下手. 因为我想到了这几 ...

  6. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  7. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

  8. hdu 2829 Lawrence(斜率优化DP)

    题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...

  9. BZOJ_1003_[ZJOI2006]物流运输_最短路+dp

    BZOJ_1003_[ZJOI2006]物流运输_最短路+dp 题意:http://www.lydsy.com/JudgeOnline/problem.php?id=1003 分析: 这种一段一段的显 ...

随机推荐

  1. 第二次作业:APP案例分析

    App案例分析 产品:三国杀-页游手游双通 选择理由 当今社会手机已经渐渐取代了电脑在人们日常生活的需求,既然要选择APP进行案例分析,首推的估计就是手机APP了.三国杀是陪伴我高中时代的主要娱乐方式 ...

  2. mongodb 复制(副本集)

    复制(副本集) 什么是复制 复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性,并可以保证数据的安全性 复制还允许从硬件故障和服务中断中恢复数据 为什么要复制 数据备份 数据灾 ...

  3. 201621123031 《Java程序设计》第2周学习总结

    Week02-Java基本语法与类库 1. 本周学习总结 本周讲了Java的基本数据类型,主要分为八类(byte,short,int,long,double,float,char,boolean),其 ...

  4. django启动uwsgi报错

    查看uwsgi.log *** Starting uWSGI 2.0.17 (64bit) on [Thu Apr 5 17:46:15 2018] *** compiled with version ...

  5. LightningChart最新版 v.8.3 全新发布,新功能使用教程。

    LightningChart最新版v.8.3全新发布,主要介绍以下五个新功能及使用教程.   1. 网格模型,三角鼠标追踪 Tracing MeshModels with mouse. Traced ...

  6. css3兼容IE8的方案 各个ie的hack

    虽然现在很多项目已经对低版本IE不要求了,但是还有部分公司对IE8还是很执着的,咱作为屌丝前端程序员不能和老板说前端潮流,不能说趋势,只能动脑子了,下面就分享一些css3兼容ie8的方案思路.主要是实 ...

  7. 手机PC监听用户复制内容

    最近应项目需求,为了获取到更多用户想要搜索的信息,需要把用户点击复制的内容获取到,然后传送给后台以更好的了解客户需求,自己在这个方法上栽了个大跟头,只考虑其一却不知道结合使用,脑袋卡顿,随笔记下,望自 ...

  8. 说说cglib动态代理

    前言 jdk中的动态代理通过反射类Proxy和InvocationHandler回调接口实现,要求委托类必须实现一个接口,只能对该类接口中定义的方法实现代理,这在实际编程中有一定的局限性. cglib ...

  9. matlab等高线绘制

    参考代码: figure;// Figure建立新的图形 z=double(z); x=1:length(z); y=x; [X2,Y2]=meshgrid(x,y); subplot(121); [ ...

  10. python/Django(增、删、改、查)操作

    python/Django(增.删.改.查)操作 我们要通过pycharm中的Django模块连接MySQL数据库进行对数据的操作. 一.创建Django项目(每创建一个项目都要进行以下设置) 1.如 ...