hdu 1317 XYZZY (bellman_ford+floyd)

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

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;
}

 

"Mstar Bin Tool"是一款专门针对Mstar系列芯片开发的固件处理软件,主要用于智能电视及相关电子设备的系统维护与深度定制。该工具包特别标注了"LETV USB SCRIPT"模块,表明其对乐视品牌设备具有兼容性,能够通过USB通信协议执行固件读写操作。作为一款专业的固件编辑器,它允许技术人员对Mstar芯片的底层二进制文件进行解析、修改与重构,从而实现系统功能的调整、性能优化或故障修复。 工具包中的核心组件包括固件编译环境、设备通信脚本、操作界面及技术文档等。其中"letv_usb_script"是一套针对乐视设备的自动化操作程序,可指导用户完成固件烧录全过程。而"mstar_bin"模块则专门处理芯片的二进制数据文件,支持固件版本的升级、降级或个性化定制。工具采用7-Zip压缩格式封装,用户需先使用解压软件提取文件内容。 操作前需确认目标设备采用Mstar芯片架构并具备完好的USB接口。建议预先备份设备原始固件作为恢复保障。通过编辑器修改固件参数时,可调整系统配置、增删功能模块或修复已知缺陷。执行刷机操作时需严格遵循脚本指示的步骤顺序,保持设备供电稳定,避免中断导致硬件损坏。该工具适用于具备嵌入式系统知识的开发人员或高级用户,在进行设备定制化开发、系统调试或维护修复时使用。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值