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

 

内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值