XYZZY(SPFA判正环)

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
#include<bits/stdc++.h>
using namespace std;
const int minn=-0x3f3f3f3f;
int MAP[111][111],dis[111],vis[111],num[111];
int n,k,x,energy;
void Init()
{
    for(int i=1; i<=n; i++)
    {
        dis[i]=minn;
        vis[i]=0;
        num[i]=0;
        for(int j=1; j<=n; j++)
        {
            if(i==j)
                MAP[i][j]=0;
            else
                MAP[i][j]=minn;
        }
    }
}
int SPFA(int s)
{
    dis[s]=100;
    queue<int>que;
    que.push(s);
    num[s]=1;
    while(!que.empty())
    {
        int u=que.front();
        que.pop();
        vis[u]=0;
        if(num[u]==n+2)//不加会超时。。
            continue;
        if(num[u]==n+1)//注意这里即使有正环也不能直接return1 ,因为有可能不是连通图!
            dis[u]=100000;
        if(dis[n]>0)//到达终点才能return 1
            return 1;
        for(int v=1; v<=n; v++)
        {
            if(dis[v]<dis[u]+MAP[u][v]&&dis[u]+MAP[u][v]>0)//最长路,不等式改变方向即可,注意走完该步一定要保证血量大于0,剩下的就是常规操作了
            {
                dis[v]=dis[u]+MAP[u][v];
                if(vis[v])
                    continue;
                vis[v]=1;
                que.push(v);
                num[v]++;
            }
        }
    }
    return 0;
}
int main()
{
    while(cin>>n&&n!=-1)
    {
        Init();
        for(int i=1; i<=n; i++)//按输入建图
        {
            cin>>energy>>k;
            for(int j=1; j<=k; j++)
            {
                cin>>x;
                MAP[i][x]=energy;
            }
        }
        int x=SPFA(1);
        if(x)
            cout<<"winnable"<<endl;
        else
            cout<<"hopeless"<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43824158/article/details/89205453
今日推荐