HDU 4568 Hunter(最短路径+DP)(2013 ACM-ICPC长沙赛区全国邀请赛)
2013-08-21 22:48
627 查看
[align=left]Problem Description[/align]
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.
View Code
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.
#include <cstdio> #include <queue> #include <utility> #include <iostream> #include <cstring> using namespace std; typedef pair<int, int> PII; const int MAXN = 205; int mat[MAXN][MAXN], tx[15], ty[15]; int dis[MAXN][MAXN], di[15][15]; bool ist[MAXN][MAXN]; int post[MAXN][MAXN]; int n, m, k; #define pos(x, y) (x*MAXN+y) int fx[4] = {-1,0,1,0}; int fy[4] = {0,1,0,-1}; 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 = 0; i < 4; ++i) { int newx = x + fx[i], newy = y + fy[i]; if(0 <= newx && newx < n && 0 <= newy && newy < m) { if(mat[newx][newy] == -1) 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[14][20000]; int ans, sum; int dfs(int u, int use) { if(dp[u][use] < 0x3f3f3f3f) return dp[u][use]; if(use == 0) { return dp[u][use] = di[u][k]; } for(int i = 0; i < k; ++i) { if(use & (1 << i)) dp[u][use] = min(dp[u][use], di[u][i] + dfs(i, use ^ (1 << i))); } return dp[u][use]; } void solve() { memset(dp, 0x3f, sizeof(dp)); ans = 0x7fff7fff; for(int i = 0; i < k; ++i) ans = min(ans, di[i][k] + dfs(i, (1 << i) ^ ((1 << k) - 1))); } void printdi() { for(int i = 0; i < k; ++i) { for(int j = 0; 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 = 0; i < n; ++i) for(int j = 0; j < m; ++j) scanf("%d", &mat[i][j]); scanf("%d", &k); memset(ist, 0, sizeof(ist)); sum = 0; for(int i = 0; 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 = 0; i < k; ++i) min_path(tx[i], ty[i], i); //printdi(); solve(); printf("%d\n", ans - sum); } }
View Code
相关文章推荐
- HDU 4571 Travel in time(最短路径+DP)(2013 ACM-ICPC长沙赛区全国邀请赛)
- HDU 4569 Special equations(枚举+数论)(2013 ACM-ICPC长沙赛区全国邀请赛)
- HDU 4572 Bottles Arrangement(数学推公式)——2013 ACM-ICPC长沙赛区全国邀请赛
- HDU 4565 -- So Easy! 数学 && 2013 ACM-ICPC 长沙赛区全国邀请赛 A题
- HDU 4565 So Easy!(数学+矩阵快速幂)(2013 ACM-ICPC长沙赛区全国邀请赛)
- HDU 4565 So Easy!(思想+矩阵快速幂)——2013 ACM-ICPC长沙赛区全国邀请赛
- HDU 4573 Throw the Stones(动态三维凸包)(2013 ACM-ICPC长沙赛区全国邀请赛)
- HDU 4571 Travel in time 2013 ACM-ICPC长沙赛区全国邀请赛G题
- HDU 4569 Special equations(思维)——2013 ACM-ICPC长沙赛区全国邀请赛
- hdu 4565 So Easy! /2013 ACM-ICPC 长沙赛区全国邀请赛A题 矩阵乘法
- 2013 ACM-ICPC长沙赛区全国邀请赛——A So Easy!
- 2013 ACM-ICPC吉林通化全国邀请赛 && HDU 4597 Play Game (博弈 + 区间dp)
- 2013 ACM-ICPC长沙赛区全国邀请赛——Bottles Arrangement
- HDU_2013 ACM-ICPC南京赛区全国邀请赛——题目重现
- hdu 4576 robot 2013 ACM-ICPC杭州赛区全国邀请赛——题目重现-1001-robot
- hdu 4578 Transformation 2013ACM-ICPC杭州赛区全国邀请赛
- 2013 ACM/ICPC 长沙赛区湖大全国邀请赛 A题(6.1修订)
- hdu 4587 割点 2013 ACM-ICPC南京赛区全国邀请赛
- 周六训练:2013 ACM-ICPC长沙赛区全国邀请赛
- 2013 ACM-ICPC长沙赛区全国邀请赛So Easy! && 2015 ACM/ICPC Asia Regional Shenyang Online-Best Solver