XYZZY

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5304    Accepted Submission(s): 1510
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
 思路:
单向路径。判断是否存在正环,初始化距离数组为负无穷小,进入n次,说明存在正环,将距离改为无穷大。进入n+1次,直接跳过。
代码:
 #include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=;
const int maxm=;
const int INF=0x3f3f3f3f;
struct edgenode {
int to,w,next;
}edges[maxm];
bool vis[maxn];
int dist[maxn],du[maxn],head[maxn];
int n,cnt;
void init() {
for(int i=;i<maxn;++i) head[i]=-;
for(int i=;i<maxm;++i) edges[i].next=-;
cnt=;
}
void addedge(int u, int v, int w) {
edges[cnt].to=v;
edges[cnt].w=w;
edges[cnt].next=head[u];
head[u]=cnt++;
}
bool spfa() {
memset(vis,false,sizeof(vis));
memset(du,,sizeof(du));
for(int i=;i<maxn;++i) dist[i]=-INF;
queue<int> q;
dist[]=;vis[]=true;
q.push();
while(!q.empty()) {
int now=q.front();q.pop();
vis[now]=false;
du[now]++;
if(du[now]>n) continue;
if(du[now]==n) dist[now]=INF;
for(int i=head[now];~i;i=edges[i].next) {
if(dist[edges[i].to]<dist[now]+edges[i].w&&dist[now]+edges[i].w>) {
dist[edges[i].to]=dist[now]+edges[i].w;
if(edges[i].to==n) return true;
if(!vis[edges[i].to]) {
vis[edges[i].to]=true;
q.push(edges[i].to);
}
}
}
}
return false;
}
int main() {
while(scanf("%d",&n)&&n!=-) {
int w,num,id;
init();
for(int i=;i<=n;++i) {
scanf("%d%d",&w,&num);
for(int j=;j<=num;++j) {
scanf("%d",&id);
addedge(i,id,w);
}
}
if(spfa()) printf("winnable\n");
else printf("hopeless\n");
}
return ;
}

HDU 1317XYZZY spfa+判断正环+链式前向星(感觉不对,但能A)的更多相关文章

  1. HDU 2544最短路 【dijkstra 链式前向星+优先队列优化】

    最开始学最短路的时候只会用map二维数组存图,那个时候还不知道这就是矩阵存图,也不懂得效率怎么样 经过几个月的历练再回头看最短路的题, 发现图可以用链式前向星来存, 链式前向星的效率是比较高的.对于查 ...

  2. Currency Exchange POJ - 1860 (spfa判断正环)

    Several currency exchange points are working in our city. Let us suppose that each point specializes ...

  3. Currency Exchange POJ - 1860 spfa判断正环

    //spfa 判断正环 #include<iostream> #include<queue> #include<cstring> using namespace s ...

  4. 单元最短路径算法模板汇总(Dijkstra, BF,SPFA),附链式前向星模板

    一:dijkstra算法时间复杂度,用优先级队列优化的话,O((M+N)logN)求单源最短路径,要求所有边的权值非负.若图中出现权值为负的边,Dijkstra算法就会失效,求出的最短路径就可能是错的 ...

  5. SPFA + 链式前向星(详解)

    求最短路是图论中最基础的算法,最短路算法挺多,本文介绍SPFA算法. 关于其他最短路算法,请看我另一篇博客最短路算法详解 链式前向星概念 简单的说,就是存储图的一个数据结构.它是按照边来存图,而邻接矩 ...

  6. 最短路 spfa 算法 && 链式前向星存图

    推荐博客  https://i.cnblogs.com/EditPosts.aspx?opt=1 http://blog.csdn.net/mcdonnell_douglas/article/deta ...

  7. POJ 3169 Layout(差分约束+链式前向星+SPFA)

    描述 Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 ...

  8. 链式前向星+SPFA

    今天听说vector不开o2是数组时间复杂度常数的1.5倍,瞬间吓傻.然后就问好的图表达方式,然后看到了链式前向星.于是就写了一段链式前向星+SPFA的,和普通的vector+SPFA的对拍了下,速度 ...

  9. 【模板】链式前向星+spfa

    洛谷传送门--分糖果 博客--链式前向星 团队中一道题,数据很大,只能用链式前向星存储,spfa求单源最短路. 可做模板. #include <cstdio> #include <q ...

随机推荐

  1. LeetCode 280. Wiggle Sort (摆动排序)$

    Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...

  2. Ceph: A Scalable, High-Performance Distributed File System译文

    原文地址:陈晓csdn博客 http://blog.csdn.net/juvxiao/article/details/39495037 论文概况 论文名称:Ceph: A Scalable, High ...

  3. Redis基本认识和基础学习-基本命令

    Redis 基本介绍 REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统. Redis是一个开源的使用ANS ...

  4. HTML+CSS学习任务清单

    HTML部分:掌握HTML的全部语法,他的主体结构,超连接及常用标记的使用 CSS部分:掌握CSS的三种选择器的使用,明白如何使用DIV+CSS进行网页布局,搞清楚浮动问题! 1,HTML的语法(包括 ...

  5. MUI点击事件获取当前对象,及当前对象的属性值

    //用惯了jquery,开始用mui还是有些不习惯 //直接贴代码吧 <nav class="mui-bar mui-bar-tab"> <a class=&qu ...

  6. where id in用 order by field 保持排序

    转载自http://blog.linuxphp.org/archives/1588/ 先看下mysql的默认排序 select id from article where id in(63261,63 ...

  7. HDU1019 Least Common Multiple(多个数的最小公倍数)

    The least common multiple (LCM) of a set of positive integers is the smallest positive integer which ...

  8. HQL语法

    HQL:Hibernate Query Language HQL是完全面向对象的查询语言,因此可以支持继承和多态等特征. $下面介绍HQL语句的语法 1.from子句 from Person 表明从P ...

  9. 在ssm框架中前后台数据交互均使用json格式

    前后台数据交互均使用json. 框架ssm(spring+springmvc+mybatis) @RequestBody注解实现接收http请求的json数据,将json数据转换为java对象,注解加 ...

  10. Problem F: 分数类的类型转换

    Description 封装一个分数类Fract,用来处理分数功能和运算,支持以下操作:   1. 构造:传入两个参数n和m,表示n/m:分数在构造时立即转化成最简分数. 2. show()函数:分数 ...