HDU 1317:XYZZY
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的更多相关文章
- HDU - 6409:没有兄弟的舞会(数学+思维)
链接:HDU - 6409:没有兄弟的舞会 题意: 题解: 求出最大的 l[i] 的最大值 L 和 r[i] 的最大值 R,那么 h 一定在 [L, R] 中.枚举每一个最大值,那么每一个区间的对于答 ...
- 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 ...
- hdu 1317 XYZZY【Bellheman_ford 判断正环小应用】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1317 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- HDU 1317 XYZZY(floyd+bellman_ford判环)
http://acm.hdu.edu.cn/showproblem.php?pid=1317 题意: 给出一个有向图,每到达一个点,都会加上或减去一些能量,我们要做的就是判断从1出发是否能到达n.初始 ...
- hdu 1317 XYZZY
http://acm.hdu.edu.cn/showproblem.php?pid=1317 #include <cstdio> #include <queue> #inclu ...
- HDU 1317 XYZZY【Bellman_Ford判断正环】
题意:给出n个房间,初始在房间1有100的能量值,每次进入一个房间,能量值可能增加也可能减小,(是点权,不是边权),问能否到达终点的时候能量值还为正 这题自己写的时候wa--wa-- 后来看了题解,还 ...
- [HDU 1317]XYZZY[SPFA变形][最长路]
题意: 一个图, 点权代表走到该点可获得的能量值. 可正可负. 一个人从1 号出发,带有100点能量. 问是否有一种方案可使人在能量值>0的时候走到n. 思路: 这个题首先要注意点权. 其实就是 ...
- HDU 2732:Leapin' Lizards(最大流)
http://acm.hdu.edu.cn/showproblem.php?pid=2732 题意:给出两个地图,蜥蜴从一个柱子跳跃到另外一个地方,那么这个柱子就可能会坍塌,第一个地图是柱子可以容忍跳 ...
- HDU 4289:Control(最小割)
http://acm.hdu.edu.cn/showproblem.php?pid=4289 题意:有n个城市,m条无向边,小偷要从s点开始逃到d点,在每个城市安放监控的花费是sa[i],问最小花费可 ...
随机推荐
- 使用docker配置gitlab服务器
下载gitlab镜像,导入 [root@gitlab ~]# docker load < gitlab_zh.tar 容器需要22端口,所以修改ssh的默认端口 [root@gitlab ~]# ...
- 题解 CF550A 【Two Substrings】
为什么我的做法跟别人如此不一样啊qwq 思路:暴力判每一个"BA"出现的位置,二分查找他前/后有没有满足条件的"AB",时间复杂度\(O(n\log_{2}n) ...
- python3.5+win7 安装 numpy 和scipy的总结
1.安装numpy. 官网下载numpy.下载地址为https://pypi.Python.org/pypi/numpy.选择与Python版本相对应的numpy版本.之后在python35 ...
- apt update时出现签名无法验证,公钥失效的解决办法
错误信息如下 W: GPG error: http://ppa.launchpad.net/ondrej/php/ubuntu xenial InRelease: The following sign ...
- Win内核原理与实现学习笔记3-windows系统结构
1.概述 1.1windows采用了双模式(dual mode)结构来保护操作系统本身,以避免被应用程序的错误而波及.操作系统核心运行在内核模式(kernel mode)下,应用程序的代码运行在用户模 ...
- C语言学习笔记5-程序结构
本系列文章由jadeshu编写,转载请注明出处.http://blog.csdn.net/jadeshu/article/details/50752148 作者:jadeshu 邮箱: jades ...
- html预加载之link标签
我们之前提及过link rel 里面有preload和prefetch.modulepreload,都是用于预加载资源 <link rel="preload" href=&q ...
- postgre-插入数据时的单引号问题
场景: 将一个HTML页面存储到数据库中 问题: HTML页面中既包含单引号也包含双引号 解决办法: 双单引号 INSERT INTO table VALUES ('<html><s ...
- 常见的RuntimeException
一般面试中java Exception(runtimeException )是必会被问到的问题常见的异常列出四五种,是基本要求.更多的....需要注意积累了 常见的几种如下: NullPointerE ...
- Docker安装mysql5.6
1.docker hub 上查找mysql镜像 2.在docker hub上(阿里云加速器)拉取mysql镜像到本地标签为5.6 3.使用mysql5.6创建容器(也叫运行镜像) 4.交互运行,进入m ...