HDU 1317 XYZZY Floyd判断能否到达+SPFA判断能否形成环及求最长路(好题)

来源:互联网 发布:淘宝上的骑士装备 编辑:程序博客网 时间:2024/06/11 08:13

点击打开链接

 

XYZZY

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


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
50 1 2-60 1 3-60 1 420 1 50 050 1 220 1 3-60 1 4-60 1 50 050 1 221 1 3-60 1 4-60 1 50 050 1 220 2 1 3-60 1 4-60 1 50 0-1
 


 

Sample Output
hopelesshopelesswinnablewinnable
 


 

Source
University of Waterloo Local Contest 2003.09.27
 


 

Recommend
Eddy
 
 
题意是说有n个房间,然后给你每个房间能够到达的房间,每个房间还有一个能量值(有正有负),让你从第一个房间出发判断能否到达第n个房间,一开始你有100能量值,如果能量值为负则失败,当然你也可以从一个房间在返回已经走过的并且可到达的房间去补充能量值。
首先你要判断他能不能到达n号房间,如果到达不了,则直接失败。如果能够到达,则判断他的能量值是否大于0,如果有环的话,则证明此点加入队列的次数要大于等于n,然后判断此点能否到达n即可。无环的话,则求从1号点到n号点的最长路即可。
#include<stdio.h>#include<string.h>#include<queue>using namespace std;int n;bool g[107][107],reach[107][107];int energy[107],power[107],count[107];void floyd(){    for(int i=1;i<=n;i++)        for(int j=1;j<=n;j++)            for(int k=1;k<=n;k++)                reach[j][k]=(reach[j][k]||reach[j][i]&&reach[i][k]);}bool spfa(int now){    queue<int>q;    memset(power,0,sizeof(power));    memset(count,0,sizeof(count));    q.push(now);    power[1]=100;    while(!q.empty())    {        int now=q.front();        q.pop();        count[now]++;        if(count[now]>=n) return reach[now][n];//如果某个点的次数大于n,则存在正环        for(int i=1;i<=n;i++)        if(g[now][i]&&power[now]+energy[i]>power[i]&&power[now]+energy[i]>0)        {            q.push(i);            power[i]=power[now]+energy[i];        }    }    return power[n]>0;}int main(){    while(scanf("%d",&n)!=EOF)    {        if(n==-1)break;        memset(g,false,sizeof(g));        memset(reach,false,sizeof(reach));        int num,door;        for(int i=1;i<=n;i++)        {            scanf("%d%d",&energy[i],&num);            while(num--)            {                scanf("%d",&door);                g[i][door]=true;                reach[i][door]=true;            }        }        floyd();        if(!reach[1][n])            printf("hopeless\n");        else        {            if(spfa(1))                printf("winnable\n");            else                printf("hopeless\n");        }    }    return 0;}

 

 

原创粉丝点击