Title: There are many rooms in a maze, and their relationship is initialized with a 100 energy value,
Each energy value in a room gets the amount of energy (which can be negative) in the anterior chamber,
If the energy is not moved by the negative, the room can be reversed (the value of the response can be regained),
Ask from Room 1, can go to room N.
Analysis: the shortest way. With the SPFA iterative solution, the final value can be found.
When initialized to all rooms, the energy value is zero, and the maximum energy value of each room can be solved by SPFA.
If there is an increasing loop, then the iteration to the upper bound is stopped (100n, which is positive from every point he can reach)
(You can also use search nesting to find out if the outer layer will be able to reach the end of the environment and whether the layer can reach the end of the loop)
╮(╯▽╰)╭: It's a very small problem to write this month.
#include <cstring> #include <cstdio>int energy[101];//link_list definetypedef struct _link_list{int point; _link_list* Next;} link_list;link_list* link_head[105];link_list link_node[20202];int link_size;void link_initial () {link_size = 0;memse T (link_head, 0, sizeof (link_head));} void Link_insert (int a, int b) {link_node[link_size].point = B;link_node[link_size].next = Link_head[a];link_head[a] = & Amp;link_node[link_size + +];} Link_list endint instack[105],stack[105],power[105];int SPFA (int s, int n) {for (int i = 1; I <= n; + + i) instack[i] = Power[i] = 0;int top = 0, Now;stack[top + +] = S;power[s] = 100;instack[s] = 1;while (top) {now = stack[--top];for (link_ List*p = Link_head[now]; P p = p->next) {if (Power[p->point] > 100*n) continue;if (Power[p->point] < Power[now]+energy[p->point]) {Power[p->point] = power[now]+energy[p->point];if (!instack[p->point]) {if (P->point = = N) return 1;instack [P->point] = 1;stack[top + +] = p->point;}}}instack[now] = 0;} return power[n];} int main () {int n,m,v;while (~scanf ("%d", &n) && n! =-1) {//Read data & Build Link_initial (); for (int i = 1; I <= n ; + + i) {scanf ("%d%d", &energy[i],&m), for (int j = 0; J < m; + + j) {scanf ("%d", &v); Link_insert (i, v);}} if (n = = 1 | | SPFA (1, N)) printf ("winnable\n"), Else printf ("hopeless\n");} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVa 10557-xyzzy