您的位置:首页 > 其它

XYZZY(spfa判环)

2015-11-10 23:17 330 查看
B - XYZZY

Time Limit:1000MS Memory Limit:32768KB

64bit IO Format:%I64d & %I64u

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 Output

hopeless

hopeless

winnable

winnable

题目大意:

有n个房间,一些房间之间是连通的,一个人可以经过一个房间多次,进入房间之间每个人有100体力值,从1号房间进入,每个房间对应一个体力值(1号房间和n号房间体力值为0),进入该房间就加上该体力值,当总体力值小于0时游戏失败,保持总体力值一直大于0并到达n号房间,游戏胜利。

思路:

经过分析题意,可基本确定用最短路的知识解决,对此题,转变为求最长路,用spfa,走一遍最长路,若体力值大于0则游戏胜利,需要注意的地方有,最后求得的最长路体力值大于0,但途中有小于0的情况,是不行的。其次,如果存在环,那么spfa会一直循环,所以需要加以限制,判断环的方法为:若一个点入队n次,就代表有环。且此环为正环,体力值就会加到无穷大,并且该环能到终点,那么游戏胜利。

spfa:

#include<iostream>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<cstdio>
#include<queue>
#define inf 0x3f3f3f3f
#define maxn 110
using namespace std;
struct node
{
int en;
int len;
int next;
};
int num;
int head[maxn];
node E[maxn*maxn];
bool vis[maxn];
int dis[maxn];
int num1[maxn];
int numm[maxn];
int n;
void init()
{
num=0;
memset(head,-1,sizeof(head));
}
void add(int st,int en)
{
E[num].en=en;
E[num].next=head[st];
head[st]=num++;
}
bool spfa()
{
memset(vis,false,sizeof(vis));
memset(dis,-inf,sizeof(dis));
memset(num1,0,sizeof(num1));
queue<int> q;
vis[1]=true;
dis[1]=100;
q.push(1);
while(!q.empty())
{
int xx=q.front();
q.pop();
vis[xx]=false;
num1[xx]++;//记录该点入队次数
if(num1[xx]>n)
continue;//在环中的点不再入队,否则一直循环
if(num1[xx]==n)
dis[xx]=inf;//若成环则体力值为无穷大
for(int i=head[xx];i!=-1;i=E[i].next)
{
int ed=E[i].en;
if(dis[ed]<dis[xx]+numm[ed]&&dis[xx]+numm[ed]>0)
{//保证途中体力值也大于0
dis[ed]=dis[xx]+numm[ed];
if(ed==n)
return true;
if(!vis[ed])
{
vis[ed]=true;
q.push(ed);
}
}
}
}
return false;
}
int main()
{
int i,j,k,a,b;
while(scanf("%d",&n)!=EOF)
{
if(n==-1)
break;
init();
for(i=1;i<=n;i++)
{
scanf("%d%d",&numm[i],&b);
for(j=1;j<=b;j++)
{scanf("%d",&k);add(i,k);}
}
if(spfa())
printf("winnable\n");
else
printf("hopeless\n");
}
return 0;
}


ps:这道题改了很多遍都是WA,查了几份代码,最后把输入数据的代码和SPFA的代码改了一小部分,就过了,此题还需思考一下
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: