HDU 1317 xy1_[ bellheman_ford judgment positive ring small application]

Source: Internet
Author: User
Link: http://acm.hdu.edu.cn/showproblem.php? PID = 1317 http://acm.hust.edu.cn/vjudge/contest/view.action? Cid = 29015 # Problem/fxy.pdf

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 1701 accepted submission (s): 419


Problem descriptionit has recently been discovered how to run open-source software on the Y-crate gaming device. A number of parameter Ising 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, processing ing its energy each time.


Inputthe 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.


Outputin one line for each case, output "winnable" if it is possible for the player to win, otherwise output "hopeless ".


Sample Input

50 1 2-60 1 3-60 1 420 1 50 050 1 220 1 3-60 1 4-60 1 50 050 1 221 1 3-60 1 4-60 1 50 050 1 220 2 1 3-60 1 4-60 1 50 0-1
 


Sample output

hopelesshopelesswinnablewinnable
 


Sourceuniversity of Waterloo local
Contest 2003.09.27


RecommendeddyQuestion:
There are n Rooms numbered from 1 to n.
Each time you enter a room, the energy value may increase or decrease.
Q: I will start from the first room and give you 100 energy values.
Ask if you can go to room n.

Enter the number of rooms in line N
Then there are n rows of data:
The first line of data in row I indicates the energy obtained from entering the room [Positive and Negative]
The second one indicates the number of rooms that can be reached from this room num
The remaining num number indicates the number of rooms that can be reached.

Algorithm: bellman_ford () Judge positive ring

Note:YesDirected Graph,
Note the following when creating a graph,Edge is not authorized...
Point right.

Ideas:

In fact, when I first did not look at the questions, I would not think of using bellman_ford () if I didn't see the complaints in the group ()
If a positive ring exists in the figure, you can keep walking through the ring to increase energy,
If the point in the ring can reach N, it will certainly win...
However, due to this preconceived idea, it was easy for me to ignore it. The essence of this question is that there is still energy when it reaches n.
Then there is a variety of indifference, and various wa's history of blood and tears .... Then there are various profound questions on the Internet. Until you see an article
Written by bellman_ford

Simultaneously record the process of adding edges to a graphConnectivityFirst, judge whether 1 and N are connected without considering the energy. [no judgment is acceptable, but it is only an irrelevant optimization.] then, use bellman_ford () to determine whether a positive ring exists.

Note:: When the positive ring does not exist, do not directly jump out of this algorithm.
Because our main purpose is not to judge the positive ring, but to make n there is energy.

There are two possibilities to win:
1. There is no positive ring, but through the relaxation operation of bellman_ford (), the energy value of N is greater than 0.
2. There is a positive ring. The point in the positive ring can reach the end point.

Code:

# Include <stdio. h> # include <string. h ># include <iostream> using namespace STD; const int maxn = 110; const int INF = 10000000000; int W [maxn] [maxn]; // judge the graph connectivity int en [maxn]; // enter the energy value int d [maxn]; // the energy value of each point int n, m; // n points, M side struct edge {int U, V;} edge [maxn * maxn]; void Floyd () // The transfer closure of the directed graph {for (int K = 1; k <= N; k ++) for (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) W [I] [J] = W [I] [J] | (W [I] [k] & W [K] [J]); // do not make a mistake. wa's tears are everywhere ...} Bool bellman_ford () {for (INT I = 1; I <= N; I ++) d [I] =-INF; d [1] = 100; // The first vertex has 100 energy for (INT I = 1; I <n; I ++) {for (Int J = 0; j <m; j ++) {int u = edge [J]. u; int v = edge [J]. v; If (d [v] <D [u] + en [v] & D [u] + en [v]> 0) // relax d [v] = d [u] + en [v]; // Add the vertex permission...} // Note: if you cannot relax like before, return false directly because the purpose of judging the positive ring is to make d [N]> 0} For (INT I = 0; I <m; I ++) {int u = edge [I]. u; int v = edge [I]. v; If (d [v] <D [u] + en [v] & D [u] + en [v]> 0) // if there is a positive ring if (W [v] [N]) // the point in the positive ring can reach the end point return true;} return d [N]> 0; // The positive ring does not exist, determine whether 100 energy values can be used to reach the destination.} int main () {While (scanf ("% d", & N )! = EOF) {If (n =-1) break; M = 0; // initialize the edge memset (W, 0, sizeof (w); memset (EN, 0, sizeof (En); For (INT I = 1; I <= N; I ++) W [I] [I] = 1; int num; for (INT I = 1; I <= N; I ++) {int V; scanf ("% d", & en [I], & num ); while (Num --) // note that it is unidirectional {scanf ("% d", & V); edge [M]. U = I; edge [M ++]. V = V; W [I] [v] = 1; // directed graph. Do not add W [v] [I] = 1} Floyd (); // test the connectivity of the directed graph/* If (! W [1] [N]) {printf ("hopeless \ n"); continue;} */If (bellman_ford () printf ("winnable \ n "); else printf ("hopeless \ n");} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.