HDU 1317 XYZZY Floyd判断能否到达+SPFA判断能否形成环及求最长路(好题)
来源:互联网 发布:淘宝上的骑士装备 编辑:程序博客网 时间:2024/06/11 08:13
点击打开链接
XYZZY
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1710 Accepted Submission(s): 424
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.
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.
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
50 1 2-60 1 3-60 1 420 1 50 050 1 220 1 3-60 1 4-60 1 50 050 1 221 1 3-60 1 4-60 1 50 050 1 220 2 1 3-60 1 4-60 1 50 0-1
Sample Output
hopelesshopelesswinnablewinnable
Source
University of Waterloo Local Contest 2003.09.27
Recommend
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];//如果某个点的次数大于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[n]>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][n]) printf("hopeless\n"); else { if(spfa(1)) printf("winnable\n"); else printf("hopeless\n"); } } return 0;}
- HDU 1317 XYZZY Floyd判断能否到达+SPFA判断能否形成环及求最长路(好题)
- HDU 1317--XYZZY 【spfa判断正环求最长路径 && floyd求传递闭包】
- hdoj 1317 XYZZY 【spfa判断正环求最长路径&&floyd求传递闭包】
- hdu 1317 XYZZY spfa判断负环
- HDU 2039 三角形 判断能否形成三角形
- HDU 1325&&POJ 1308 Is It A Tree? 并查集判断能否树(能否形成环)
- HDU 1317 XYZZY Bellman-Ford求最长路 判断正环
- hdu 1317 XYZZY(floyd+SPFA)
- XYZZY(SPFA变形:判断在有正负环的情况下如何找出最长路)
- [HDU 1317]XYZZY[SPFA变形][最长路]
- HDU - 1317 XYZZY (floyd + 最长路)
- 【HDU】1317 XYZZY spfa判负环+floyd求传递闭包
- HDU 3342 Legal or Not 判断拓补排序能否形成环
- POJ 1932 XYZZY (spfa判断正环+判断是否连通 or SPFA+floyd 求解差分约束)
- HDU:1317 XYZZY(SPFA处理正环+Flody判断点连通)
- 给定初速度 判断球能否到达某个点
- hdu 1317 XYZZY(bellman_ford判断环,有负权)
- 判断二阶魔方能否一步还原 (HDU 5983)
- hdu1233最小生成树 prim算法模板
- 基类子类的this指针相同,但在基类函数永远调用基类的成员变量
- 视图切换的方式
- Hierarchical Storage structure
- Android xml资源文件中@、@android:type、@*、?、@+含义和区别
- HDU 1317 XYZZY Floyd判断能否到达+SPFA判断能否形成环及求最长路(好题)
- windows 下 解决vim乱码
- CentOS 6.3安装VNC报错Could not start Xvnc的解决办法
- note : font operation
- const成员函数和非const的成员函数之间的调用
- codeforces270A
- 栈的原地翻转
- 运算符
- poj 2955 Brackets 【区间DP】