【HDU】5025 Saving Tang Monk 状压最短路

2024-09-05 14:48

本文主要是介绍【HDU】5025 Saving Tang Monk 状压最短路,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

传送门:【HDU】5025 Saving Tang Monk


题目分析:这题一开始想都没想就敲了优先队列+dij。。然后TLE了。。。

后来发现是一个稀疏图。。换成spfa就过了。。

这是一个四维最短路,x轴,y轴,拿到第几个钥匙,打怪的状态(状压),然后就是很简单的状压最短路了。

要注意到钥匙不用状压,因为拿到钥匙是有顺序的。


代码如下:


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std ;typedef long long LL ;#define rep( i , a , b ) for ( int i = a ; i < b ; ++ i )
#define FOR( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REV( i , a , b ) for ( int i = a ; i >= b ; -- i )
#define travel( e , H , u ) for ( Edge* e = H[u] ; e ; e = e -> next )
#define clr( a , x ) memset ( a , x , sizeof a )
#define CPY( a , x ) memcpy ( a , x , sizeof a )
#define ls ( o << 1 )
#define rs ( o << 1 | 1 )
#define lson ls , l , m
#define rson rs , m + 1 , r
#define mid ( ( l + r ) >> 1 )
#define root 1 , 1 , n
#define rt o , l , rconst int MAXN = 105 ;
const int MAXQ = 3000005 ;
const int INF = 0x3f3f3f3f ;struct Node {int x , y , u , s ;Node () {}Node ( int x , int y , int u , int s ) : x ( x ) , y ( y ) , u ( u ) , s ( s ) {}
} Q[MAXQ] ;int head , tail ;
char G[MAXN][MAXN] ;
int d[MAXN][MAXN][10][33] ;
int map[MAXN][MAXN] ;
bool vis[MAXN][MAXN][10][33] ;
int path[4][2] = { { 1 , 0 } , { -1 , 0 } , { 0 , 1 } , { 0 , -1 } } ;
int sx , sy ;
int ex , ey ;
int n , m ;int dijkstra () {int ans = INF ;clr ( d , INF ) ;head = tail = 0 ;d[sx][sy][0][0] = 0 ;Q[tail ++] = Node ( sx , sy , 0 , 0 ) ;while ( head != tail ) {Node now = Q[head ++] ;if ( head == MAXQ ) head = 0 ;if ( now.u == m && now.x == ex && now.y == ey ) {ans = min ( ans , d[now.x][now.y][now.u][now.s] ) ;continue ;}vis[now.x][now.y][now.u][now.s] = 0 ;rep ( i , 0 , 4 ) {int nx = now.x + path[i][0] ;int ny = now.y + path[i][1] ;if ( nx < 0 || nx >= n || ny < 0 || ny >= n || G[nx][ny] == '#' ) continue ;int nu = now.u ;int ns = now.s ;int nd = 1 ;if ( G[nx][ny] == 'S' ) {if ( ! ( ns & ( 1 << map[nx][ny] ) ) {ns |= ( 1 << map[nx][ny] ) ;++ nd ;}}if ( G[nx][ny] <= '9' && G[nx][ny] > '0' ) {int tmp = G[nx][ny] - '0' ;if ( tmp == nu + 1 ) ++ nu ;}if ( d[nx][ny][nu][ns] > d[now.x][now.y][now.u][now.s] + nd ) {d[nx][ny][nu][ns] = d[now.x][now.y][now.u][now.s] + nd ;if ( !vis[nx][ny][nu][ns] ) {Q[tail ++] = Node ( nx , ny , nu , ns ) ;if ( tail == MAXQ ) tail = 0 ;vis[nx][ny][nu][ns] = 1 ;}}}}return ans == INF ? -1 : ans ;
}void solve () {int cnt = 0 ;clr ( vis , 0 ) ;rep ( i , 0 , n ) {scanf ( "%s" , G[i] ) ;rep ( j , 0 , n ) {if ( G[i][j] == 'K' ) {sx = i ;sy = j ;} else if ( G[i][j] == 'T' ) {ex = i ;ey = j ;} else if ( G[i][j] == 'S' ) {map[i][j] = cnt ++ ;}}}int step = dijkstra () ;if ( ~step ) printf ( "%d\n" , step ) ;else printf ( "impossible\n" ) ;
}int main () {while ( ~scanf ( "%d%d" , &n , &m ) && ( n || m ) ) solve () ;return 0 ;
}


这篇关于【HDU】5025 Saving Tang Monk 状压最短路的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1139262

相关文章

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 1502 MPI Maelstrom(单源最短路dijkstra)

题目真是长得头疼,好多生词,给跪。 没啥好说的,英语大水逼。 借助字典尝试翻译了一下,水逼直译求不喷 Description: BIT他们的超级计算机最近交货了。(定语秀了一堆词汇那就省略吧再见) Valentine McKee的研究顾问Jack Swigert,要她来测试一下这个系统。 Valentine告诉Swigert:“因为阿波罗是一个分布式共享内存的机器,所以它的内存访问

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

poj 3159 (spfa差分约束最短路) poj 1201

poj 3159: 题意: 每次给出b比a多不多于c个糖果,求n最多比1多多少个糖果。 解析: 差分约束。 这个博客讲差分约束讲的比较好: http://www.cnblogs.com/void/archive/2011/08/26/2153928.html 套个spfa。 代码: #include <iostream>#include <cstdio>#i