hdu 1317 XYZZY (bellman_ford+floyd)

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

《hdu 1317 XYZZY (bellman_ford+floyd)》

 

第二组的图:输出winnable

《hdu 1317 XYZZY (bellman_ford+floyd)》

 

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

 

    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/Rainbow_storm/article/details/81263440
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞