XYZZY(spfa求最长路)
http://acm.hdu.edu.cn/showproblem.php?pid=1317
XYZZY
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3514 Accepted Submission(s): 973
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
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
Source
题意: 每个房间有一个能量值,初始100能量,问是否可以从1走到n
题解: 这个题是用spfa求最长路,并且判环很巧妙的写法
用一个数组存放点的权值,每次走进这个点的时候如果从起点到这个点的剩余能量加上这个点的能量大于0 的话就可以走出去,代码中给出详细的注释
说明:在求最长路的时候只能用spfa或者是Floyd不能用dijk(因为dijk使用贪心写的,所以会有问题)spfa在判环的时候很灵活,再建图的时候也很灵活,如果所给的是点权值而不是边权的时候可以用一个点的数组来存点权,帅气。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 #define N 110 6 #define INF 0x1fffffff 7 int node[N]; 8 struct Edge{ 9 int to; 10 int next; 11 }edge[N*N]; 12 int dis[N]; 13 int que[N*N]; 14 bool inq[N]; 15 int cnt[N]; 16 int n; 17 int top; 18 int head[N]; 19 int Enct; 20 void add(int from , int to) 21 { 22 edge[Enct].to = to; 23 edge[Enct].next = head[from]; 24 head[from] = Enct++; 25 } 26 void init() 27 { 28 Enct = 0; 29 memset(head,-1,sizeof(head)); 30 } 31 void spfa() 32 { 33 top = 0; 34 for(int i =1 ;i <= n ;i++) 35 inq[i] = false,cnt[i] = 0,dis[i]= -INF;//要定义成一个没有意义的数-INF 36 que[top++] = 1; 37 dis[1] = 100; 38 inq[1] = true; 39 cnt[1]++; 40 for(int i= 0 ; i < top ; i++) 41 { 42 int u = que[i]; 43 inq[u] = false; 44 if(dis[u] + node[u] <= 0) continue;//没办法走出这个点所以不再用这个点去更新其他的点 45 for(int j = head[u] ; j!=-1 ;j = edge[j].next) 46 { 47 Edge e = edge[j]; 48 if(dis[e.to]<dis[u]+node[u])//如果下一个点的到起点的dis小于这个点到起点的dis加上这个点自己的权值的话 更新为最长录路 49 { 50 dis[e.to] = dis[u] + node[u]; 51 if(inq[e.to]==false) 52 { 53 // que[top++] = e.to; 54 // inq[e.to] = true; 55 cnt[e.to]++; 56 if(cnt[e.to]>n) continue;//这里在判定环的时候在这个点在第n次加入队列的时候证明有环,这时候仍把它加入队列,并用这个点权值更新为INF 57 //并用这个点去更新这个环所在路径的值都为INF这样就可以解决有正环但却不一定与起点终点联通的问题了,因为这样处理后这个环只能更新其路径上的值 58 else if(cnt[e.to]==n) 59 dis[e.to] = INF;//更新 60 que[top++] = e.to; 61 inq[e.to] = 1; 62 } 63 } 64 } 65 } 66 }//spfa在用的时候一定要注意其编号是不是从1开始编号的 还是从0,注意 67 int main() 68 { 69 while(~scanf("%d",&n)&&n!=-1) 70 { 71 init(); 72 int a,cn,t; 73 int tm = 0; 74 for(t = 1 ; t <= n ;t++ ) 75 { 76 scanf("%d%d",&a,&cn); 77 node[t] = a; 78 for(int i =0 ;i < cn;i++) 79 { 80 int to; 81 scanf("%d",&to); 82 add(t,to); 83 } 84 } 85 spfa(); 86 // for(int i = 1; i <= n; i++) printf("%d ", dis[i]); puts(""); 87 if(dis[n]>0)puts("winnable"); 88 else puts("hopeless"); 89 } 90 return 0; 91 }