欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HDU 1317:XYZZY

程序员文章站 2024-02-24 22:26:16
...

XYZZY

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6591    Accepted Submission(s): 1892


Problem Description
It has recently been discovered how to run open-source software on the Y-Crate gaming device. A number of enterprising 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, receiving its energy each time. 
 

Input
The 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. 
 

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

Sample Input

5 0 1 2 -60 1 3 -60 1 4 20 1 5 0 0 5 0 1 2 20 1 3 -60 1 4 -60 1 5 0 0 5 0 1 2 21 1 3 -60 1 4 -60 1 5 0 0 5 0 1 2 20 2 1 3 -60 1 4 -60 1 5 0 0 -1
 

Sample Output

hopeless hopeless winnable winnable
 

Source

题意:出发点就有100点能量,到达一个新点能得到一个正或者负的能量,可走重复的点 ,所以如果有环,且环能得到总能量大于0,则必定可以到最终点 
知识点:flord,sbfa;

首先判断是否联通,若不,则。返回失败。判断是否有正环,有则可成功。


源代码:

#include<bits/stdc++.h>
 using namespace std;
 const int M=105;
 int pet[M][M];
 int vis[M][M];
 int ans[M];//走的次数
 int po[M];//力量
 int ene[M];//房间
 int n;
 int sbfa(int s)
 {
     queue<int> q;
     memset(po,0,sizeof(po));
     po[s] = 100;
     memset(ans,0,sizeof(ans));
     q.push(s);
     while(!q.empty())
     {
         int v = q.front();
         q.pop();
         ans[v]++;
         if(ans[v]>=n) return vis[v][n]; //有正环
         for(int i = 1; i<=n; i++)
         {
             if(pet[v][i]&&po[i]<po[v]+ene[i]&&po[v]+ene[i]>0)
             {
                 po[i] = po[v]+ene[i];

                 q.push(i);
             }
         }
     }
     return po[n]>0;
 }
 int main()
 {
     while(scanf("%d",&n),n!=-1)
     {
         memset(vis,0,sizeof(vis));
         memset(pet,0,sizeof(pet));
         for(int i=1;i<=n;i++)
         {
             int a,b;
             scanf("%d %d",&ene[i],&a);
             for(int j=1;j<=a;j++)
             {
                 scanf("%d",&b);
                 vis[i][b]=1;
                 pet[i][b]=1;
             }
         }
         for(int k=1;k<=n;k++)
         {
             for(int i=1;i<=n;i++)
             {
                 for(int j=1;j<=n;j++)
                 {
                     if(vis[i][j]||(vis[i][k]&&vis[k][j]))
                        vis[i][j]=1;
                 }
             }
         }
         if(!vis[1][n])
         {
            printf("hopeless\n");
            continue;
         }
         if(sbfa(1))
            printf("winnable\n");
         else
            printf("hopeless\n");

     }
     return 0;
 }