Problem D: XYZZY
ADVENT: /ad�vent/, n.The prototypical computer adventure game, first designed by Will Crowther on the PDP-10 in the mid-1970s as an attempt at computer-refereed fantasy gaming, and expanded into a puzzle-oriented game by Don Woods at Stanford in 1976. (Woods had been one of the authors of INTERCAL.) Now better known as Adventure or Colossal Cave Adventure, but the TOPS-10 operating system permitted only six-letter filenames in uppercase. See also vadding, Zork, and Infocom.
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.
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
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
Output for Sample Input
hopeless hopeless winnable winnable 初始能量值为100,没走过一个房间加上该房间的能量值,问能否从起点到终点,能量值相当于边的权值,原问题等价于求2点之间的是否存在距离大于0,
难点在于存在正环,负环,参考了别人的处理方法1.存在正环时并且能到终点则win,否则按照普通的做法,2.spfa,为避免出现正环但不能到达终点的情况导致spfa死循环要设置一个最大的循环次数。总之都是看别人的然后水过的,惭愧
#include <stdio.h> int i,j,m,x,f,k,n,top,tail,q[100],visit[101],low[101],energy[101],map[101][101]; int main() {while (scanf("%d",&n)) {if (n==-1) break; for (i=1;i<=n;i++) for (j=1;j<=n;j++) map[i][j]=0; for (i=1;i<=n;i++) {scanf("%d%d",&energy[i],&m); for (j=1;j<=m;j++) {scanf("%d",&x); map[i][x]=1; } } for (i=1;i<=n;i++) {visit[i]=1; low[i]=0;} visit[1]=0; low[1]=100; q[0]=1; top=0; tail=1; f=0; m=0; while (top!=tail) {for (i=1;i<=n;i++) if ((map[q[top]][i]==1)&&(low[q[top]]+energy[i]>low[i])) {low[i]=low[q[top]]+energy[i]; if (visit[i]==1) //开始把这个条件和上面的if放一起了,每次入队的是未标记的,每次进行松弛操作时不管有没有标记的 {q[tail]=i; tail=(tail+1)%100; visit[i]=0; ++m; } } visit[q[top]]=1; top=(top+1)%100; if (low[n]>0) {f=1;break;} } if (f==1) printf("winnable\n"); else printf("hopeless\n"); } return 0; }