[UVA][正環][spfa] 10557 - XYZZY
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
找到一條 1 到 n 且中間路徑不可小於等於 0, 跟原本負環檢查一樣, 更新次數多於 n 時找到正環,
但此題條件有些不同, 因此找到正環後, 把權值換到無窮大, 之後進行更新, 倘若正環上的點可以連到終點,
則直接輸出 winnable
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int map[101][101], mt[101];
int cap[101], arrive[101][101];
void bfs(int n, int st) {
if(arrive[st][n] == 1)
return;
arrive[st][n] = 1;
int i;
for(i = 0; i < mt[n]; i++)
bfs(map[n][i], st);
}
void spfa(int n) {
if(arrive[1][n] == 0) {
puts("hopeless");
return;
}
int tn, i, j;
int dis[101] = {}, used[101] = {};
int update[101] = {};
queue<int> Q;
memset(dis, -1, sizeof(dis));
dis[1] = 100;
Q.push(1);
while(!Q.empty()) {
tn = Q.front();
Q.pop();
used[tn] = 0;
update[tn]++;
if(dis[tn] >= 0xfffffff)
continue;
if(update[tn] > n) {
if(arrive[tn][n]) {
puts("winnable");
return;
}
dis[tn] = 0xfffffff;
}
for(i = 0; i < mt[tn]; i++) {
if(dis[map[tn][i]] < dis[tn] + cap[map[tn][i]]) {
dis[map[tn][i]] = dis[tn] + cap[map[tn][i]];
if(used[map[tn][i]] == 0) {
used[map[tn][i]] = 1;
Q.push(map[tn][i]);
}
}
}
}
if(dis[n] > 0)
puts("winnable");
else
puts("hopeless");
}
int main() {
int n, m, i, y;
while(scanf("%d", &n) && n > 0) {
memset(mt, 0, sizeof(mt));
memset(arrive, 0, sizeof(arrive));
for(i = 1; i <= n; i++) {
scanf("%d %d", &cap[i], &m);
while(m--) {
scanf("%d", &y);
map[i][mt[i]++] = y;
}
}
for(i = 1; i <= n; i++)
bfs(i, i);
spfa(n);
}
return 0;
}