XYZZY(spfa求最长路)
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
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.
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.
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
hopeless
winnable
winnable
#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求最长路)的更多相关文章
- spfa求最长路
http://poj.org/problem?id=1932 spfa求最长路,判断dist[n] > 0,需要注意的是有正环存在,如果有环存在,那么就要判断这个环上的某一点是否能够到达n点,如 ...
- POJ 3592--Instantaneous Transference【SCC缩点新建图 && SPFA求最长路 && 经典】
Instantaneous Transference Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6177 Accep ...
- HDU - 6201 transaction transaction transaction(spfa求最长路)
题意:有n个点,n-1条边的无向图,已知每个点书的售价,以及在边上行走的路费,问任选两个点作为起点和终点,能获得的最大利益是多少. 分析: 1.从某个结点出发,首先需要在该结点a花费price[a]买 ...
- 洛谷 P3627 [APIO2009]抢掠计划 Tarjan缩点+Spfa求最长路
题目地址:https://www.luogu.com.cn/problem/P3627 第一次寒假训练的结测题,思路本身不难,但对于我这个码力蒟蒻来说实现难度不小-考试时肛了将近两个半小时才刚肛出来. ...
- [HDU 1317]XYZZY[SPFA变形][最长路]
题意: 一个图, 点权代表走到该点可获得的能量值. 可正可负. 一个人从1 号出发,带有100点能量. 问是否有一种方案可使人在能量值>0的时候走到n. 思路: 这个题首先要注意点权. 其实就是 ...
- POJ 3126 --Father Christmas flymouse【scc缩点构图 && SPFA求最长路】
Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 3007 Accep ...
- hdu 1534(差分约束+spfa求最长路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1534 思路:设s[i]表示工作i的开始时间,v[i]表示需要工作的时间,则完成时间为s[i]+v[i] ...
- 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 ...
- 训练赛 Grouping(强连通分量缩点 + DAG求最长路)
http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=158#problem/F 大致题意:给出n个人和m种关系(ti,si),表示ti ...
随机推荐
- mac上虚拟机安装旧版本的macosx 10.8
前言 由于测试的需要,需要10.8的macosx,但又不想降级自己mac版本,所以还是装虚拟机,Parallels Desktop试验了安装不了osx,就换VMware Fusion,发现是可以的. ...
- JavaScript的简单入门
一.导读 简介:JavaScript简称js,是基于对象和事件驱动的脚本语言,主要运用于客户端.原名LiveScript,本身和Java没有任何关系,但语法上很类似. 特点:交互性(它可以做的就是信息 ...
- JAVA NIO学习二:通道(Channel)与缓冲区(Buffer)
今天是2018年的第三天,真是时光飞逝,2017年的学习计划还没有学习完成,因此继续开始研究学习,那么上一节我们了解了NIO,那么这一节我们进一步来学习NIO相关的知识.那就是通道和缓冲区.Java ...
- LAMP第二部分apache配置
课程大纲:1. 下载discuz! mkdir /data/wwwcd /data/wwwwget http://download.comsenz.com/DiscuzX/3.2/Discuz_X3 ...
- C# .net中json字符串和对象之间的转化方法
http://blog.csdn.net/xuexiaodong009/article/details/46998069 json作为作为一种最常用的数据,应用很广泛,在.net中如何把一个对象转化为 ...
- 如何高逼格读取Web.config中的AppSettings
http://edi.wang/post/2015/4/22/how-to-read-webconfig-appsettings-with-bigiblity 先插句题外话,下版本的ASP.NET貌似 ...
- python打开一个本地目录文件路径
os.path.abspath()os 模块为 python 语言标准库中的 os 模块包含普遍的操作系统功能.主要用于操作本地目录文件.path.abspath()方法用于获取当前路径下的文件. 比 ...
- 在macOS上通过pyenv安装和切换多版本Python
1. 安装homebrew 官网 http://brew.sh/index_zh-cn.html 打开终端,在终端中粘贴如下脚本 /usr/bin/ruby -e "$(curl -fsSL ...
- Python,PyCharm2017安装教程,包含注册码
一,安装PyCharm 1.下载PyCharm 进入https://www.jetbrains.com/pycharm/download/#section=windows官网下载页面,可以到到PyCh ...
- vim 查找的技巧
vim查找时如果有多个特殊字符需要转义容易出错还繁琐. 要确保要查找的 URL 仍保存在寄存器 u 中.然后,我们输入 / 或者 ? 调出查找提示符,二者均能正确工作.最后,要依次输入原义开关 \V ...