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. 数据库入门(以MySQL为例)

    一.数据库中的概念 1.数据库是用户存放数据.访问数据.操作数据的存储仓库,用户的各种数据被有组织地存放在数据库中.可以随时被有权限的用户查询.统计.添加.删除.和修改.可以说,数据库是长期存储在计算 ...

  2. Xamarin Android Gestures详解

    通过Gesture的监听我们将实现一个,手指的快速滑动显示坐标的变化,我们先来看一看效果图: 1.Android中手势交互的执行顺序 1.手指触碰屏幕时,触发MotionEvent事件! 2.该事件被 ...

  3. 回顾2017系列篇(一):最佳的11篇UI/UX设计文章

    2017已经接近尾声,在这一年中,设计领域发生了诸多变化.也是时候对2017年做一个总结,本文主要是从2017设计文章入手,列出了个人认为2017设计行业里最重要的UI/UX文章的前11名,供大家参考 ...

  4. ubuntu设置静态ip

    设置固定ip地址 >>>>>>>>>> ifconfig -a,  (注:p1p1为网卡名称) 配置静态ip vim /etc/networ ...

  5. LAMP第一部分-环境搭建

    1. 安装mysqlcd /usr/local/src/ wget http://syslab.comsenz.com/downloads/linux/mysql-5.1.40-linux-i686- ...

  6. 本机向windows服务器传输文件的三种方法

    闲来无事,在腾讯云上申请了一个免费的服务器,想将自己写的网页发布到服务器上,服务器的申请很简单,百度搜索 腾讯云 ,然后新人第一次注册能申请到免费一个月的云主机,虽然配置不怎么高,但是还是能用的,这是 ...

  7. ASP.NET Core使用静态文件、目录游览与MIME类型管理

    前言 今天我们来了解了解ASP.NET Core中的静态文件的处理方式. 以前我们寄宿在IIS中的时候,很多静态文件的过滤 和相关的安全措施 都已经帮我们处理好了. ASP.NET Core则不同,因 ...

  8. 关于linux命令ssh的总结

    因为项目计算量比较大,需要将任务分布到多台电脑上面运行,因为对于分布式概念不熟,就想到了linux最简单的ssh协议,远程控制其他电脑,然后写shell脚本统一在所有电脑上运行程序.(我的操作系统为U ...

  9. python pandas 合并数据函数merge join concat combine_first 区分

    pandas对象中的数据可以通过一些内置的方法进行合并:pandas.merge,pandas.concat,实例方法join,combine_first,它们的使用对象和效果都是不同的,下面进行区分 ...

  10. Head First设计模式之生成器模式

    一.定义 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示.建造者模式是一种对象创建型模式. 二.结构 角色 Builder(抽象建造者):它为创建一个产品Product对象的 ...