HDU 1317 Xyzzy

Source: Internet
Author: User

Xyzzy

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 4099 Accepted Submission (s): 1136


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 Input50 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-6 0 1 50 0-1

Sample outputhopelesshopelesswinnablewinnable

Sourceuniversity of Waterloo Local Contest 2003.09.27

Recommendeddy | We have carefully selected several similar problems for you:1535 1384 1531 1385 1271 The problem is the most difficult short circuit I have encountered so far, to use a new algorithm, copy Online The idea of the great God.

1, first use Floyd algorithm to determine the connectivity of the graph. If 1 is not connected to N, the output is hopeless.

2, using the Bellman_ford algorithm to determine whether there is a positive circle, if a point has a positive circle, and the point is connected to the nth point. The output is winnable. Of course, it is also possible to arrive without a positive circle. Then there is the question of how to find the circle. The Bellman_ford algorithm can determine if there is a negative circle. Bellman_ford is to solve the shortest circuit problem, the core is the relaxation method. If DIST[V]<DIST[U]+MAP[U][V], then dist[v]=dist[u]+map[u][v]. After the cycle n-1, if there is a dist[v]<dist[u]+map[u][v], then there is a negative circle. So we have a way to find the circle: The dist array is initialized to negative infinity. If DIST[V]>DIST[U]+MAP[U][V], then dist[v]=dist[u]+map[u][v]. After the cycle n-1, if there is still dist[v]>dist[u]+map[u][v], then there is a positive circle.

One of the things to note is that. You can go down one room condition is that the current energy value is greater than 0.

Test instructions: There are n rooms (n<=100), each room has a point (room 1th and room N of the weight of 0), when the room will be automatically obtained the right to the point (may be a negative right). Give some non-direction edges. There is a person, the initial energy value of 100, the initial position is the 1th room, to go to room N, and the road must not make the body energy value is less than or equal to 0. Can reach the nth room even if win, ask whether win.

The first line enters N, the next n lines, the first input weights, the second input indicates that the room can reach M rooms, and then enter M reachable rooms.

Test instructions

1#include <iostream>2#include <cstdio>3#include <cstring>4 #defineM 1055 #defineINF 0x3f3f3f3f6 using namespacestd;7 structnode8 {9     intb;Ten} ss[m*M]; One intMap[m][m],pow[m],n; A  - voidFloyd ()//Judging whether you can go from the beginning to the end - { the     inti,j,k; -      for(k=1; k<=n; k++) -          for(i=1; i<=n; i++) -              for(j=1; j<=n; J + +) +                 if(!Map[i][j]) -map[i][j]=map[i][k]&&Map[k][j]; + } A  at intBellman_ford (intLen//Determine if there is a positive ring, if there is, and is connected with n, then the output can be reached - { -     intdis[ the]; -     inti,j,k,a,b; -      for(i=1; i<=n; i++) -dis[i]=-inf; indis[1]= -; -      for(k=1; k<n; k++)//multiple loops, an algorithm to     { +          for(i=0; i<len; i++) -         { theA=ss[i].a; *b=ss[i].b; $             if(Dis[b]<dis[a]+pow[b] && dis[a]+pow[b]>0)Panax Notoginsengdis[b]=dis[a]+Pow[b]; -         } the     } +  A      for(i=0; i<len; i++) the     { +A=ss[i].a; -b=ss[i].b; $         if(Dis[b]<dis[a]+pow[b] && dis[a]+pow[b]>0&&Map[b][n]) $             return 1; -     } -     returnDis[n]>0; the } - Wuyi intMain () the { -     inti,j,len,m,x; Wu      while(~SCANF ("%d",&N)) -     { About         if(n==-1) Break; $memset (SS,0,sizeof(ss)); -memset (POW,0,sizeof(POW)); -memset (Map,0,sizeof(map)); -len=0; A          for(i=1; i<=n; i++) +         { thescanf"%d%d",&pow[i],&m); -              for(j=0; j<m; J + +) $             { thescanf"%d",&x); theSs[len].a=i; thess[len].b=x; themap[i][x]=1; -len++; in             } the         } the Floyd (); About         if(!map[1][n])//If you cannot reach the end, the direct output cannot the         { theprintf"hopeless\n"); the             Continue; +         } -         if(Bellman_ford (len)) theprintf"winnable\n");Bayi         Else theprintf"hopeless\n"); the     } -     return 0; -}

HDU 1317 Xyzzy

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.