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:
In one line for each case, output "winnable" if it is possible for the player to win, otherwise output "hopeless".
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
hopeless hopeless winnable winnable
G. V. Cormack
用spfa求最长路。因为可能有正圈,所以对于这种情况要特别判断。如果这个圈可以到 n ,则win。所以可以反向建图,从n出发搜索可以到达的点。
#include<cstdio> #include<algorithm> #include<vector> #include<cstring> #include<stack> #include<iostream> #include<queue> #include<cmath> #include<string> #include<set> #include<map> using namespace std; const int maxn = 100 + 5; const int mod = 1000000000 + 7; const double eps = 1e-7; const int INF = 1000000000; typedef long long LL; typedef pair<LL, int> P; vector<int> G[maxn], rG[maxn]; int val[maxn]; int n; int can[maxn]; void dfs(int x){ can[x] = 1; for(int i = 0;i < rG[x].size();i++){ int to = rG[x][i]; if(!can[to]) dfs(to); } } LL dis[maxn], cnt[maxn]; priority_queue<P> pq; bool bfs(){ while(!pq.empty()) pq.pop(); pq.push(P(100, 1)); memset(dis, 0, sizeof dis); memset(cnt, 0, sizeof cnt); dis[1] = 100; while(!pq.empty()){ P p = pq.top(); pq.pop(); LL eng = p.first; int pos = p.second; if(eng < dis[pos]) continue; if(cnt[pos] > n || pos == n){ return true; } for(int i = 0;i < G[pos].size();i++){ int to = G[pos][i]; if(can[to] && dis[pos]+val[to] > dis[to]){ dis[to] = dis[pos]+val[to]; pq.push(P(dis[to], to)); cnt[to]++; } } } return false; } int main(){ while(scanf("%d", &n)){ if(n == -1) break; for(int i = 0;i < maxn;i++){ G[i].clear(); rG[i].clear(); } for(int i = 1;i <= n;i++){ int num; scanf("%d%d", &val[i], &num); while(num--){ int x; scanf("%d", &x); G[i].push_back(x); rG[x].push_back(i); } } memset(can, 0, sizeof can); dfs(n); if(bfs()) printf("winnable\n"); else printf("hopeless\n"); } return 0; } /* 7 0 1 2 0 2 3 5 -100 1 4 -100 1 7 1 1 6 0 1 5 0 0 5 0 1 4 100 1 5 -80 1 2 -90 1 3 0 0 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 7 0 1 2 -98 2 3 5 -1 1 4 101 1 1 -60 1 6 -60 1 7 0 0 8 0 1 2 0 1 3 -5 1 4 -50 1 5 -40 2 6 7 110 1 3 0 1 8 0 0 1 0 0 7 0 2 2 6 -60 1 3 100 1 4 100 1 5 100 1 2 -120 1 7 0 0 15 0 2 2 11 -10 1 3 -10 1 4 -10 1 5 -10 1 6 -10 1 7 -10 1 8 -10 1 9 -10 1 10 -10 1 13 50 1 12 50 1 11 11 2 10 14 -100 1 15 0 0 */