XYZZY
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4421 Accepted Submission(s): 1252
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.
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.
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
hopeless
winnable
winnable
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
int n;
int energy[maxn];
int power[maxn]; //记录到每一点的力量值
int cnt[maxn]; //记录访问每一个点的次数
bool g[maxn][maxn];
bool reach[maxn][maxn];
void floyd()
{
for(int k = ; k<=n; k++)
for(int i = ; i<=n; i++)
for(int j = ; j<=n; j++)
if(reach[i][j]||(reach[i][k]&&reach[k][j])) reach[i][j] = true;
}
bool spfa(int s)
{
queue<int> q;
memset(power,,sizeof(power));
power[s] = ;
memset(cnt,,sizeof(power));
q.push(s);
while(!q.empty())
{
int v = q.front();
q.pop();
cnt[v]++;
if(cnt[v]>=n) return reach[v][n]; //有正环
for(int i = ; i<=n; i++)
{
if(g[v][i]&&power[i]<power[v]+energy[i]&&power[v]+energy[i]>)
{
power[i] = power[v]+energy[i]; q.push(i);
}
}
}
return power[n]>; }
void solve()
{
while(scanf("%d",&n)!=EOF&&n!=-)
{
memset(g,false,sizeof(g));
memset(reach,false,sizeof(reach));
for(int i = ; i<=n; i++)
{
int k,door;
scanf("%d %d",&energy[i],&k);
for(int j = ; j<=k; j++)
{
scanf("%d",&door);
g[i][door] = true;
reach[i][door] = true;
}
}
floyd();
if(!reach[][n])
{
printf("hopeless\n");
continue;
}
if(spfa())
{
printf("winnable\n");
}
else
{
printf("hopeless\n");
}
}
}
int main()
{
solve();
return ;
}