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. Java 基础 -- 泛型、集合、IO、反射

    package com.java.map.test; import java.util.ArrayList; import java.util.Collection; import java.util ...

  2. gulp-prompt入个了门

    gulp-prompt版本:0.4.1 源码:gulp-prompt 一.gulp-prompt的简介 gulp-prompt 是一个基于gulp的命令行提示. 我们可以用它来完成命令行中互动功能. ...

  3. 三、第一个IDEA创建的MAVEN工程——JavaWeb点滴

    一.Maven是什么? Maven是一个项目管理工具,它包含了一个项目对象模型 (Project Object Model),一组标准集合,一个项目生命周期(Project Lifecycle),一个 ...

  4. 树的三种遍历方式(C语言实现)

    //************************************************************************* // [前序]遍历算法 //二叉树不空,先访问根 ...

  5. Android 开发笔记___DateUtil——Time

    package com.example.alimjan.hello_world; /** * Created by alimjan on 6/30/2017. */ import java.text. ...

  6. 网页头部 lang的声明

    1. 简体中文页面:html lang=zh-cmn-Hans2. 繁体中文页面:html lang=zh-cmn-Hant3. 英语页面:html lang=en 4. <回来>的音频, ...

  7. webapp通用选择器:iosselect

    1,这个组件解决什么问题 在IOS系统中,safari浏览器的select标签默认展示样式和iOS-UIPickerView展示方式一致,形如下图: 这个选择器操作方便,样式优美.但是在安卓系统中展示 ...

  8. Promise同时进入catch和then——踩坑

    记录今天使用Promise遇到的一个坑--在resolve()返回运行then之后,函数又进入到了catch,源代码大意如下: var pro = function() { return new Pr ...

  9. 使用java生成mapbox-gl可读的vector tile

    概述 mapbox-gl主要数据源来自mapbox vector tile,本文就是要阐述怎样把postgresql中的地理空间数据转换成vector tile,流程图如下: 配置 该工程采用spri ...

  10. 使用Jquery.js框架和CSS3实现3D相册的制作

    有关3D相册的制作主要包括以下几个知识点: 1.有关图片的位置摆放,也就是一个相对定位绝对定位的使用: 2.有关CSS3中transform属性的使用(transform-style: preserv ...