HDU 1317XYZZY spfa+判断正环+链式前向星(感觉不对,但能A)
XYZZY
Total Submission(s): 5304 Accepted Submission(s): 1510
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.
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.
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
单向路径。判断是否存在正环,初始化距离数组为负无穷小,进入n次,说明存在正环,将距离改为无穷大。进入n+1次,直接跳过。
代码:
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=;
const int maxm=;
const int INF=0x3f3f3f3f;
struct edgenode {
int to,w,next;
}edges[maxm];
bool vis[maxn];
int dist[maxn],du[maxn],head[maxn];
int n,cnt;
void init() {
for(int i=;i<maxn;++i) head[i]=-;
for(int i=;i<maxm;++i) edges[i].next=-;
cnt=;
}
void addedge(int u, int v, int w) {
edges[cnt].to=v;
edges[cnt].w=w;
edges[cnt].next=head[u];
head[u]=cnt++;
}
bool spfa() {
memset(vis,false,sizeof(vis));
memset(du,,sizeof(du));
for(int i=;i<maxn;++i) dist[i]=-INF;
queue<int> q;
dist[]=;vis[]=true;
q.push();
while(!q.empty()) {
int now=q.front();q.pop();
vis[now]=false;
du[now]++;
if(du[now]>n) continue;
if(du[now]==n) dist[now]=INF;
for(int i=head[now];~i;i=edges[i].next) {
if(dist[edges[i].to]<dist[now]+edges[i].w&&dist[now]+edges[i].w>) {
dist[edges[i].to]=dist[now]+edges[i].w;
if(edges[i].to==n) return true;
if(!vis[edges[i].to]) {
vis[edges[i].to]=true;
q.push(edges[i].to);
}
}
}
}
return false;
}
int main() {
while(scanf("%d",&n)&&n!=-) {
int w,num,id;
init();
for(int i=;i<=n;++i) {
scanf("%d%d",&w,&num);
for(int j=;j<=num;++j) {
scanf("%d",&id);
addedge(i,id,w);
}
}
if(spfa()) printf("winnable\n");
else printf("hopeless\n");
}
return ;
}
HDU 1317XYZZY spfa+判断正环+链式前向星(感觉不对,但能A)的更多相关文章
- HDU 2544最短路 【dijkstra 链式前向星+优先队列优化】
最开始学最短路的时候只会用map二维数组存图,那个时候还不知道这就是矩阵存图,也不懂得效率怎么样 经过几个月的历练再回头看最短路的题, 发现图可以用链式前向星来存, 链式前向星的效率是比较高的.对于查 ...
- Currency Exchange POJ - 1860 (spfa判断正环)
Several currency exchange points are working in our city. Let us suppose that each point specializes ...
- Currency Exchange POJ - 1860 spfa判断正环
//spfa 判断正环 #include<iostream> #include<queue> #include<cstring> using namespace s ...
- 单元最短路径算法模板汇总(Dijkstra, BF,SPFA),附链式前向星模板
一:dijkstra算法时间复杂度,用优先级队列优化的话,O((M+N)logN)求单源最短路径,要求所有边的权值非负.若图中出现权值为负的边,Dijkstra算法就会失效,求出的最短路径就可能是错的 ...
- SPFA + 链式前向星(详解)
求最短路是图论中最基础的算法,最短路算法挺多,本文介绍SPFA算法. 关于其他最短路算法,请看我另一篇博客最短路算法详解 链式前向星概念 简单的说,就是存储图的一个数据结构.它是按照边来存图,而邻接矩 ...
- 最短路 spfa 算法 && 链式前向星存图
推荐博客 https://i.cnblogs.com/EditPosts.aspx?opt=1 http://blog.csdn.net/mcdonnell_douglas/article/deta ...
- POJ 3169 Layout(差分约束+链式前向星+SPFA)
描述 Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 ...
- 链式前向星+SPFA
今天听说vector不开o2是数组时间复杂度常数的1.5倍,瞬间吓傻.然后就问好的图表达方式,然后看到了链式前向星.于是就写了一段链式前向星+SPFA的,和普通的vector+SPFA的对拍了下,速度 ...
- 【模板】链式前向星+spfa
洛谷传送门--分糖果 博客--链式前向星 团队中一道题,数据很大,只能用链式前向星存储,spfa求单源最短路. 可做模板. #include <cstdio> #include <q ...
随机推荐
- 注解的形式与xml文件的形式完成事务管理及xml文件的配置
需要的jar包: c3p0-0.9.2.1.jar com.springsource.net.sf.cglib-2.2.0.jar com.springsource.org.aopalliance-1 ...
- 如何线上部署node.js项目
来源:http://blog.csdn.net/chenlinIT/article/details/73343793 前言 最近工作不是很忙,在空闲时间学习用node+express搭建自己的个人博客 ...
- ssh免密码记录
主机器A通过ssh连多台从机器(b1,b2,b3). 1.使用root用户操作,避免权限问题. 2.在主从机器中安装ssh,命令: ssh-keygen –t rsa 然后都回车,生成的文件在/roo ...
- HDU 6112 今夕何夕
今夕何夕 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- Codeforces Round #383 (Div. 2) B. Arpa’s obvious problem and Mehrdad’s terrible solution
B. Arpa’s obvious problem and Mehrdad’s terrible solution time limit per test 1 second memory limit ...
- Android 开发笔记___switch__开关
default switch <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...
- Node.js Web 模块
什么是 Web 服务器? Web服务器一般指网站服务器,是指驻留于因特网上某种类型计算机的程序,Web服务器的基本功能就是提供Web信息浏览服务.它只需支持HTTP协议.HTML文档格式及URL,与客 ...
- 行内脚本的位置放置----css阻塞行内脚本
行内脚本:避免放置在css和其他资源之间. 若在head中,最好放在css样式表之前,如果放置在样式表之后,会引起css阻塞. css阻塞:由于浏览器要保证css和JavaScript的执行顺序,cs ...
- tamper-proof 对象 nonextensible对象 sealed对象 frozen对象
tamper-proof 对象JavaScript的缺点之一就是每个对象都可以被相同执行上下文的代码修改,很容易导致意外覆盖,或则一不小心把native 对象覆盖.Ecmascript 5提供了 t ...
- 运维必须掌握的150个Linux命令
线上查询及帮助命令(1个)man 目录操作命令(6个)ls tree pwd mkdir rmdir cd 文件操作命令(7个)touch cp mv rm ln find rename 文件查看及处 ...