hdu 4568 spfa 最短路算法+旅行商问题
来源:互联网 发布:淘宝如何定义客户信息 编辑:程序博客网 时间:2024/06/05 14:33
http://acm.hdu.edu.cn/showproblem.php?pid=4568
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.
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
23 33 2 35 4 31 4 211 13 33 2 35 4 31 4 221 12 2
Sample Output
811
/**hdu 4568 spfa 最短路算法+旅行商问题题目大意:给定一个n*m的棋盘,每一个格子有一个值,代表经过这个格子的花费,给出sum个宝藏点的坐标,求从棋盘的任意一个边进入棋盘,经过所有的宝藏点后在走出 棋盘所需要的最小花费解题思路:spfa处理处任意两个宝藏点之间的最短距离(最小花费)和每个宝藏点和边界的最短距离。然后状态压缩:dp[s][i]表示经过宝藏点的状态为s并且结尾点为i的 最小花费*/#include <stdio.h>#include <string.h>#include <algorithm>#include <iostream>#include <queue>using namespace std;const int maxn=205;int n,m;struct note{ int x,y;} point[15];int dx[4][2]= {1,0,0,1,-1,0,0,-1};int dis[maxn][maxn],a[maxn][maxn],dis_border[25],length[20][20];bool vis[maxn][maxn];int dp[1<<15][15];void spfa(int s){ for(int i=0;i<n;i++) for(int j=0;j<m;j++) dis[i][j]=0x3f3f3f3f; memset(vis,0,sizeof(vis)); queue<pair<int,int> >q; q.push(make_pair(point[s].x,point[s].y)); vis[point[s].x][point[s].y]=1; dis[point[s].x][point[s].y]=0; while(!q.empty()) { int x=q.front().first; int y=q.front().second; q.pop(); vis[x][y]=0; if(x==0||x==n-1||y==0||y==m-1) dis_border[s]=min(dis_border[s],dis[x][y]); for(int i=0; i<4; i++) { int xx=x+dx[i][0]; int yy=y+dx[i][1]; if(xx>=0&&xx<n&&yy>=0&&yy<m&&a[xx][yy]!=-1) { if(dis[xx][yy]>dis[x][y]+a[xx][yy]) { dis[xx][yy]=dis[x][y]+a[xx][yy]; if(vis[xx][yy]==0) { vis[xx][yy]=1; q.push(make_pair(xx,yy)); } } } } }}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",&a[i][j]); } } int k; scanf("%d\n",&k); for(int i=0;i<k;i++) { scanf("%d%d",&point[i].x,&point[i].y); } //===预处理=== for(int i=0;i<k;i++) { dis_border[i]=0x3f3f3f3f; for(int j=0;j<k;j++) { if(i==j) length[i][j]=0; else length[i][j]=0x3f3f3f3f; } } for(int i=0;i<(1<<k);i++) { for(int j=0;j<k;j++) { dp[i][j]=0x3f3f3f3f; } } //===求有宝藏的点之间和每一个宝藏点和边界的最短距离=== for(int i=0; i<k; i++) { spfa(i); for(int j=0; j<k; j++) { if(j==i)continue; length[i][j]=min(dis[point[j].x][point[j].y],length[i][j]); } dp[1<<i][i]=dis_border[i]+a[point[i].x][point[i].y]; } ///===求最优路径=== for(int s=0;s<(1<<k);s++) { for(int i=0;i<k;i++) { if(s&(1<<i)==0)continue; if(dp[s][i]==0x3f3f3f3f)continue; for(int j=0;j<k;j++) { if(s&(1<<j)==1)continue; dp[s|(1<<j)][j]=min(dp[s|(1<<j)][j],dp[s][i]+length[i][j]); } } } ///===还要回到边界== int ans=0x3f3f3f3f; for(int i=0;i<k;i++) { ans=min(ans,dp[(1<<k)-1][i]+dis_border[i]); } printf("%d\n",ans); } return 0;}
0 0
- hdu 4568 spfa 最短路算法+旅行商问题
- HDU 2066(一个人的旅行) SPFA(最短路问题)
- HDU 2066 一个人的旅行【最短路,多起点多终点,Dijkstra算法+spfa算法】
- HDU 2066 一个人的旅行【最短路,多起点多终点,Dijkstra算法+spfa算法】
- HDU 2066 一个人的旅行【最短路 dijkstra & floyed & SPFA 】
- hdu 2066 一个人的旅行(最短路+SPFA)
- hdu 2066 一个人的旅行 最短路spfa
- 最短路+hdu+spfa算法模板题
- HDU 2544 最短路 <SPFA算法>
- hdu 2544 最短路(SPFA算法)
- 最短路算法spfa hdu 1863
- HDU 3790 最短路径问题【多关键字最短路,Dijkstra算法+spfa算法】
- 最短路+状态压缩dp(旅行商问题)hdu-4568-Hunter
- 最短路--SPFA算法
- 最短路SPFA算法
- 最短路: SPFA算法
- spfa算法 最短路
- SPFA最短路算法
- 在Python中生成单体实例的方法
- 而健康三姐夫了三剑客
- 7.1 Qstring
- C++的内存管理
- R语言-安装randomForest和ROCR包
- hdu 4568 spfa 最短路算法+旅行商问题
- String详解, String和CharSequence区别, StringBuilder和StringBuffer的区别 (String系列之1)
- C语言数组类型以及函数类型的定义
- android开发之上传图片和调用相机以及相册
- Source Insight基本使用和快捷键
- 鸡啄米vc++2010系列29(树形控件Tree Control 上)
- 虚拟现实(VR)vs增强现实(AR)
- MySQL查看数据库连接数和状态
- sql中的group by 和 having 用法解析