HDU-1317 XYZZY

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个房间,每个房间都有一个能量值(可正可负),当走进这个房间时游戏角色的能量值将会加上这个房间的能量。如果游戏角色能从起点(编号
为1)走到终点(编号为n),则这款游戏合格,否则不合格。能量值为正时才可行走。初始能量值为100,现在已经告知了每个房间的能量值和与之相连的房间。
题目解析:单源最长路。看到终点n的最长路是否为正,用floyd算法,但存在正环时要加以判断,当某个节点进入队列的次数为n时,一定存在正环,则将其的dis置为INF。还要注意当某个节点已经在队列中,无须再次将其加入队列。
 
代码如下:
 1 # include<iostream>
 2 # include<cstdio>
 3 # include<algorithm>
 4 # include<cstring>
 5 # include<queue>
 6 using namespace std;
 7 const int INF=1<<29;
 8 struct edge
 9 {
10     int to,w,nxt;
11 };
12 edge e[5500];
13 int mp[105][105],ey[105],inque[105],outque[105];
14 int n,head[105],cnt,dis[105];
15 void add(int u,int v,int w)
16 {
17     e[cnt].to=v;
18     e[cnt].w=w;
19     e[cnt].nxt=head[u];
20     head[u]=cnt++;
21 }
22 void maketree()
23 {
24     cnt=0;
25     memset(head,-1,sizeof(head));
26     for(int i=1;i<=n;++i){
27         for(int j=1;j<=n;++j){
28             if(i==j)
29                 continue;
30             if(mp[i][j])
31                 add(i,j,ey[j]);
32         }
33     }
34 }
35 bool spfa()
36 {
37     fill(dis,dis+n+1,-INF);
38     fill(inque,inque+n+1,0);
39     fill(outque,outque+n+1,0);
40     queue<int>q;
41     q.push(1);
42     dis[1]=100;
43     inque[1]=1;
44     while(!q.empty())
45     {
46         int u=q.front();
47         q.pop();
48         inque[u]=0;
49         ++outque[u];
50         if(outque[u]>n)
51             continue;
52         if(outque[u]==n)
53             dis[u]=INF;
54         for(int i=head[u];i!=-1;i=e[i].nxt){
55             if(dis[e[i].to]<dis[u]+e[i].w&&dis[u]+e[i].w>0){
56                 dis[e[i].to]=dis[u]+e[i].w;
57                 if(e[i].to==n)
58                     return true;
59                 else if(!inque[e[i].to]){
60                     inque[e[i].to]=1;
61                     q.push(e[i].to);
62                 }
63             }
64         }
65     }
66     return false;
67 }
68 int main()
69 {
70     while(scanf("%d",&n))
71     {
72         if(n==-1)
73             break;
74         int a,b;
75         memset(mp,0,sizeof(mp));
76         for(int i=1;i<=n;++i){
77             scanf("%d%d",ey+i,&a);
78             while(a--){
79                 scanf("%d",&b);
80                 mp[i][b]=1;
81             }
82         }
83         maketree();
84         if(spfa())
85             printf("winnable\n");
86         else
87             printf("hopeless\n");
88     }
89     return 0;
90 }
View Code

 

 
posted @ 2015-07-28 08:59  20143605  阅读(397)  评论(0编辑  收藏  举报