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 ...
随机推荐
- Asp.net Api中使用OAuth2.0实现“客户端验证”
一.实现继承自OAuthAuthorizationServerProvider的类,实现以"客户端验证"方式传入的相关认证和access_token发放. public class ...
- 中高级JavaScript易错面试题
写出下题的输出 1.函数的实参与形参length var length = 10; function fn() { console.log(this.length); } var obj = { le ...
- [Scikit-learn] 4.4 Dimensionality reduction - PCA
2.5. Decomposing signals in components (matrix factorization problems) 2.5.1. Principal component an ...
- 总结:PyQt5自定义信号源
定义一个信号源有4个方面要注意: 1.定义信号源 A = pyqtSignal([str], [int,str]) 这里特别使用信号源重载的情况加以说明.如上就是信号源A的重载,一个可以发送str参数 ...
- 利用wsdl.exe自动将wsdl文档转换为C#代码
1.获取完整的wsdl文档 获取下面这个博客中提到的wsdl http://www.cnblogs.com/LCCRNblog/p/3716406.html 将获取到的wsdl放到一个文本中,改后缀( ...
- Problem X
Problem Description Recently, iSea went to an ancient country. For such a long time, it was the most ...
- hbase的HQuorumPeer和QuorumPeerMain
hbase是列式数据库,既可以单机也可以以集群的方式搭建,以集群的方式搭建一般建立在hdfs之上. 分布式的hbase如何启动? 首先启动hadoop,然后就来问题了:zookeeper和hbase的 ...
- 0_Simple__simpleAssert + 0_Simple__simpleAssert_nvrtc
在核函数中使用强制终止函数 assert().并且在静态代码和运行时编译两种条件下使用. ▶ 源代码:静态使用 #include <windows.h> #include <stdi ...
- npm常用命令及版本号浅析
npm 包管理器的常用命令 测试环境为node>=8.1.3&&npm>=5.0.3 1, 首先是安装命令 //全局安装 npm install 模块名 -g //本地安装 ...
- java非阻塞IO(NIO)流程
单线程 多线程(Netty/Mina)