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 管程解决生产者消费者问题

    同样是实验存档.//.. 依然以生产者消费者问题作为背景. 管程(=“资源管理程序”)将资源和对资源的操作封装起来,资源使用者通过接口操作资源就 ok,不用去考虑进程同步的问题. 管程: packag ...

  2. lesson - 10 课程笔记

    CTRL+C CTRL+D :前者用于结束一个程序,后者用于结束终端输入. --符号 *:匹配任意长度的任意字符 ?:匹配任意一个字符 #: shell 中表注释 \: 脱意符号 []:任意属于字符组 ...

  3. 我的第一个python web开发框架(19)——产品发布相关事项

    好不容易小白将系统开发完成,对于发布到服务器端并没有什么经验,于是在下班后又找到老菜. 小白:老大,不好意思又要麻烦你了,项目已经弄完,但要发布上线我还一头雾水,有空帮我讲解一下吗? 老菜:嗯,系统上 ...

  4. JMeter 插件管理

    JMeter管理的插件包括了jmeter-plugins.org上常用的插件以及各种第三方插件和JMeter核心插件. JMeter插件管理器主要管理插件安装,卸载,升级等操作. 安装插件管理 1.下 ...

  5. Xampp配置本地域名及常见错误解决

    本地域名配置 1.计算机-->C盘-->Windows-->System32-->drivers-->etc-->hosts 127.0.0.1       loc ...

  6. JavaScript调试技巧

    熟悉工具可以让工具在工作中发挥出更大的作用.尽管江湖传言 JavaScript 很难调试,但如果你掌握了几个技巧,就能用很少的时间来解决错误和bug. 文中已经列出了14个你可能不知道的调试技巧,但是 ...

  7. 使用Template格式化Python字符串

    对Python字符串,除了比较老旧的%,以及用来替换掉%的format,及在python 3.6中加入的f这三种格式化方法以外,还有可以使用Template对象来进行格式化. from string ...

  8. MapReduce工作原理流程简介

    在MapReduce整个过程可以概括为以下过程: 输入 --> map --> shuffle --> reduce -->输出 输入文件会被切分成多个块,每一块都有一个map ...

  9. Mac操作系统下忘记MYSQL的密码

    1. 在系统偏好 中,中止MySQL服务.: 2. cd/usr/local/mysql/bin   sudo ./mysqld_safe --skip-grant-tables 3. 登录MySQL ...

  10. MySQL 基础命令

    的说法啊打发 第1章 SQL语句 mysql版本:针对mysql-5.6.36 版本 (5.7会有一些变动) 1.1 常用命令 # 查看数据库 mysql> show databases; sh ...