HDU 1317 XYZZY Floyd判断能否到达+SPFA判断能否形成环及求最长路(好题)
2013-08-13 18:59
351 查看
点击打开链接
Total Submission(s): 1710 Accepted Submission(s): 424
[/b]
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
In one line for each case, output "winnable" if it is possible for the player to win, otherwise output "hopeless".
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
hopeless
hopeless
winnable
winnable
[align=left]Source[/align]
University of Waterloo Local Contest 2003.09.27
[align=left]Recommend[/align]
Eddy
题意是说有n个房间,然后给你每个房间能够到达的房间,每个房间还有一个能量值(有正有负),让你从第一个房间出发判断能否到达第n个房间,一开始你有100能量值,如果能量值为负则失败,当然你也可以从一个房间在返回已经走过的并且可到达的房间去补充能量值。
首先你要判断他能不能到达n号房间,如果到达不了,则直接失败。如果能够到达,则判断他的能量值是否大于0,如果有环的话,则证明此点加入队列的次数要大于等于n,然后判断此点能否到达n即可。无环的话,则求从1号点到n号点的最长路即可。
XYZZY
[b]Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1710 Accepted Submission(s): 424
[/b]
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
In one line for each case, output "winnable" if it is possible for the player to win, otherwise output "hopeless".
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
hopeless
hopeless
winnable
winnable
[align=left]Source[/align]
University of Waterloo Local Contest 2003.09.27
[align=left]Recommend[/align]
Eddy
题意是说有n个房间,然后给你每个房间能够到达的房间,每个房间还有一个能量值(有正有负),让你从第一个房间出发判断能否到达第n个房间,一开始你有100能量值,如果能量值为负则失败,当然你也可以从一个房间在返回已经走过的并且可到达的房间去补充能量值。
首先你要判断他能不能到达n号房间,如果到达不了,则直接失败。如果能够到达,则判断他的能量值是否大于0,如果有环的话,则证明此点加入队列的次数要大于等于n,然后判断此点能否到达n即可。无环的话,则求从1号点到n号点的最长路即可。
#include<stdio.h> #include<string.h> #include<queue> using namespace std; int n; bool g[107][107],reach[107][107]; int energy[107],power[107],count[107]; void floyd() { for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) for(int k=1;k<=n;k++) reach[j][k]=(reach[j][k]||reach[j][i]&&reach[i][k]); } bool spfa(int now) { queue<int>q; memset(power,0,sizeof(power)); memset(count,0,sizeof(count)); q.push(now); power[1]=100; while(!q.empty()) { int now=q.front(); q.pop(); count[now]++; if(count[now]>=n) return reach[now] ;//如果某个点的次数大于n,则存在正环 for(int i=1;i<=n;i++) if(g[now][i]&&power[now]+energy[i]>power[i]&&power[now]+energy[i]>0) { q.push(i); power[i]=power[now]+energy[i]; } } return power >0; } int main() { while(scanf("%d",&n)!=EOF) { if(n==-1)break; memset(g,false,sizeof(g)); memset(reach,false,sizeof(reach)); int num,door; for(int i=1;i<=n;i++) { scanf("%d%d",&energy[i],&num); while(num--) { scanf("%d",&door); g[i][door]=true; reach[i][door]=true; } } floyd(); if(!reach[1] ) printf("hopeless\n"); else { if(spfa(1)) printf("winnable\n"); else printf("hopeless\n"); } } return 0; }
相关文章推荐
- HDU 3342 Legal or Not 判断拓补排序能否形成环
- HDU 2039 三角形 判断能否形成三角形
- HDU 1325&&POJ 1308 Is It A Tree? 并查集判断能否树(能否形成环)
- hdu 1317 SPFA+连通判断+最长路
- HDU 6201 2017沈阳网络赛 树形DP或者SPFA最长路
- 给定初速度 判断球能否到达某个点
- 最短路 SPFA 判断负环 静态邻接表(链式前向星) HDU 2544 最短路 POJ 3259 Wormholes
- hdu 5971 Wrestling Match 判断能否构成二分图 [Problem A]2016ACM/ICPC亚洲区大连站
- hdu 1317 spfa判环求最长路
- 深度优先算法--判断迷宫的一个起点能否到达一个终点
- hdu 1534(差分约束+spfa求最长路)
- [HDU 1317]XYZZY[SPFA变形][最长路]
- HDU 1317 XYZZY Bellman-Ford求最长路 判断正环
- HDU 1317--XYZZY 【spfa判断正环求最长路径 && floyd求传递闭包】
- 搜索之广度优先【三维迷宫】(判断从一点能否到达另一点,最少走几步)
- HDU 1317/POJ 1932 Floyd + Spfa 最长路
- HDU 1317(Floyd判断连通性+spfa判断正环)
- HDU1224 Free DIY Tour(最短路变形,最长路打印路径,spfa)
- (floyd 1.1)hdu 1217 Arbitrage(使用floyd来求最长路——判断是否存在一种货币,经过一个兑换回路以后>=1单元)
- XYZZY(SPFA变形:判断在有正负环的情况下如何找出最长路)