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 rooms. One of the rooms is the start and one of the rooms is the finish. Each room has an energy value between - and +. One-way doorways interconnect pairs of rooms. The player begins in the start room with 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 (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 . A line containing - 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 -
- -
- -
- -
- - Sample Output hopeless
hopeless
winnable
winnable

题目

  题目大意:一个最多有100个节点的图,你需要从1走到n(注意哦,有向边)。初始有100个能量,到达一个点会使你的能量值变化,如果能在能量为正的情况下到达点n则输出“NOIP有希望了dalao%%%”,否则输出“NOIP翻车了好开心”。(233333~)

  芒果君:之前我们做的都是最短路,现在要做最长路,改个符号就行了。这道题对于我的难点是——如何判环。在不断更新最长路时负环可以忽略,但遇到正环应该怎样处理?我看到有一种Floyd+SPFA的方法比较好理解,其实很像广搜。这道题数据比较小,那我们就先做一遍Floyd,在SPFA中记录一个点被放进队列的次数,如果这个次数很不科学(cnt==n),那么就说明它在正环里。But,存在正环不能说明到不了终点,相反,如果它与终点连通,一定能到达(如果你觉得正环到终点可能能量会耗尽,那么请你讲正环看成无敌聚能环233333再好好想想),所以我们要在之前处理连通性。

 #include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<vector>
using namespace std;
vector<int>e[];
queue<int>Q;
int oc[],vis[],dis[],map[][],n,w[];
int read()
{
int x=,f=;
char ch=getchar();
while(ch<''||ch>''){
if(ch=='-') f=-;
ch=getchar();
}
while(ch>=''&&ch<=''){
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
void floyd()
{
for(int k=;k<=n;++k)
for(int i=;i<=n;++i)
for(int j=;j<=n;++j)
map[i][j]=map[i][j]||(map[i][k]&&map[k][j]);
}
bool spfa()
{
memset(dis,-/,sizeof(dis));
memset(vis,,sizeof(vis));
memset(oc,,sizeof(oc));
dis[]=;
Q.push();
vis[]=;
oc[]=;
while(!Q.empty()){
int x=Q.front();
Q.pop();
vis[x]=;
if(oc[x]>=n) {
if(map[x][n]) return true;
else continue;
}
for(int i=;i<e[x].size();++i){
int v=e[x][i];
if(dis[v]<dis[x]+w[v]&&dis[x]+w[v]>){
dis[v]=dis[x]+w[v];
if(v==n) return true;
if(!vis[v]){
oc[v]++;
vis[v]=;
Q.push(v);
}
}
}
}
return false;
}
int main()
{
int cnt,t;
while(){
scanf("%d",&n);
if(n==-) break;
memset(map,,sizeof(map));
for(int i=;i<=n;++i){
e[i].clear();
w[i]=read();cnt=read();
while(cnt--){
t=read();
e[i].push_back(t);
map[i][t]=;
}
}
floyd();
if(spfa()) printf("winnable\n");
else printf("hopeless\n");
}
return ;
}

HDU 1317:XYZZY的更多相关文章

  1. HDU - 6409:没有兄弟的舞会(数学+思维)

    链接:HDU - 6409:没有兄弟的舞会 题意: 题解: 求出最大的 l[i] 的最大值 L 和 r[i] 的最大值 R,那么 h 一定在 [L, R] 中.枚举每一个最大值,那么每一个区间的对于答 ...

  2. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  3. hdu 1317 XYZZY【Bellheman_ford 判断正环小应用】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1317 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  4. HDU 1317 XYZZY(floyd+bellman_ford判环)

    http://acm.hdu.edu.cn/showproblem.php?pid=1317 题意: 给出一个有向图,每到达一个点,都会加上或减去一些能量,我们要做的就是判断从1出发是否能到达n.初始 ...

  5. hdu 1317 XYZZY

    http://acm.hdu.edu.cn/showproblem.php?pid=1317 #include <cstdio> #include <queue> #inclu ...

  6. HDU 1317 XYZZY【Bellman_Ford判断正环】

    题意:给出n个房间,初始在房间1有100的能量值,每次进入一个房间,能量值可能增加也可能减小,(是点权,不是边权),问能否到达终点的时候能量值还为正 这题自己写的时候wa--wa-- 后来看了题解,还 ...

  7. [HDU 1317]XYZZY[SPFA变形][最长路]

    题意: 一个图, 点权代表走到该点可获得的能量值. 可正可负. 一个人从1 号出发,带有100点能量. 问是否有一种方案可使人在能量值>0的时候走到n. 思路: 这个题首先要注意点权. 其实就是 ...

  8. HDU 2732:Leapin' Lizards(最大流)

    http://acm.hdu.edu.cn/showproblem.php?pid=2732 题意:给出两个地图,蜥蜴从一个柱子跳跃到另外一个地方,那么这个柱子就可能会坍塌,第一个地图是柱子可以容忍跳 ...

  9. HDU 4289:Control(最小割)

    http://acm.hdu.edu.cn/showproblem.php?pid=4289 题意:有n个城市,m条无向边,小偷要从s点开始逃到d点,在每个城市安放监控的花费是sa[i],问最小花费可 ...

随机推荐

  1. json读写

    import json l = [,,}] print(json.dumps(l)) d = dict(b=None,a=,c='abc') print(json.dumps(d, separator ...

  2. springBoot怎样访问静态资源?+静态资源简介

    1.静态资源 怎样通过浏览器访问静态资源? 注意:不需要加static目录.因为只是告诉springboot目录,而不是静态资源路劲. 这时访问路径就需要加上/static

  3. Linux配置Tomcat8080端口 远程无法访问解决办法

    是因为Linux的防火墙没有开放8080端口 解决办法: /sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT #开启8080端口  /sbin/ ...

  4. c++ 数据类型长度

    #include <iostream> using namespace std; int main() { cout << "char: " << ...

  5. mysql数据库的还原及常见问题解决

    例如:需要还原的数据库脚本文件为test.sql,脚本中已包含数据库的创建,test.sql所在目录为/home 1.常用source命令 进入mysql数据库控制台,如mysql -uroot -p ...

  6. eclipse CDT Error: Program "g++" not found in PATH

    右击project explore ->properties-  >c/c++ build->environment , 设置 mingw_home 路径

  7. Postgresql - MATERIALIZED VIEW

    MATERIALIZED VIEWPG 9.3 版本之后开始支持物化视图.View 视图:虚拟,不存在实际的数据,在查询视图的时候其实是对视图内的表进行查询操作. 物化视图:实际存在,将数据存成一张表 ...

  8. js中的splice方法和slice方法简单总结

    slice:是截取用的 splice:是做删除 插入 替换用的 slice(start,end): 参数: start:开始位置的索引 end:结束位置的索引(但不包含该索引位置的元素) 例如: va ...

  9. [go]日志库小例子

    输出日志 //输出日志到console msg := fmt.Sprintf(format, args...) //format里的坑 args解出的数据相匹配 fmt.Fprintf(os.Stdo ...

  10. Java同步数据结构之SynchronousQueue

    前言 严格来说SynchronousQueue并不是像它的名字那样是一种Queue,它更像是一个数据接力的交汇点,还记得在介绍Exchanger的时候提到过Exchanger可以看作是Synchron ...