http://acm.hdu.edu.cn/showproblem.php?pid=1317

XYZZY

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3514    Accepted Submission(s): 973

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
 
Source
 
题意: 每个房间有一个能量值,初始100能量,问是否可以从1走到n
题解: 这个题是用spfa求最长路,并且判环很巧妙的写法
    用一个数组存放点的权值,每次走进这个点的时候如果从起点到这个点的剩余能量加上这个点的能量大于0 的话就可以走出去,代码中给出详细的注释
说明:在求最长路的时候只能用spfa或者是Floyd不能用dijk(因为dijk使用贪心写的,所以会有问题)spfa在判环的时候很灵活,再建图的时候也很灵活,如果所给的是点权值而不是边权的时候可以用一个点的数组来存点权,帅气。
 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 110
#define INF 0x1fffffff
int node[N];
struct Edge{
int to;
int next;
}edge[N*N];
int dis[N];
int que[N*N];
bool inq[N];
int cnt[N];
int n;
int top;
int head[N];
int Enct;
void add(int from , int to)
{
edge[Enct].to = to;
edge[Enct].next = head[from];
head[from] = Enct++;
}
void init()
{
Enct = ;
memset(head,-,sizeof(head));
}
void spfa()
{
top = ;
for(int i = ;i <= n ;i++)
inq[i] = false,cnt[i] = ,dis[i]= -INF;//要定义成一个没有意义的数-INF
que[top++] = ;
dis[] = ;
inq[] = true;
cnt[]++;
for(int i= ; i < top ; i++)
{
int u = que[i];
inq[u] = false;
if(dis[u] + node[u] <= ) continue;//没办法走出这个点所以不再用这个点去更新其他的点
for(int j = head[u] ; j!=- ;j = edge[j].next)
{
Edge e = edge[j];
if(dis[e.to]<dis[u]+node[u])//如果下一个点的到起点的dis小于这个点到起点的dis加上这个点自己的权值的话 更新为最长录路
{
dis[e.to] = dis[u] + node[u];
if(inq[e.to]==false)
{
// que[top++] = e.to;
// inq[e.to] = true;
cnt[e.to]++;
if(cnt[e.to]>n) continue;//这里在判定环的时候在这个点在第n次加入队列的时候证明有环,这时候仍把它加入队列,并用这个点权值更新为INF
//并用这个点去更新这个环所在路径的值都为INF这样就可以解决有正环但却不一定与起点终点联通的问题了,因为这样处理后这个环只能更新其路径上的值
else if(cnt[e.to]==n)
dis[e.to] = INF;//更新
que[top++] = e.to;
inq[e.to] = ;
}
}
}
}
}//spfa在用的时候一定要注意其编号是不是从1开始编号的 还是从0,注意
int main()
{
while(~scanf("%d",&n)&&n!=-)
{
init();
int a,cn,t;
int tm = ;
for(t = ; t <= n ;t++ )
{
scanf("%d%d",&a,&cn);
node[t] = a;
for(int i = ;i < cn;i++)
{
int to;
scanf("%d",&to);
add(t,to);
}
}
spfa();
// for(int i = 1; i <= n; i++) printf("%d ", dis[i]); puts("");
if(dis[n]>)puts("winnable");
else puts("hopeless");
}
return ;
}

XYZZY(spfa求最长路)的更多相关文章

  1. spfa求最长路

    http://poj.org/problem?id=1932 spfa求最长路,判断dist[n] > 0,需要注意的是有正环存在,如果有环存在,那么就要判断这个环上的某一点是否能够到达n点,如 ...

  2. POJ 3592--Instantaneous Transference【SCC缩点新建图 &amp;&amp; SPFA求最长路 &amp;&amp; 经典】

    Instantaneous Transference Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6177   Accep ...

  3. HDU - 6201 transaction transaction transaction(spfa求最长路)

    题意:有n个点,n-1条边的无向图,已知每个点书的售价,以及在边上行走的路费,问任选两个点作为起点和终点,能获得的最大利益是多少. 分析: 1.从某个结点出发,首先需要在该结点a花费price[a]买 ...

  4. 洛谷 P3627 [APIO2009]抢掠计划 Tarjan缩点+Spfa求最长路

    题目地址:https://www.luogu.com.cn/problem/P3627 第一次寒假训练的结测题,思路本身不难,但对于我这个码力蒟蒻来说实现难度不小-考试时肛了将近两个半小时才刚肛出来. ...

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

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

  6. POJ 3126 --Father Christmas flymouse【scc缩点构图 &amp;&amp; SPFA求最长路】

    Father Christmas flymouse Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 3007   Accep ...

  7. hdu 1534(差分约束+spfa求最长路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1534 思路:设s[i]表示工作i的开始时间,v[i]表示需要工作的时间,则完成时间为s[i]+v[i] ...

  8. HDU 1224 Free DIY Tour(spfa求最长路+路径输出)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1224 Free DIY Tour Time Limit: 2000/1000 MS (Java/Oth ...

  9. 训练赛 Grouping(强连通分量缩点 + DAG求最长路)

    http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=158#problem/F 大致题意:给出n个人和m种关系(ti,si),表示ti ...

随机推荐

  1. mac上虚拟机安装旧版本的macosx 10.8

    前言 由于测试的需要,需要10.8的macosx,但又不想降级自己mac版本,所以还是装虚拟机,Parallels Desktop试验了安装不了osx,就换VMware Fusion,发现是可以的. ...

  2. JavaScript的简单入门

    一.导读 简介:JavaScript简称js,是基于对象和事件驱动的脚本语言,主要运用于客户端.原名LiveScript,本身和Java没有任何关系,但语法上很类似. 特点:交互性(它可以做的就是信息 ...

  3. JAVA NIO学习二:通道(Channel)与缓冲区(Buffer)

    今天是2018年的第三天,真是时光飞逝,2017年的学习计划还没有学习完成,因此继续开始研究学习,那么上一节我们了解了NIO,那么这一节我们进一步来学习NIO相关的知识.那就是通道和缓冲区.Java ...

  4. LAMP第二部分apache配置

    课程大纲:1. 下载discuz! mkdir /data/wwwcd /data/wwwwget  http://download.comsenz.com/DiscuzX/3.2/Discuz_X3 ...

  5. C# .net中json字符串和对象之间的转化方法

    http://blog.csdn.net/xuexiaodong009/article/details/46998069 json作为作为一种最常用的数据,应用很广泛,在.net中如何把一个对象转化为 ...

  6. 如何高逼格读取Web.config中的AppSettings

    http://edi.wang/post/2015/4/22/how-to-read-webconfig-appsettings-with-bigiblity 先插句题外话,下版本的ASP.NET貌似 ...

  7. python打开一个本地目录文件路径

    os.path.abspath()os 模块为 python 语言标准库中的 os 模块包含普遍的操作系统功能.主要用于操作本地目录文件.path.abspath()方法用于获取当前路径下的文件. 比 ...

  8. 在macOS上通过pyenv安装和切换多版本Python

    1. 安装homebrew 官网 http://brew.sh/index_zh-cn.html 打开终端,在终端中粘贴如下脚本 /usr/bin/ruby -e "$(curl -fsSL ...

  9. Python,PyCharm2017安装教程,包含注册码

    一,安装PyCharm 1.下载PyCharm 进入https://www.jetbrains.com/pycharm/download/#section=windows官网下载页面,可以到到PyCh ...

  10. vim 查找的技巧

    vim查找时如果有多个特殊字符需要转义容易出错还繁琐. 要确保要查找的 URL 仍保存在寄存器 u 中.然后,我们输入 / 或者 ? 调出查找提示符,二者均能正确工作.最后,要依次输入原义开关 \V ...