1. 程式人生 > >poj 1932 XYZZY(spfa最長路+判斷正環+floyd求傳遞閉包)

poj 1932 XYZZY(spfa最長路+判斷正環+floyd求傳遞閉包)

XYZZY
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 4154   Accepted: 1185

Description

The prototypical computer adventure game, first designed by Will Crowther on the PDP-10 in the mid-1970s as an attempt at computer-refereed fantasy gaming, and expanded into a puzzle-oriented game by Don Woods at Stanford in 1976. (Woods had been one of the authors of INTERCAL.) Now better known as Adventure or Colossal Cave Adventure, but the TOPS-10 operating system permitted only six-letter filenames in uppercase. See also vadding, Zork, and Infocom.


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

Waterloo local 2003.09.27 題目意思:
n個點,每個點有一個權值,存在負權值
構成一個有向圖,點從1到n編號,起點是1,終點是n
起點和終點的權值都為0
每個點可以走多次
現在又一個人,從起點開始走,他開始具有100的能量,走到一個點,就加上這個點的權值
如果能量小於等於0的話就會死去,不能到達終點
問你他是否可以到達終點 分析:
每個點可以走多次,意味著只要存在正環且正環上的某個點到n是可達的(所以需要求傳遞閉包)
那麼他一定可以到達n點,這是第一種情況
第二種情況就是跑個最長路,然後dis[n]是大於0的
這兩種情況都是可以到達n的 注意最長路dis初始化0......  
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
#define INF 99999999
#define me(a,x) memset(a,x,sizeof(a))
int mon1[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
int mon2[13]= {0,31,29,31,30,31,30,31,31,30,31,30,31};
int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};

int getval()
{
    int ret(0);
    char c;
    while((c=getchar())==' '||c=='\n'||c=='\r');
    ret=c-'0';
    while((c=getchar())!=' '&&c!='\n'&&c!='\r')
        ret=ret*10+c-'0';
    return ret;
}
void out(int a)
{
    if(a>9)
        out(a/10);
    putchar(a%10+'0');
}

#define max_v 105
int vis[max_v];
int dis[max_v];
int a[max_v];
int G[max_v][max_v];
int cnt[max_v];
int n;
int kk;

void init()
{
    kk=0;
    me(a,0);
    me(vis,0);
    me(G,0);
    me(cnt,0);
    me(dis,0);
}
void floyd()
{
    for(int k=1;k<=n;k++)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(i==j)
                    continue;
                if(G[i][k]&&G[k][j])
                    G[i][j]=1;
            }
        }
    }
}
int spfa(int s)
{
    queue<int> q;
    q.push(s);
    vis[s]=1;
    dis[s]=a[s];
    cnt[s]++;

    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;

        for(int i=1;i<=n;i++)
        {
            if(G[u][i]&&dis[i]<dis[u]+a[i])
            {
                dis[i]=dis[u]+a[i];
                if(vis[i]==0)
                {
                    q.push(i);
                    vis[i]=1;
                    cnt[i]++;
                    if(cnt[i]>n)
                    {
                        kk=i;
                        return 0;
                    }
                }
            }
        }
    }
    return 1;
}
int main()
{
    while(~scanf("%d",&n))
    {
        if(n<0)
            break;
        int x,y,z;
        init();
        for(int i=1;i<=n;i++)
        {
            scanf("%d %d",&x,&y);
            a[i]=x;
            while(y--)
            {
                scanf("%d",&z);
                G[i][z]=1;
            }
        }
        a[1]=100;
        int flag=spfa(1);
        floyd();
        if((flag==0&&G[kk][n])||dis[n]>0)
        {
            printf("winnable\n");
        }else
        {
            printf("hopeless\n");
        }
    }
    return 0;
}
/*
題目意思:
n個點,每個點有一個權值,存在負權值
構成一個有向圖,點從1到n編號,起點是1,終點是n
起點和終點的權值都為0
每個點可以走多次
現在又一個人,從起點開始走,他開始具有100的能量,走到一個點,就加上這個點的權值
如果能量小於等於0的話就會死去,不能到達終點
問你他是否可以到達終點

分析:
每個點可以走多次,意味著只要存在正環且正環上的某個點到n是可達的(所以需要求傳遞閉包)
那麼他一定可以到達n點,這是第一種情況
第二種情況就是跑個最長路,然後dis[n]是大於0的
這兩種情況都是可以到達n的

注意最長路dis初始化0......

*/