XYZZY
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 3694   Accepted: 1059

Description

The prototypical computer adventure game, first designed by Will Crowther on the PDP-10 in the mid-1970s as an attempt at computer-refereed fantasy gaming, and expanded into a puzzle-oriented game by Don Woods at Stanford in 1976. (Woods had been one of the authors of INTERCAL.) Now better known as Adventure or Colossal Cave Adventure, but the TOPS-10 operating system permitted only six-letter filenames in uppercase. See also vadding, Zork, and Infocom.

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

题意:某人从 1走到 n,初始值为100,经过的每个点都有一个能量值,他走到每个点时会增加或者减少这个点的能量值,但是都不能小于 0 ,问他最后能否走到第n个点?
题解:spfa 判断正环,如果某个点的入队次数>n,那么则证明这个数赋值 INF ,然后这个点就不能进入了,然后每一次要判断是否大于 0
#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
const int INF = ;
const int N = ;
const int M = N*N;
struct Edge{
int v,next;
}edge[M];
int head[N];
int tot;
int n;
int val[N];
void addEdge(int u,int v,int &k){
edge[k].v = v,edge[k].next = head[u],head[u] = k++;
}
void init(){
memset(head,-,sizeof(head));
tot = ;
}
int low[N],time[N],pre[N];
bool vis[N];
bool spfa(int s){
for(int i=;i<=n;i++){
vis[i] = false;
low[i] = -INF;
time[i] = ;
pre[i] = i;
}
low[s] = ;
time[s]++;
queue<int> q;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
if(time[u]>n) low[u] = INF;
for(int k = head[u];k!=-;k=edge[k].next){
int v = edge[k].v;
if(low[v]<low[u]+val[v]&&low[u]+val[v]>){
low[v] = low[u]+val[v];
if(!vis[v]&&time[v]<=n){
vis[v] = true;
q.push(v);
time[v]++;
}
}
}
}
int temp = n;
if(low[n]<=) return false;
return true;
}
int main(){
while(scanf("%d",&n)!=EOF,n!=-){
init();
int num;
for(int u=;u<=n;u++){
scanf("%d%d",&val[u],&num);
for(int j=;j<=num;j++){
int v;
scanf("%d",&v);
addEdge(u,v,tot);
}
}
if(spfa()) printf("winnable\n");
else printf("hopeless\n");
}
return ;
}

hdu 1932(spfa)的更多相关文章

  1. hdu 4568(SPFA预处理+TSP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4568 思路:先用spfa预处理出宝藏与宝藏之间的最短距离,宝藏到边界的最短距离,然后就是经典的求TSP ...

  2. HDU 1317XYZZY spfa+判断正环+链式前向星(感觉不对,但能A)

    XYZZY Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  3. HDU 4568 SPFA + TSP

    这道题是长沙邀请赛的题,当时是道签到题. 这种题还是很常见的,讲一下思路. 首先是预处理出每个宝藏之间的距离,还有到边的距离,直接对每个宝藏进行一次SPFA就可以了. 然后就是经典的求TSP的过程. ...

  4. Key Vertex (hdu 3313 SPFA+DFS 求起点到终点路径上的割点)

    Key Vertex Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tota ...

  5. HDU 6166 Spfa

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6166 题意:给出一个n个点的有向图.然后给你k个点,求这k个点任意两点之间的最短路的最小值.思路: 以 ...

  6. HDU 1535 SPFA 前向星存图优化

    Invitation Cards Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  7. HDU 1874 SPFA/Dijkstra/Floyd

    这题作为模板题,解法好多... 最近周围的人都在搞图论阿,感觉我好辣鸡,只会跟风学习. 暂时只有SPFA和Dijkstra的 SPFA (邻接表版.也可以写成临接矩阵存图,但题目可能给出平行边的,所以 ...

  8. hdu 3768(spfa+暴力)

    Shopping Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  9. hdu 3790(SPFA)

    最短路径问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

随机推荐

  1. D - 小木棒

    D - 小木棒 Time Limit: 1000/1000MS (C++/Others) Memory Limit: 65536/65536KB (C++/Others) Problem Descri ...

  2. PAT 1084 外观数列

    https://pintia.cn/problem-sets/994805260223102976/problems/994805260583813120 外观数列是指具有以下特点的整数序列: d, ...

  3. Vue组件间通信:一个例子学会Vue组件-Vue.js学习总结)(转载)

    详情请点击 http://www.jianshu.com/p/9ad1ba89a04b

  4. HTML5表单提交与PHP环境搭建

    PHP服务器使用xampp集成套件 路径 D:\xampp\htdocs\MyServer\index.php 访问 http://localhost/MyServer/index.php 能够正常显 ...

  5. [洛谷P4238]【模板】多项式求逆

    题目大意:多项式求逆 题解:$ A^{-1}(x) = (2 - B(x) * A(x)) \times B(x) \pmod{x^n} $ ($B(x)$ 为$A(x)$在$x^{\lceil \d ...

  6. Phaser的timer用法

    1. 延迟timer,相当于setTimeout game.time.events.add(Phaser.Timer.SECOND*5,this.delayOver,this); 2. 循环timer ...

  7. Elasticsearch 5.2.1Cluster 搭建

    1.安装java cd ~ wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fw ...

  8. auto login vpnclient bat

    @echo offstart "" /b "C:\Program Files (x86)\Cisco Systems\VPN Client\vpngui.exe" ...

  9. javascript中Date使用总结(转)

    //全局函数 Date //Date 类的静态方法 Date.parse Date.UTC //Date 对象的建立方法 new Date() new Date(毫秒数) new Date(标准时间格 ...

  10. Eclipse Jetty调试时无法保存js文件

    Jetty会使用内存映射文件来缓存静态文件,包括js,css文件. 在Windows下,使用内存映射文件会导致文件被锁定,所以当Jetty启动的时候无法在编辑器对js或者css文件进行编辑. 解决办法 ...