POJ - 1932 XYZZY 【SPFA】
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.
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.
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 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
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
Sample Output
hopeless hopeless winnable winnable
题意:
给一个有向图,每个点都有一个权值。
1是起点,n是终点。
每个人从起点开始走,初始生命值为100,每到一个点,生命值就加上这个点的权值。
权值可能是负数。
所以生命值可能到0,到0就死了
问能否活着从起点走到终点。
每个点可以走多次。
做法:
判断有无正权回路,如果有,并且可以活着从起点到这个回路,并且这个回路也可以走向终点。
那么,就是就一定能赢。
当然,如果可以正常走到终点,还活着,那么也能赢。
我是把正负权值颠倒,SPFA判断负环,去解决正环问题。
代码:
1 #include<iostream> 2 using namespace std; 3 #include<cstdio> 4 #include<cstring> 5 #include<queue> 6 #include<vector> 7 struct Data{ 8 int f; 9 int p; 10 bool operator<(const struct Data &pt)const{ 11 return f>pt.f; 12 } 13 }; 14 const int maxn = 1200; 15 int f[maxn]; 16 int a[maxn]; 17 int is_circle[maxn]; 18 int start_open[maxn]; 19 int v[maxn]; 20 int end_open[maxn]; 21 vector<int> maps[maxn]; 22 vector<int> re_maps[maxn]; 23 void init(int n){ 24 memset(is_circle,0,sizeof(is_circle)); 25 memset(start_open,0,sizeof(start_open)); 26 memset(end_open,0,sizeof(end_open)); 27 memset(f,0x3f3f3f3f,sizeof(f)); 28 memset(v,0,sizeof(v)); 29 memset(a,0,sizeof(a)); 30 start_open[1] = 1; 31 end_open[n] = 1; 32 for(int i=0;i<n;i++) 33 maps[i].clear(),re_maps[i].clear(); 34 } 35 priority_queue<Data> team; 36 void spfa(int s,int ok[],vector<int> maps[]){ 37 Data pt; 38 pt.p = s; 39 pt.f = -100+a[1]; 40 while(!team.empty()) 41 team.pop(); 42 team.push(pt); 43 while(!team.empty()){ 44 pt = team.top(); 45 team.pop(); 46 int p = pt.p; 47 for(int i=0;i<maps[p].size();i++){ 48 int np = maps[p][i]; 49 if(ok[np]==1) 50 continue; 51 ok[np] = 1; 52 Data pd; 53 pd.p = np; 54 pd.f = pt.f + 1; 55 team.push(pd); 56 } 57 } 58 } 59 bool get_ans(int n){ 60 Data pt; 61 pt.f = a[1]-100; 62 pt.p = 1; 63 f[1] = pt.f; 64 while(!team.empty()) 65 team.pop(); 66 team.push(pt); 67 while(!team.empty()){ 68 pt = team.top(); 69 team.pop(); 70 //cout<<pt.p<<" "<<f[pt.p]<<endl; 71 if(f[pt.p]<pt.f) 72 continue; 73 if(pt.f>=0) 74 continue; 75 if(v[pt.p]>n){ 76 continue; 77 } 78 v[pt.p]+=1; 79 if(pt.p==n){ 80 if(f[pt.p]<0){ 81 return 1; 82 } 83 } 84 if(v[pt.p]>n){ 85 is_circle[pt.p] = 1; 86 continue; 87 } 88 for(int i=0;i<maps[pt.p].size();i++){ 89 int np = maps[pt.p][i]; 90 if(f[np]>f[pt.p]+a[np]){ 91 Data pd; 92 pd.p = np; 93 pd.f = f[pt.p]+a[np]; 94 f[np] = pd.f; 95 team.push(pd); 96 } 97 } 98 } 99 for(int i=1;i<=n;i++){ 100 // cout<<i<<" "<<start_open[i]<<" "<<end_open[i]<<endl; 101 if(is_circle[i]==1&&start_open[i]==1&&end_open[i]==1) 102 return 1; 103 } 104 return 0; 105 } 106 int main(){ 107 int n; 108 while(scanf("%d",&n)!=EOF&&n>=0){ 109 init(n); 110 for(int i=1;i<=n;i++){ 111 scanf("%d",&a[i]); 112 a[i]*=-1; 113 int k = 0; 114 scanf("%d",&k); 115 for(int j=0;j<k;j++){ 116 int p; 117 scanf("%d",&p); 118 maps[i].push_back(p); 119 re_maps[p].push_back(i); 120 } 121 } 122 spfa(1,start_open,maps); 123 spfa(n,end_open,re_maps); 124 int ans = get_ans(n); 125 if(ans) 126 printf("%s\n","winnable"); 127 else 128 printf("%s\n","hopeless"); 129 } 130 return 0; 131 }