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. Java I/O---RandomAccessFile类(随机访问文件的读取和写入)

    1.JDK API中RandomAccessFile类的描述 此类的实例支持对随机访问文件的读取和写入.随机访问文件的行为类似存储在文件系统中的一个大型 byte 数组.存在指向该隐含数组的光标或索引 ...

  2. bzoj 4538: [Hnoi2016]网络

    Description 一个简单的网络系统可以被描述成一棵无根树.每个节点为一个服务器.连接服务器与服务器的数据线则看做一条树边.两个服务器进行数据的交互时,数据会经过连接这两个服务器的路径上的所有服 ...

  3. 【bzoj3809】Gty的二逼妹子序列

    Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数. 为了方便,我们 ...

  4. 使用trim_galore软件遇到的问题

    我的原始测序数据是双端测序,在用trim_galore软件去接头的这一步,使用的命令行是 time nohup trim_galore R17002628-SKOV3-m6A_combined_R1. ...

  5. Webpack 2 视频教程 013 - 自动分离 CSS 到独立文件

    原文发表于我的技术博客 这是我免费发布的高质量超清「Webpack 2 视频教程」. Webpack 作为目前前端开发必备的框架,Webpack 发布了 2.0 版本,此视频就是基于 2.0 的版本讲 ...

  6. Python 学习教程汇总

    Python快速教程http://www.cnblogs.com/vamei/archive/2012/09/13/2682778.html简明Python教程https://bop.molun.ne ...

  7. Spring Boot实战:Restful API的构建

    上一篇文章讲解了通过Spring boot与JdbcTemplate.JPA和MyBatis的集成,实现对数据库的访问.今天主要给大家分享一下如何通过Spring boot向前端返回数据. 在现在的开 ...

  8. iconfont-字体图标

    看到支付宝官网,使用很多iconfont-字体图标.素色,纯色,体积小,尝试使用做个demo,以便日后参考使用 ============================ <h1>第一个结构 ...

  9. Python 数据科学系列 の Numpy、Series 和 DataFrame介绍

    本課主題 Numpy 的介绍和操作实战 Series 的介绍和操作实战 DataFrame 的介绍和操作实战 Numpy 的介绍和操作实战 numpy 是 Python 在数据计算领域里很常用的模块 ...

  10. mysql字符串操作相关函数用法总结

    功能用法简单例子一览表 函数 功能 用法 例子 left() 从字符串左边为边界返回相应长度的子字符串 left(str, length) mysql> select left('vssf',3 ...