hdu4568Hunter(BFS+TSP)
来源:互联网 发布:有什么交通大数据 编辑:程序博客网 时间:2024/06/05 15:44
Hunter
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 725 Accepted Submission(s): 200
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.
23 33 2 35 4 31 4 211 13 33 2 35 4 31 4 221 12 2
811
刚看到这道题时,我是立马就想到了BFS,因为是图和最短路的结合,这时候用BFS准没错。
但是我就着BFS的常规思路想下去想了一天。没有任何结果。。。
后来看了一下最小的数字是n,就是宝藏的点数才到13,我就知道突破点在这里了。。
后来总结,一般看到一道题当自己完全没思路时,可以按题目最小的数字想。突破点一定在这里,特别是动态规划题。。
然后就把每个节点的最短路径求出来。然后那200*200的迷宫地图对于我们就没用了。
我们只需要是点与点的最短路径。
然后就是有13个点的TSP问题了(外面算一个点)
但是悲哀的是,用BFS把没两个节点最短路径求出来之后就不会做了,如果是枚举 O(N !)复杂度就绝对超时。
然后就上网找TSP的高效解法。选中了动态规划。
后来只能将TSP动态规划的模板原封不动地抄下来。。。
终于A了。
动态规划方程解释:
假设从顶点s出发,令d(i, V’)表示从顶点i出发经过V’(是一个点的集合)中各个顶点一次且仅一次,最后回到出发点s的最短路径长度。
推导:(分情况来讨论)
①当V’为空集,那么d(i, V’),表示从i不经过任何点就回到s了,如上图的 城市3->城市0(0为起点城市)。此时d(i, V’)=Cis(就是 城市i 到 城市s 的距离)、
②如果V’不为空,那么就是对子问题的最优求解。你必须在V’这个城市集合中,尝试每一个,并求出最优解。
d(i, V’)=min{Cik + d(k, V’-{k})}
注:Cik表示你选择的城市和城市i的距离,d(k, V’-{k})是一个子问题。
综上所述,TSP问题的动态规划方程就出来了:
dp[i][j]的解释:i是一种状态如:i=3 则i为00000000000011,就是说只有点一和点二在集合中。那这样就可以表示V’这个集合了。。。。
#include<cstdio>#include<cstring>#include<cstdlib>#include<queue>using namespace std;int path[202][202];int f[202][202],flag[202][202];int minpath[15][15];int dir[][2] = {{-1,0},{0,-1},{1,0},{0,1}};int row,col;int n;int pf[15];int dp[1<<16][20];struct node{int x;int y;int min;bool operator < (const node& a) const{return min>a.min;}};struct node1{int x;int y;}node1[20];int Min(int a,int b){return a>b?b:a;}void BFS(int x,int y,int num){memset(flag,0,sizeof(flag));priority_queue<node> pq;while(!pq.empty())pq.pop();struct node nn,var;nn.x = x;nn.y = y;nn.min = 0;pq.push(nn);flag[x][y] = 1;while(!pq.empty()){nn = pq.top();pq.pop();if(f[nn.x][nn.y]>0){int i = f[nn.x][nn.y];minpath[num][i] = nn.min;pf[num]++;}if(minpath[num][0]==-1 && (nn.x==1 || nn.x==row || nn.y==1 || nn.y==col)){minpath[num][0] = nn.min;minpath[0][num] = nn.min+path[x][y];pf[num]++;}if(pf[num]>n )return ;for(int j=0;j<4;j++){int xx = nn.x+dir[j][0];int yy = nn.y+dir[j][1];if(path[xx][yy]!=-1 && flag[xx][yy]==0){var.min = nn.min+path[xx][yy];var.x = xx;var.y = yy;pq.push(var);flag[xx][yy] = 1;}}}}int main(){int T,i,j;scanf("%d",&T);while(T--){memset(path,-1,sizeof(path));memset(f,0,sizeof(f));memset(minpath,-1,sizeof(minpath));scanf("%d%d",&row,&col);for(i=1;i<=row;i++)for(j=1;j<=col;j++)scanf("%d",&path[i][j]);scanf("%d",&n);for(i=1;i<=n;i++){scanf("%d%d",&node1[i].x,&node1[i].y);node1[i].x++;node1[i].y++;f[node1[i].x][node1[i].y] = i;}memset(pf,0,sizeof(pf));for(i=1;i<=n;i++)BFS(node1[i].x,node1[i].y,i); //每次求出node1[i]和其他点的最短距离,包括出去的最短距离memset(dp,0x3f,sizeof(dp));minpath[0][0] = 0; if(!n) {printf("0\n");continue;} for( i=0;i<=n;i++)//这里到下面都是套用别人的模板 { dp[1<<i][i]=minpath[0][i]; //自己对TSP的动态规划不熟,不能自己写~~~ }int end=(1<<(n+1)); for( i=0;i<end;i++) { for( j=0;j<=n;j++) { if((i>>j)&1) //满足j要在i状态里面 { for(int k=0;k<=n;k++) { if((i>>k)&1) //满足k要在i状态里面 { dp[i][j]=Min(dp[i][j],dp[i&(~(1<<j))][k]+minpath[k][j]); } } } } } printf("%d\n",dp[end-1][0]);}return 0;}
- hdu4568Hunter(BFS+TSP)
- hdu4568Hunter(最短路+TSP)
- hdu3681 BFS+TSP+二分
- HDU3681Prison Break(BFS+TSP+二分+dp状态压缩)
- HDOJ 题目4568 Hunter(BFS+状压DP,TSP)
- HDOJ 题目4856 Tunnels(BFS+状态压缩,TSP)
- Cleaning Robot-dfs+bfs+tsp
- HOJ 2226&POJ2688 Cleaning Robot(BFS+TSP(状态压缩DP))
- BFS+状态压缩DP+二分枚举+TSP
- BFS+优先队列+状态压缩DP+TSP
- HDU 4568 - Hunter(BFS+DP`TSP)
- hdu 3681 Prison Break bfs + 二分 + TSP
- POJ2688 Cleaning Robot(BFS+TSP问题)
- FZU 2186 BFS+状压(类似TSP)
- UVa 116 - Unidirectional TSP(单向TSP)
- 单向TSP(unidirectional TSP,UVa 116)
- uva116 unidirectional TSP 单向TSP (DP)
- hdu 4771 Stealing Harry Potter's Precious (2013 杭州 onsite B题)bfs预处理后,dfs枚举或tsp
- java 基础知识总结
- Android 代码片段---从相册或相机获取图片保存并处理
- mysql导入及导出txt文件
- 图形学实验2-二维射击游戏
- C# combobox绑定数据源(datasource)
- hdu4568Hunter(BFS+TSP)
- hdu 3879 Base Station (最大权闭合图)
- Noip冲刺
- hdu 4734 F(x) (2013成都网络赛G题)(数位DP)
- strstr函数的简单实现
- Window7—64位:Ruby on Rails - “Add 'gem sqlite3'' to your Gemfile”
- ubuntu12.04 qt连接mysql
- IOS的变量前加extern和static字段
- 第三方支付业务浅析