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. MongoDB用户权限管理

    创建用户账号: (roles参数指定了用户的角色以及这个账号授权的数据库,在同一个数据库中不能同时创建两个用户名相同的账号) Mongodb内置的用户角色: 数据库用户角色:read.readWrit ...

  2. 修改Jupyter notebook的启动目录

    修改Jupyter notebook的启动目录 1. 在控制台输入以下命令,检查Jupyter notebook的安装目录 jupyter notebook --generate-config ​ 如 ...

  3. zookeeper启动异常

    zookeeper启动报异常 java.io.EOFException  at java.io.DataInputStream.readInt(DataInputStream.java:392) 遇到 ...

  4. PHPMailer < 5.2.18 远程代码执行漏洞(CVE-2016-10033)

    PHPMailer < 5.2.18 Remote Code Execution 本文将简单展示一下PHPMailer远程代码执行漏洞(CVE-2016-10033)的利用过程,使用的是别人已经 ...

  5. python密码错误三次锁定

    程序需求: 输入用户名,密码 认证成功显示欢迎信息 输入错误三次后锁定用户 流程图: 好像画的不咋地 #!/usr/bin/env python # _*_ coding:utf-8 _*_ # Fi ...

  6. SSM框架搭建(Spring+SpringMVC+MyBatis)与easyui集成并实现增删改查实现

    一.用myEclipse初始化Web项目 新建一个web project: 二.创建包 controller        //控制类 service //服务接口 service.impl //服务 ...

  7. 2017年当下最值得你关注的前端开发框架,不知道你就OUT了!

    近几年随着 jQuery.Ext 以及 CSS3 的发展,以 Bootstrap 为代表的前端开发框架如雨后春笋般挤入视野,可谓应接不暇. 在这篇分享中,我将总结2017年当下最值得你关注的前端开发框 ...

  8. COBBLER无人值守安装

    cobbler-自动安装系统 1.1 cobber简介 1.1.1 cobbler说明 Cobbler是一个Linux服务器安装的服务,可以通过网络启动(PXE)的方式来快速安装.重装物理服务器和虚拟 ...

  9. 激光相机数据融合(3)--KITTI数据集

    KITTI数据集提供了双目图像,激光数据,和imu/gps位置信息,其中还包括了大量的算法.下载地址为:http://www.cvlibs.net/datasets/kitti/raw_data.ph ...

  10. spring装配Bean过程

    主要流程: 1.读取配置文件 2.实例化bean和填充bean属性 这个粗略的流程感觉更像是一个需求,有了这个需求,那么spring内部是怎么处理的呢? 我们知道spring的两个核心接口BeanFa ...