(HDU1317)XYZZY(Floyd+spfa)

XYZZY

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

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

这是一道神奇的图论题,因为每间房间都有自己所带的能量数,所以显而易见的:如果存在环路而且走完一圈之后能量是增长的,并且这个环和终点之间有路径联通,就可以判断一定能走到终点。不然就只能老老实实spfa看看终点的权值是否大于0;

相对来说简单一点,但有几个坑点需要注意:

1、判环的时候如果有正环,不能立刻退出,还要判断一下和终点是否联通,若不连通是没有意义的。

2、因为这道题的权值在点上,所以spfa是要修改一下条件。

3、数据范围,Floyd的复杂度是o(n^3),幸好这道题节点数不超过一百。先用floyd改一下判断节点连通情况,再spfa判正环。

#include<iostream>
#include<cstdio>
#include<vector>
#include<set>
#include<map>
#include<string.h>
#include<algorithm>
#include<queue>
#define inf 0x3f3f3f3f

using namespace std;
int ma[105][105];
int reach[105][105];
int power[105];

int flod(int n)
{

    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            for(int k=1;k<=n;k++)
                if(reach[i][j]||(reach[i][k]&&reach[k][j]))
                    reach[i][j]=1;
    if(reach[1][n])
        return 1;
    return 0;
}

int spfa(int n)
{
    queue<int> run;
   // int vis[105]={0};
    int cnt[105]={0};
    int quan[105];
    memset(quan,0,sizeof(quan));
    run.push(1);
    //vis[1]=1;
    cnt[1]++;
    quan[1]=100;
    while(!run.empty())
    {
        int cur=run.front();
        run.pop();
        if(++cnt[cur]>=n)
        {
            if(reach[cur][n])
                return 1;
            else
                break;
        }
        for(int i=1;i<=n;i++)
            if(ma[cur][i]&&quan[i]<quan[cur]+power[i]&&quan[cur]+power[i]>0)
            {
                quan[i]=quan[cur]+power[i];
                run.push(i);
            }
    }
    if(quan[n]>0)
        return 1;
    return 0;
}

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        if(n==-1)
            break;
            memset(ma,0,sizeof(ma));
            memset(reach,0,sizeof(reach));
            memset(power,0,sizeof(power));
        for(int i=1;i<=n;i++)
        {

            int k;
            scanf("%d%d",&power[i],&k);
            for(int j=1;j<=k;j++)
            {
                int tem;
                scanf("%d",&tem);
                ma[i][tem]=1;
                reach[i][tem]=1;
            }
        }
        if(!flod(n))
        {
            printf("hopeless\n");
            continue;
        }
        if(!spfa(n))
            printf("hopeless\n");
        else
            printf("winnable\n");

    }
    return 0;
}
时间: 2024-10-14 04:46:34

(HDU1317)XYZZY(Floyd+spfa)的相关文章

HDU - 2544 - 最短路 (最基础单源最短路问题!!dijkstra+floyd+SPFA)

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 34617    Accepted Submission(s): 15001 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找

hdoj2544 最短路(Dijkstra || Floyd || SPFA)

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2544 思路 最短路算法模板题,求解使用的Dijkstra算法.Floyd算法.SPFA算法可以当做求解最短路问题的模板使用. 代码 Dijkstra算法: 1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 using namespace

(双端队列优化的SPFA) bzoj 2100

Description Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, she travels the C (1 <= C <= 200,000) cowpaths which are arranged as the usual graph which connects P (1 <= P <= 100,000) pastures convenientl

最短路(Dijkstra,Floyd,Bellman_Ford,SPFA)

当然,这篇文章是借鉴大佬的... 最短路算法大约来说就是有4种——Dijkstra,Floyd,Bellman_Ford,SPFA 接下来,就可以一一看一下... 1.Dijkstra(权值非负,适用于有向图及无向图,单源最短路) 1 Dijkstra's算法解决的是图中单个源点到其它顶点的最短路径.只能解决权值非负(看了代码就知道了)2 Dijkstral只能求出任意点到达源点的最短距离(不能求出任意两点之间的最短距离),同时适用于有向图和无向图,复杂度为O(n^2).3算法的过程: 1设置顶

hdu 1596(Floyd 变形)

http://acm.hdu.edu.cn/showproblem.php?pid=1596 find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6911    Accepted Submission(s): 2450 Problem Description XX星球有很多城市,每个城市之间有一条或

最短路问题(floyd算法)(优化待续)

问题描述: 最短路问题(short-path problem):若网络中的每条边都有一个数值(长度.成本.时间等),则找出两节点(通常是源节点和阱节点)之间总权和最小的路径就是最短路问题.最短路问题是网络理论解决的典型问题之一,可用来解决管路铺设.线路安装.厂区布局和设备更新等实际问题. 1.floyd算法 算法描述: Floyd算法又称为插点法,是一种用于寻找给定的加权图中多源点之间最短路径的算法.该算法名称以创始人之一.1978年图灵奖获得者.斯坦福大学计算机科学系教授罗伯特·弗洛伊德命名.

hdu 1217 (Floyd变形)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4430    Accepted Submission(s): 2013 Problem Description Arbitrage is the use of discr

POJ -1062 昂贵的聘礼(前向星 &amp;&amp; SPFA)

题目链接:昂贵的聘礼 这个题对自己收获挺大的,模板要自己经常敲,才能理解,要自己经常敲,从能温故而知新,自己以前总结的建图方式,做题的时候要会用,要敢用,否则==NULL. 题意对于交换条件描述的有点不清楚,这里解释一下,假设8件物品,等级差距不能超过3,酋长LV 5,所以可以进行交换的LV区间是[2,5][3,6][4,7][5,8],不必考虑题目那一句,"但是如果他和某个地位较低的人进行了交易,地位较高的的人不会再和他交易,他们认为这样等于是间接接触,反过来也一样".越看越晕,只要

HDU 2680Choose the best route (SPFA)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 百度之星编程大赛--您报名了吗? 杭电ACM 2014暑期集训队--选拔安排~ Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis