hdu 1317 XYZZY (bellman_ford+floyd)

本文介绍了一种基于图论的游戏迷宫挑战算法设计,利用Floyd-Warshall算法判断可达性,并借助Bellman算法检测能量增益环路,确保玩家能够在特定条件下完成游戏。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

XYZZY

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7069    Accepted Submission(s): 2022


 

Problem Description

It has recently been discovered how to run open-source software on the Y-Crate gaming device. A number of enterprising designers have developed Advent-style games for deployment on the Y-Crate. Your job is to test a number of these designs to see which are winnable.
Each game consists of a set of up to 100 rooms. One of the rooms is the start and one of the rooms is the finish. Each room has an energy value between -100 and +100. One-way doorways interconnect pairs of rooms.

The player begins in the start room with 100 energy points. She may pass through any doorway that connects the room she is in to another room, thus entering the other room. The energy value of this room is added to the player's energy. This process continues until she wins by entering the finish room or dies by running out of energy (or quits in frustration). During her adventure the player may enter the same room several times, receiving its energy each time.

 

 

Input

The input consists of several test cases. Each test case begins with n, the number of rooms. The rooms are numbered from 1 (the start room) to n (the finish room). Input for the n rooms follows. The input for each room consists of one or more lines containing:

the energy value for room i
the number of doorways leaving room i
a list of the rooms that are reachable by the doorways leaving room i
The start and finish rooms will always have enery level 0. A line containing -1 follows the last test case.

 

 

Output

In one line for each case, output "winnable" if it is possible for the player to win, otherwise output "hopeless".

 

 

Sample Input

5
0 1 2
-60 1 3
-60 1 4
20 1 5
0 0
5
0 1 2
20 1 3
-60 1 4
-60 1 5
0 0
5
0 1 2
21 1 3
-60 1 4
-60 1 5
0 0
5
0 1 2
20 2 1 3
-60 1 4
-60 1 5
0 0
-1

 

Sample Outputhopeless

 

hopeless

hopeless

winnable
winnable

 

Source

University of Waterloo Local Contest 2003.09.27

 

 

 

题意:有N个房间,房间之间能够通过连接的通道到达,这些道路是单向的,也就是有向图,每一个房间有一个能量值,范围是[-100,100],房间1和房间N的能量值为0,我们初始身上有100能量,每到达一个房间,身上的能量值就加上这个房间的能量值,房间能够重复进入。如果我们从当前房间A前往目标房间B,身上的能量值加上房间B的能量值不是正数,则不能到达目标房间B,问题是我们能否从房间1出发最后到达房间N。如果能输出winnable,不能则输出hopeless。

思路:首先我们需要检查是否存在这样的路从1到达N,如果不存在,直接输出hopeless。我们可以利用floyd算法去检查。Floyd-Warshall算法是一种在具有正或负边缘权重(但没有负周期)的加权图中找到最短路径的算法。因为题中可能会出现负周期——行成环路,可以无限增加能量值。但是可以用于检查1到N是否连通。然后我们再使用Bellman算法,如果松弛N-1次后,任然存在更新。说明图中存在负周期,说明能量可以无限叠加,检查环路的点是否与N连通就行了。如果不存在负周期,则检查到达N的能量是否大于0。

附上两组测试数据:

7
0 2 2 3
-101 1 7
1 1 4
1 1 6
1 1 4
1 1 5
0 0
7
0 2 2 3
-101 1 7
1 2 1 4
1 2 3 6
1 1 4
1 1 5
0 0

第一组的图:输出hopeless

 

第二组的图:输出winnable

 

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int N,M;
const int MAXN =105;
const int INF = 0x3f3f3f3f;
/*
	d数组表示到达i房间的最大的能量值;
	p数组表示每个房间的能量值;
	G数组存图。G[i][j]=1,表示i到j有1条通路。
	E数组存边。
*/
struct edge{
    int from,to;
}E[MAXN*MAXN];
int d[MAXN],P[MAXN];
int G[MAXN][MAXN];
bool floyd(){
	for(int k=1;k<=N;k++)
		for(int i=1;i<=N;i++)
			for(int j=1;j<=N;j++)
				if(G[i][k]&&G[k][j])
					G[i][j]=1;
	return G[1][N];
}

bool bellman(){
    fill(d,d+MAXN,-INF);
    d[1]=100;
    for(int i=1;i<N;i++){
        bool flag=false;
        for(int j=0;j<M;j++){
            if(d[E[j].to] < d[E[j].from]+P[E[j].to] && d[E[j].from]+P[E[j].to]>0){
                flag=true;
                d[E[j].to]=d[E[j].from]+P[E[j].to];
            }
        }
        if(!flag) break;
    }
	for(int j=0;j<M;j++){
        if(d[E[j].to] < d[E[j].from]+P[E[j].to] && d[E[j].from]+P[E[j].to]>0){
            d[E[j].to]=d[E[j].from]+P[E[j].to];
			if(G[E[j].to][N]){
				return true;
			}
		}
    }
	return d[N]>0;
}

int main(){
    while(~scanf("%d",&N)&&(N!=-1)){
		memset(G,0,sizeof(G));
		memset(E,0,sizeof(E));
		memset(P,0,sizeof(P));
		M=0;
		int con;//连接多少扇门
        for(int i=1;i<=N;i++){
			scanf("%d%d",&P[i],&con);
			for(int j=1;j<=con;j++){
				int tmp;scanf("%d",&tmp);
				E[M++]=(edge){i,tmp};			
				G[i][tmp]=1;
			}
        }
		if(!floyd()){
			printf("hopeless\n");
			continue;
		}
		if(bellman()){
			printf("winnable\n");
		}else{
			printf("hopeless\n");
		}
    }
    return 0;
}

 

分数阶傅里叶变换(Fractional Fourier Transform, FRFT)是对传统傅里叶变换的拓展,它通过非整数阶的变换方式,能够更有效地处理非线性信号以及涉及时频局部化的问题。在信号处理领域,FRFT尤其适用于分析非平稳信号,例如在雷达、声纳和通信系统中,对线性调频(Linear Frequency Modulation, LFM)信号的分析具有显著优势。LFM信号是一种频率随时间线性变化的信号,因其具有宽频带和良好的时频分辨率,被广泛应用于雷达和通信系统。FRFT能够更精准地捕捉LFM信号的时间和频率信息,相比普通傅里叶变换,其性能更为出色。 MATLAB是一种强大的数值计算和科学计算工具,拥有丰富的函数库和用户友好的界面。在MATLAB中实现FRFT,通常需要编写自定义函数或利用信号处理工具箱中的相关函数。例如,一个名为“frft”的文件可能是用于执行分数阶傅里叶变换的MATLAB脚本或函数,并展示其在信号处理中的应用。FRFT的正确性验证通常通过对比变换前后信号的特性来完成,比如评估信号的重构质量、信噪比等。具体而言,可以通过计算原始信号与经过FRFT处理后的信号之间的相似度,或者对比LFM信号的关键参数(如初始频率、扫频率和持续时间)是否在变换后得到准确恢复。 在MATLAB代码实现中,通常包含以下步骤:首先,生成LFM信号模型,设定其初始频率、扫频率、持续时间和采样率等参数;其次,利用自定义的frft函数对LFM信号进行分数阶傅里叶变换;接着,使用MATLAB的可视化工具(如plot或imagesc)展示原始信号的时域和频域表示,以及FRFT后的结果,以便直观对比;最后,通过计算均方误差、峰值信噪比等指标来评估FRFT的性能。深入理解FRFT的数学原理并结合MATLAB编程技巧,可以实现对LFM信号的有效分析和处理。这个代码示例不仅展示了理论知识在
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值