Let's talk about:
This question is relatively difficult, because it was thought of several times later. First, give you a series of nodes with serial numbers ranging from 1 to n, and tell you the serial numbers of each node that can enter the next node. Assume that you have a capacity of 100 at the beginning, and now you are required to start from node 1 and continue to enter another node from one node, of course, when you enter, you must add the value represented by the node (range:-100 to + 100 ). You can enter the same node multiple times. If your capacity is less than or equal to 0, you fail. Is there any way to go from node 1 to node n. Next let's talk about the solution: First of all, the most painful thing is that a node can repeat it. In this way, if the DFS is continuously running, it is very likely that the endless loop is coming. So I set an array to mark the energy value when I enter a node for the first time. However, after careful consideration, it is not necessary to go to the last node N, as long as the node can be reached again, and the subsequent energy value is greater than later, therefore, it must be able to successfully reach the final node. However, this idea is actually wrong, because it is a one-way graph, it is very likely to be circled in a circle, and then it will crash. Therefore, it is necessary to determine whether each node can reach the final node regardless of its energy value. In this issue report, I am actually using a stupid method, that is, to repeat each node DFS, as long as the final node is used to reverse the DFS, it is OK to mark a node. Based on the above analysis, we can start DFS from node 1. When we reach a node again, and when we reach a node that cannot reach the final node or reach a node again, if the current energy value is less than or equal to the energy value obtained for the first time, stop the node and stop DFS. When the node reaches a node again and the energy value is greater than the first time, the node can also reach the final node, so the whole traversal is successful. (Omitted obvious successes or failures) for detailed troubleshooting steps, please read the following source code.
Source code:
# Include <stdio. h> # include <string. h> # define maxn 100 + 5int room [maxn]; // the energy value of each node int roomconnect [maxn] [maxn]; // number of nodes that can be reached by the node and the first element is the number of nodes that can be reached int roomenergy [maxn]; // the energy value int roomvisit [maxn] For the first arrival of a node; // DFS mark char abletofinish [maxn]; // whether the node can reach the final node int energy; int N; int EDFs (INT ); // consider the energy value int DFS (INT); // judge whether a node can reach the final node, regardless of the energy value int main () {int I, J; // freopen ("data", "r", stdin); While (scanf ("% d", & N) & n! =-1) {energy = 100; for (I = 1; I <= N; I ++) {scanf ("% d", & Room [I]); scanf ("% d", & roomconnect [I] [0]); For (j = 1; j <= roomconnect [I] [0]; j ++) scanf ("% d", & roomconnect [I] [J]) ;}for (I = 1; I <= N; I ++) {roomenergy [I] =-1; memset (roomvisit, 0, sizeof (roomvisit); abletofinish [I] = DFS (I ); // determine whether each node can reach the final node without considering the energy value} If (EDFs (1) printf ("winnable \ n "); else printf ("hopeless \ n");} return 0;} int EDFs (INT num) {int I; Energy + = room [num]; If (! Abletofinish [num]) {// This node cannot reach the final node energy-= room [num]; // pay attention to return 0 ;} else if (Energy <= roomenergy [num] | energy <= 0) {// reach a node again, and the energy value is less than or equal to the value of the first time, then, you no longer need to search for Energy-= room [num]; return 0;} else if (num = N & Energy> 0 | roomenergy [num]! =-1 & Energy> roomenergy [num]) {// reach a node again, and the energy value is greater than the first time, and the node can reach the final node, energy-= room [num]; return 1;} roomenergy [num] = energy; for (I = 1; I <= roomconnect [num] [0]; I ++) if (EDFs (roomconnect [num] [I]) {energy-= room [num]; return 1;} return 0;} int DFS (INT num) {int I; If (num = N) return 1; roomvisit [num] = 1; for (I = 1; I <= roomconnect [num] [0]; I ++) if (! Roomvisit [roomconnect [num] [I]) if (DFS (roomconnect [num] [I]) return 1; return 0 ;}
Xysyaxa 10557 (similar to the longest Path Problem of a directed graph)