HDU1317:XYZZY(Floyd+Bellman)

原创 2013年12月03日 21:34:31
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
 


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

 

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define inf 99999999
using namespace std;

struct node
{
    int now,to;
} e[10005];
int first[105],next[10005];
int power[105],map[105][105],n;

void add_edge(node e,int k)
{
    next[k] = first[e.now];
    first[e.now] = k;
}

void Floyd()
{
    int i,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];
}

int bellman_ford(int len)
{
    int dis[105];
    int i,k,now,to;
    for(i = 1; i<=n; i++)
        dis[i] = -inf;
    dis[1] = 100;
    for(k = 0; k<n-1; k++)
    {
        for(i = 0; i<len; i++)
        {
            now = e[i].now;
            to = e[i].to;
            if(dis[to]<dis[now]+power[to] && dis[now]+power[to]>0)
                dis[to] = dis[now]+power[to];
        }
    }
    for(i = 0; i<len; i++)
    {
        now = e[i].now;
        to = e[i].to;
        if(dis[to]<dis[now]+power[to] && dis[now]+power[to]>0 && map[to][n])//判断环,要注意判断这个环能到终点
            return 1;
    }
    return dis[n]>0;
}

int main()
{
    int i,j,len,m;
    while(~scanf("%d",&n),n>0)
    {
        memset(e,0,sizeof(e));
        memset(power,0,sizeof(power));
        memset(map,0,sizeof(map));
        memset(first,-1,sizeof(first));
        memset(next,-1,sizeof(next));
        len = 0;
        for(i = 1; i<=n; i++)
        {
            scanf("%d%d",&power[i],&m);
            for(j = 0; j<m; j++)
            {
                int x;
                scanf("%d",&x);
                e[len].now = i;
                e[len].to = x;
                map[i][x] = 1;
                add_edge(e[len],len);
                len++;
            }
        }
        Floyd();
        if(!map[1][n])
        {
            printf("hopeless\n");
            continue;
        }
        if(bellman_ford(len))
            printf("winnable\n");
        else
            printf("hopeless\n");
    }

    return 0;
}


 

版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU1317( 负权处理)

题意:有n个房间(n 思路: 1.首先可以发现,只要知道到第n个房间时最大可以获得多少能量值(当然必须保证中途都大于0),就能知道能否赢。 2.考虑到负环情况,跑SPFA的时候最...
  • u013445530
  • u013445530
  • 2015年03月04日 21:27
  • 889

hdu1317XYZZY (SPFA处理 有环的情况)

Problem Description It has recently been discovered how to run open-source software on the Y-Crate g...
  • u010372095
  • u010372095
  • 2013年12月07日 00:08
  • 1254

HDU1317:XYZZY(Floyd+Bellman)

Problem Description It has recently been discovered how to run open-source software on the Y-Crate g...
  • libin56842
  • libin56842
  • 2013年12月03日 21:34
  • 2108

hdu1317 XYZZY(spfa最长路判环)

http://acm.hdu.edu.cn/showproblem.php?pid=1317 题意:给你n个房间,每个房间都有可以补充的能量,有些是负的。刚开始玩家有100的体力,接下来的输入每...
  • Flynn_curry
  • Flynn_curry
  • 2016年05月31日 22:14
  • 436

XYZZY HDU1317

XYZZY HDU1317
  • l_mark
  • l_mark
  • 2017年09月12日 22:26
  • 49

hdu1317 XYZZY

带负权图 bellman判环 为什么要循环V-1次? 答:因为最短路径肯定是个简单路径,不可能包含回路的, 如果包含回路,且回路的权值和为正的,那么去掉这个回路,可以得到更短的路径 如果回路的...
  • u011032846
  • u011032846
  • 2014年03月31日 06:17
  • 736

HDU1317 XYZZY

XYZZY Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su...
  • Z_zhangyinqian
  • Z_zhangyinqian
  • 2015年08月04日 17:03
  • 208

spfa hdu1317 XYZZY

诶,,还是第一次处理负环 先给一组数据 6 0 2 2 5 1 1 3 1 1 4 1 1 2 -100 1 6 0 0首先第一步肯定是要处理能量,把能量全部取负,这样就能套用最短路了 ...
  • qwb492859377
  • qwb492859377
  • 2015年08月05日 11:14
  • 810

XYZZY(hdu1317,传递闭包+Dijstra)

http://acm.hdu.edu.cn/showproblem.php?pid=1317 http://acm.hust.edu.cn/vjudge/contest/view.action?ci...
  • JHC23
  • JHC23
  • 2013年08月17日 23:06
  • 622

hdu1317 负权最短路(Floyd+bellman)

首先要读懂题,做这个题时读题看样例琢磨了半天才明白= =; 题目的意思是,一个人从一号房间走到最后一个房间,每个房间有相应的能量值(可正可负),当走到这个房间的时候可以获取这个房间的能量值,如果走到...
  • zs120197
  • zs120197
  • 2016年07月24日 12:45
  • 281
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:HDU1317:XYZZY(Floyd+Bellman)
举报原因:
原因补充:

(最多只允许输入30个字)