HDU 1317 Xyzzy Domestic SPFA algorithm AC, the topic input part akzent is difficult to understand. Internal analysis

Source: Internet
Author: User

XyzzyTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 3017 Accepted Submission (s): 824


Problem DescriptionIt has recently been discovered what to run Open-source software on the Y-crate gaming device. A number of enterprising designers has developed advent-style games for deployment on the y-crate. Your job is-to-test a number of these designs to see which is winnable.
Each game consists of a set of rooms. One of the rooms is the start and one of the rooms is the finish. Each is a energy value between-100 and +100. One-way doorways interconnect pairs of rooms.

The player begins in the start hostel with the points. She may pass through any doorway that connects the "the" and the "to another", thus entering the other. The energy value of this are added to the player's energy. This process continues until she wins by entering the finish, or dies by running off of energy (or quits in Frustratio n). During Her adventure The player may enter the same-several times, receiving its-energy each time.

Inputthe input consists of several test cases. Each test case is begins with N, the number of rooms. The rooms is numbered from 1 (the start hostel) to n (the finish). Input for the n rooms follows. The input for each of the consists to one or more lines containing:

The energy value for the
The number of doorways leaving I
A list of the rooms that is reachable by the doorways leaving I
The start and finish rooms are always having enery level 0. A Line containing-1 follows the last test case.

Outputin one line for each case, output "winnable" if it's 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
This problem is medium difficulty, the general people have ideas, but may be a bit difficult AC. Let me first say the input and output of this problem, first enter an integer n to represent the number of rooms, the following n rows are all the information of the room, for example, the first number represents the current room energy values, the second number is to have m doors connected to other rooms, The following m-number means that the room represented by the number can be reached from room I. It took me a long time to understand. It is important to note that, when the positive loop is judged, it cannot directly outputwinnable,and decide if you can reach the end. also note that to reach the end of the room, the stamina must be greater than 0, not greater than or equal to 0. Finally, if the problem is preprocessed with Floyd, to determine whether to reach the end point, and then use Bellman-ford or SPFA to determine if there is positive ring, it should be simpler. I'm a pure egg. I want to practice the domestic algorithm SPFA. Here's the code:
#include <cstdio> #include <queue> #include <cstring> #define INF 100000000#define MAX 110using namespace std; int Dis[max],graph[max][max], C[max]; bool Visited[max]; void init (int n) {memset (visited,false,sizeof ( visited)); Memset (C,0,sizeof (c)); for (int i = 0; I <= N; ++i) {for (int j = 0; J <= i; ++j) {graph[i][j] = Graph[j] [I] =-inf;}}} void Spfa (int n) {queue<int> que; for (int i = 0; I <= N; ++i) {dis[i] =-inf;} DIS[1] = Que.push (1), while (!que.empty ()) {int k = Que.front (); Que.pop (); visited[k] = false; C[k]++;if (c[k]>n)//Save In the positive loop, Dis[k] is set to INF, while allowing him to no longer be accessed {dis[k] = INF; Visited[k] = true;} for (int i = 1; I <= n; ++i) {if (graph[k][i] = =-inf) {continue;} else if (dis[k]+graph[k][i]>0) {if (Dis[i]<dis[k] + graph[k][i]) {dis[i] = Dis[k]+graph[k][i]; if (!visited[i]) { Que.push (i); visited[i] = true;}}}}} int main () {int n; ~scanf ("%d", &n) && n!=-1) {init (n); for (int i = 1; I <= n; ++i) {int W, m, X; SCA NF ("%d%d", &w,&AMP;M); for (int j = 0; j < m; ++j) {scanf ("%d", &x); graph[i][x] = w;}} SPFA (n), if (Dis[n] > 0) {puts ("winnable");} Else{puts ("hopeless");}} return 0;}

with June

HDU 1317 Xyzzy Domestic SPFA algorithm AC, the topic input part akzent is difficult to understand. Internal analysis

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.