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 ...
随机推荐
- lintcode-84-落单的数 III
84-落单的数 III 给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到这两个数字. 样例 给出 [1,2,2,3,4,4,5,3],返回 1和5 挑战 O(n)时间复杂度, ...
- 《学习OpenCV》课后习题解答7
题目:(P105) 创建一个结构,结构中包含一个整数,一个CvPoint和一个 CvRect:称结构体为"my_struct". a. 写两个函数:void Write_my_st ...
- C - 最长公共子序列
C - 最长公共子序列 Time Limit: 1000/1000MS (C++/Others) Memory Limit: 65536/65536KB (C++/Others) Problem De ...
- [GitHub] - Unity Timer
https://github.com/akbiggs/UnityTimer#unity-timer Run actions after a delay in Unity3D. This library ...
- Java的同步容器和并发容器
前言: 之前在介绍Java集合的时候说到,java提供的实现类很少是线程安全的.只有几个比较古老的类,比如Vector.Hashtable等是线程安全的,尤其是Hashtable,古老到连命名规范都没 ...
- js计算当前日期上一个月和下一个月
/** * 获取上一个月 * * @date 格式为yyyy-mm-dd的日期,如:2014-01-25 */ funct ...
- elementUI中的el-form怎么使用正则进行验证
http://element.eleme.io/#/zh-CN/component/form里给出了一个form验证的例子,但是都是使用el-form带有的验证规则,怎么使用自己定义的规则进行验证呢? ...
- Notice : brew install php70
To enable PHP in Apache add the following to httpd.conf and restart Apache: LoadModule php7_module ...
- BZOJ1934 [Shoi2007]Vote 善意的投票 【最小割】
题目 幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神.虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法,他们也可以投和自己本来意愿 ...
- AOJ.602 大家来找茬
大家来找茬 Time Limit: 1000 ms Case Time Limit: 1000 ms Memory Limit: 64 MB Total Submission: 627 Submiss ...