Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8054 Accepted Submission(s): 2293
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 Output
hopeless
hopeless
winnable
winnable
大致题意:最近,有人发现了如何在Y-Crate游戏设备上运行开源软件。许多有进取心的设计师开发了Advent风格的游戏,可以在Y-Crate上进行部署。你的工作是测试一些设计,看看哪些是可赢的。
每场比赛由多达100个房间组成。其中一个房间是起点,其中一个房间是终点。每个房间的能量值介于-100和+100之间。对于这些房间,两两之间有单向门连接。
玩家在起点时有100点能量。她可以通过任何连接她所在房间的单向门到另一个房间。这个房间的能量值会增加到玩家的能量储蓄。这个过程将一直持续到她进入终点获胜或因能量耗尽而死亡(或者沮丧地退出)。在她的冒险中,玩家可以多次进入同一个房间,每次都能获得房间所提供的能量。
解题思路:找最长路即可
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
#define stdio ios::sync_with_stdio(false)
#define met(a, b) memset(a, b, sizeof(a))
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e2 + 10;
int idx = 0;
int vis[MAXN], dis[MAXN], cnt[MAXN], head[MAXN];//vis标记数组 cnt数组记录房间一共走过多少次
using namespace std;
struct Edgenode
{
int to, w, next;
}edges[MAXN * MAXN];
struct SPFA
{
void init(int n)//初始化函数
{
idx = 0;
met(vis, 0);
met(cnt, 0);
met(head, -1);
met(dis, -0x3f);
return ;
}
void add(int u, int v, int w)//加边
{
edges[idx].w = w;
edges[idx].to = v;
edges[idx].next = head[u];
head[u] = idx;
idx++;
return ;
}
int spfa(int n)
{
dis[1] = 100;//房间初始能量100
vis[1] = 1;
queue<int> Q;
Q.push(1);
while (!Q.empty())
{
int q = Q.front(); Q.pop();
cnt[q]++;
vis[q] = 0;
if (q == n)//走到最后一个房间
return 1;
if (cnt[q] >= n + 1)//当一个房间使用大于N-1次时即可判定有环存在
continue;
if (cnt[q] >= n)
dis[q] = INF;//有正权环的情况下直接将权值赋为最大值
for (int i = head[q]; i != -1; i = edges[i].next)
{
int to = edges[i].to;
if (dis[to] < dis[q] + edges[i].w && dis[q] + edges[i].w > 0)//要找最长路 并且经过每一个房间能量都必须大于0
{
dis[to] = dis[q] + edges[i].w;
if (!vis[to])
vis[to] = 1, Q.push(to);
}
}
}
return -1;
}
}sp;
int main()
{
int n;
while (cin >> n && n != -1)
{
sp.init(n);
for (int i = 1; i <= n; i++)
{
int w, m;
cin >> w >> m;
while (m--)
{
int to;
cin >> to;
sp.add(i, to, w);
}
}
int flag = sp.spfa(n);
if (flag == 1)
cout << "winnable" << endl;
else
cout << "hopeless" << endl;
}
return 0;
}