HDU 5253 连接的管道 (bfs + 优先队列)
来源:互联网 发布:mysql 现实数据库 编辑:程序博客网 时间:2024/06/14 05:15
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5253
广搜,和普通的优先队列广搜又不太一样,具体看代码吧。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;int N, M;struct node {int x, y, cost;friend bool operator < (node a, node b) {return a.cost > b.cost; //优先队列 }};int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};int map[1010][1010];int vis[1010][1010];int main() {int T;scanf("%d", &T);int kase;for(kase = 1; kase <= T; kase++) {scanf("%d %d", &N, &M);int i, j;for(i = 1; i <= N; i++) {for(j = 1; j <= M; j++) {scanf("%d", &map[i][j]);}}memset(vis, 0, sizeof(vis));priority_queue<node> q;node now, next;now.x = 1, now.y = 1, now.cost = 0;q.push(now);int tot = N * M; // 总点数,用于判退出 int minc = 0;printf("Case #%d:\n", kase);while(!q.empty()) {now = q.top();q.pop();if(vis[now.x][now.y] == 1) continue; //有可能访问到重复的点 tot--;vis[now.x][now.y] = 1;minc += now.cost;if(tot == 0) {printf("%d\n", minc);break;}for(i = 0; i < 4; i++) {next.x = now.x + dir[i][0];next.y = now.y + dir[i][1];if(next.x < 1 || next.x > N || next.y < 1 || next.y > M || vis[next.x][next.y] == 1) {continue;}next.cost = abs(map[next.x][next.y] - map[now.x][now.y]);q.push(next);}}}return 0;}
0 0
- HDU 5253 连接的管道 (bfs + 优先队列)
- hdu5253-连接的管道(优先队列+bfs)
- 5253 连接的管道【最小生成树prim+优先队列】
- hdu 1242 优先队列的bfs
- HDU 1242 (BFS+优先队列)
- HDU 2653 BFS+优先队列
- HDU 1242 BFS+优先队列
- HDU 2653 BFS+优先队列
- hdu 1242 (bfs+优先队列)
- HDU 2822 BFS+优先队列
- hdu 5040 bfs + 优先队列
- hdu 1026 BFS+优先队列
- HDU 1242 BFS+优先队列
- hdu 2822 bfs+优先队列
- HDU 3442 优先队列+BFS
- HDU 3533 BFS+优先队列
- HDU 4308 BFS+优先队列
- HDU 3619 BFS+优先队列
- 理解类加载器
- springmvc实现图片的上传
- 基于Jetsontk1和OpenCV下的人脸检测
- java项目——防止羊毛党“薅羊毛”
- 文章标题
- HDU 5253 连接的管道 (bfs + 优先队列)
- STL 源码剖析 stack
- shiro---你的所见所得由我决定
- 关于Hello Hell项目的经验总结
- STL 源码剖析 queue
- Java:垃圾回收机制
- APP开发之原型图设计
- 1001 Problem A
- 剑指Offer--031-连续子数组的最大和