HDU 1317XYZZY spfa+判断正环+链式前向星(感觉不对,但能A)

XYZZY

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5304    Accepted Submission(s): 1510

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

思路:
单向路径。判断是否存在正环,初始化距离数组为负无穷小,进入n次,说明存在正环,将距离改为无穷大。进入n+1次,直接跳过。
代码:

 1 #include<iostream>
 2 #include<string>
 3 #include<algorithm>
 4 #include<vector>
 5 #include<queue>
 6 #include<cstdlib>
 7 #include<cstring>
 8 #include<cstdio>
 9 #include<cmath>
10 using namespace std;
11 const int maxn=105;
12 const int maxm=10005;
13 const int INF=0x3f3f3f3f;
14 struct edgenode {
15     int to,w,next;
16 }edges[maxm];
17 bool vis[maxn];
18 int dist[maxn],du[maxn],head[maxn];
19 int n,cnt;
20 void init() {
21     for(int i=0;i<maxn;++i) head[i]=-1;
22     for(int i=0;i<maxm;++i) edges[i].next=-1;
23     cnt=0;
24 }
25 void addedge(int u, int v, int w) {
26     edges[cnt].to=v;
27     edges[cnt].w=w;
28     edges[cnt].next=head[u];
29     head[u]=cnt++;
30 }
31 bool spfa() {
32     memset(vis,false,sizeof(vis));
33     memset(du,0,sizeof(du));
34     for(int i=0;i<maxn;++i) dist[i]=-INF;
35     queue<int> q;
36     dist[1]=100;vis[1]=true;
37     q.push(1);
38     while(!q.empty()) {
39         int now=q.front();q.pop();
40         vis[now]=false;
41         du[now]++;
42         if(du[now]>n) continue;
43         if(du[now]==n) dist[now]=INF;
44         for(int i=head[now];~i;i=edges[i].next) {
45             if(dist[edges[i].to]<dist[now]+edges[i].w&&dist[now]+edges[i].w>0) {
46                 dist[edges[i].to]=dist[now]+edges[i].w;
47                 if(edges[i].to==n) return true;
48                 if(!vis[edges[i].to]) {
49                     vis[edges[i].to]=true;
50                     q.push(edges[i].to);
51                 }
52             }
53         }
54     }
55     return false;
56 }
57 int main() {
58     while(scanf("%d",&n)&&n!=-1) {
59         int w,num,id;
60         init();
61         for(int i=1;i<=n;++i) {
62             scanf("%d%d",&w,&num);
63             for(int j=1;j<=num;++j) {
64                 scanf("%d",&id);
65                 addedge(i,id,w);
66             }
67         }
68         if(spfa()) printf("winnable\n");
69         else printf("hopeless\n");
70     }
71     return 0;
72 }

时间: 2024-11-02 10:42:30

HDU 1317XYZZY spfa+判断正环+链式前向星(感觉不对,但能A)的相关文章

Currency Exchange POJ - 1860 spfa判断正环

//spfa 判断正环 #include<iostream> #include<queue> #include<cstring> using namespace std; const int N=1e4; const int INF=2e9; int h[N],to[N],ne[N],idx; double r[N],c[N]; int n, m,X; double V; void add(int u,int v,double r1,double c1) { to[id

单元最短路径算法模板汇总(Dijkstra, BF,SPFA),附链式前向星模板

一:dijkstra算法时间复杂度,用优先级队列优化的话,O((M+N)logN)求单源最短路径,要求所有边的权值非负.若图中出现权值为负的边,Dijkstra算法就会失效,求出的最短路径就可能是错的. 设road[i][j]表示相邻的i到j的路长U集合存储已经求得的到源点最短路径的节点,S集合表示还没求得的节点dis[i]表示i到源节点(设为0)的最短路径vis[i]=1表示i节点在U集合中 刚开始dis[0]=0,vis[0]=1;dis[i]=maxn,vis[i]=0;for 1 to

最短路 spfa 算法 &amp;&amp; 链式前向星存图

推荐博客  https://i.cnblogs.com/EditPosts.aspx?opt=1 http://blog.csdn.net/mcdonnell_douglas/article/details/54379641 spfa  自行百度 说的很详细 spfa 有很多实现的方法  dfs  队列  栈  都可以 时间复杂度也不稳定 不过一般情况下要比bellman快得多 #include <stdio.h> #include <math.h> #include <st

UESTC30-最短路-Floyd最短路、spfa+链式前向星建图

最短路 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的T-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗? Input 输入包括多组数据. 每组数据第一行是两个整数NN ,MM (N≤100N≤100 ,M≤10000M≤1000

最短路 SPFA()链式前向星

在极端情况下,图特别大,用邻接链表也会超空间限制,此时需要用到链式前向星来存图. 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 const int inf = INT_MAX / 10; 5 const int num = ???; 6 struct Edge{ 7 int to, next, w;//edge[i]的i就是起点,终点to,权值w,相同起点的下一条边next 8 }edge[num]; 9 int n, m, cnt;

HDU3342 Legal or Not【拓扑排序】【链式前向星】

Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4633    Accepted Submission(s): 2115 Problem Description ACM-DIY is a large QQ group where many excellent acmers get together. It is

图的存储结构:邻接矩阵(邻接表)&amp;链式前向星

[概念]疏松图&稠密图: 疏松图指,点连接的边不多的图,反之(点连接的边多)则为稠密图. Tips:邻接矩阵与邻接表相比,疏松图多用邻接表,稠密图多用邻接矩阵. 邻接矩阵: 开一个二维数组graph[ ][ ]来记录图中点a与点b之间是否连通,初始化为0(或者-1之类的看情况):如果图中有可忽略的重边(如 只需重边中的最小边或最大边),则保存需要的那条边的边权,但如果有无法忽略的重边,就一定不要用邻接矩阵. int graph[MAXN][MAXN]; void graphInit() { me

关于MOD&amp;链式前向星-2015年9月25日

好久没写了,其实一直也在做,但是没心情写总结文章,所以还是以后做一点就写一点吧. 最近状态很差很差,打水题都过不了,我也是醉了. 重做了去年的两道noip题,D1T2和D2T3,里面包括了MOD运算. MOD运算基本满足四则运算,所以很多表达式在模p意义下都是等价的,但除法除外. 但是还要注意,需要在非负情况下进行计算,当出现减法时,一定要加到正值再做. 另外注意溢出的问题,及时取模也是很重要的. noip2014D1T2可以用链式前向星存图. 大概就是这样: struct Edge{ int

hdu2647 逆拓扑,链式前向星。

原文地址 题目分析 题意 老板发工资,但是要保证发的工资数满足每个人的期望,比如A期望工资大于B,只需比B多1元钱即可.老板发的最低工资为888元.输出老板最少发的工资总数,若是无法满足大家的期望,则输出-1. 分析 很明显这是一个拓扑问题,若存在环则无法满足大家的期望.若按常理,A>B,则可能会建立A指向B的有向边.此题不然,因为我们只知道最少的钱数是888,所以从小到大进行拓扑排序更为恰当.所以是建立B指向A的有向边.此之为逆拓扑排序.因为这样处理后排序的结果与原先拓扑排序的顺序相反. 以图