码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1317 XYZZY (SPFA 找正环 + Floyd 判连通)

时间:2015-02-10 23:12:56      阅读:437      评论:0      收藏:0      [点我收藏+]

标签:图论   hdu   floyd   spfa   


XYZZY

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 3004    Accepted Submission(s): 817

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
 

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1317


题目大意:有n个房间,每个房间都有对应的能量值(可正可负),现在从1出发要到达n,初始能量为100,问能否够达到n点,到达n的条件是过程中及最后的能量值都要大于0

题目分析:先用Floyd判连通,如果1-n不连通,直接输出hopeless,否则找到正环意味着能量可以无限大直接退出,找到正环的标志是一个点加入队列的次数大于n,若不存在正环,就找权值最大的路,更新p数组,最后根据p[n]的值来判断输赢

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int const MAX = 105;
int n;
int w[MAX], p[MAX], cnt[MAX];
bool map[MAX][MAX], ok[MAX][MAX];

void Floyd()
{
    for(int k = 1; k <= n; k++)
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                if(!ok[i][j])
                    ok[i][j] = ok[i][k] && ok[k][j];
}

bool SPFA(int v0)
{
    memset(p, 0, sizeof(p));
    memset(cnt, 0, sizeof(cnt));
    p[1] = 100;
    queue <int> q;
    q.push(v0);
    while(!q.empty())
    {
        int cur = q.front();
        q.pop();
        cnt[cur]++;
        if(cnt[cur] > n)
            return ok[cur][n];
        for(int i = 1; i <= n; i++)
        {
            if(map[cur][i] && p[i] < p[cur] + w[i])
            {
                q.push(i);
                p[i] = p[cur] + w[i];
            }
        }
    }
    if(p[n] > 0)
        return true;
    return false;
}

int main()
{
    while(scanf("%d", &n) != EOF && n != -1)
    {
        int num, v;
        memset(w, 0, sizeof(w));
        memset(map, false, sizeof(map));
        memset(ok, false, sizeof(ok));
        for(int i = 1; i <= n; i++)
        {
            scanf("%d %d", &w[i], &num);
            for(int j = 1; j <= num; j++)
            {
                scanf("%d", &v);
                map[i][v] = true;
                ok[i][v] = true;
            }
        }
        Floyd();
        if(!ok[1][n])
            printf("hopeless\n");
        else 
        {
            if(SPFA(1))
                printf("winnable\n");
            else 
                printf("hopeless\n");
        }
    }
}


HDU 1317 XYZZY (SPFA 找正环 + Floyd 判连通)

标签:图论   hdu   floyd   spfa   

原文地址:http://blog.csdn.net/tc_to_top/article/details/43710957

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!