hdu 1932(spfa)
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 3694 | Accepted: 1059 |
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
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
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)的更多相关文章
- hdu 4568(SPFA预处理+TSP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4568 思路:先用spfa预处理出宝藏与宝藏之间的最短距离,宝藏到边界的最短距离,然后就是经典的求TSP ...
- HDU 1317XYZZY spfa+判断正环+链式前向星(感觉不对,但能A)
XYZZY Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- HDU 4568 SPFA + TSP
这道题是长沙邀请赛的题,当时是道签到题. 这种题还是很常见的,讲一下思路. 首先是预处理出每个宝藏之间的距离,还有到边的距离,直接对每个宝藏进行一次SPFA就可以了. 然后就是经典的求TSP的过程. ...
- Key Vertex (hdu 3313 SPFA+DFS 求起点到终点路径上的割点)
Key Vertex Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tota ...
- HDU 6166 Spfa
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6166 题意:给出一个n个点的有向图.然后给你k个点,求这k个点任意两点之间的最短路的最小值.思路: 以 ...
- HDU 1535 SPFA 前向星存图优化
Invitation Cards Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 1874 SPFA/Dijkstra/Floyd
这题作为模板题,解法好多... 最近周围的人都在搞图论阿,感觉我好辣鸡,只会跟风学习. 暂时只有SPFA和Dijkstra的 SPFA (邻接表版.也可以写成临接矩阵存图,但题目可能给出平行边的,所以 ...
- hdu 3768(spfa+暴力)
Shopping Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- hdu 3790(SPFA)
最短路径问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
随机推荐
- IDEA使用maven构建时控制台中文乱码的解决办法
使用maven clean install 项目时控制台中文乱码,解决办法如下: Setting->maven->runner VMoptions: -Dfile.encoding=UTF ...
- Java 无法初始化Connection的问题
通过断点调试捕获错误消息:The server time zone value '�й���ʱ��' is unrecognized or represents more than one time ...
- java正则表达式 1 -- 符号
正则表达式主要是用于操作字符串的规则 1 首先体验一下正则表达式: 需求:某个串只能是数字 传统方法: public class Demo2{ public static void main(Stri ...
- java--List转换成json格式
方法一 首先导入jar包,json-rpc-1.0.jar public class List2Json { public static JSONArray ProLogList2Json(List& ...
- WEB-INF目录
背景: 在项目中,使用 "${pageContext.request.contextPath}/image/01.jpg"获取不到该图片.在浏览器中直接输入地址也找不到,报错404 ...
- 段寻址*****************************TBD
fffff880`01b05be1 ff9708020000 call qword ptr [rdi+208h] ds:002b:fffff980`0554ae88=fffffa8004b ...
- vue2.0中改变了数组值不能实时反映到页面
页面中点击事件checkContent,改变row数组中的row[99]的值,如果注释更改,那么页面是不能实时获取的,如图更改,则可以 具体原理:http://blog.csdn.net/websof ...
- 教你一步一步在linux中正确的安装Xcache加速php
#第一步,下载Xcache wget http://xcache.lighttpd.net/pub/Releases/3.1.0/xcache-3.1.0.tar.gz #第一步非常简单,如果你下载不 ...
- Mac OSX 10.11安装Jekyll
一说常见的博客管理工具大家想到的就是WordPress.不过现在部分个人博客用户开始从WordPress转移到Jekyll上了.Jekyll是一种本地生成静态页面进而线上发布的博客工具,而且现在已经有 ...
- [C/C++] C++常见面试题
参考:http://blog.csdn.net/shihui512/article/details/9092439 1.new.delete.malloc.free之间的关系 malloc和free都 ...