• HDU 1317 XYZZY


    XYZZY

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

    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
     
    Recommend
    Eddy
     


    这个题目意思就是

    给你一堆房间 每个房间有一个值,进入房间时,你的精力+该房间的值,给你房间之间的单向连通关系,你初始有100精力,问你能不能走到最后一个房间。

    一开始感觉没啥难的,就是跑spfa,如果有正环,原点到该点的距离设为正无穷

    但是一直wa

    后来比较代码发现 想法是没错,但是spfa有环的时候写错了!

    原来是这么写的

                    if(!flag[v])
                    {
                        if(tims[v]<=m)
                        {
                            tims[v]++;
                            q.push(v);
                            flag[v]=1;
                        }
                        else
                        {
                            d[v]=inf;
                            flag[v]=1;
                        }
                    }


    这样存在的问题就是

    你判断出来了正环,值也赋值了,但是这个点没有被加入到队列里用来更新后面的点!


    简直傻狗!!!





    所以改一下代码就好了

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<cstdlib>
    #include<algorithm>
    using namespace std;
    const int inf=0x3f3f3f3f;
    int w[111];
    int d[111];
    int m,x,y;
    int n;
    struct self
    {
        int x,y,w,nxt;
    }s[111111];
    int fst[111];
    int ans;
    queue<int>q;
    int tims[111];
    int flag[111];
    void add(int x,int y)
    {
        n++;
        s[n].x=x;
        s[n].y=y;
        s[n].nxt=fst[x];
        fst[x]=n;
    }
    
    
    void spfa()
    {
        while(!q.empty())q.pop();
        memset(flag,0,sizeof(flag));
        memset(tims,0,sizeof(tims));
        for(int i=1;i<=m;i++)
            d[i]=-inf;
        q.push(1);
        d[1]=100;
        flag[1]=1;
        tims[1]=1;
        int ok=0;
        while(!q.empty())
        {
            int u=q.front();
            q.pop();
            flag[u]=0;
            if(d[u]<=0)continue;
            //cout<<"d["<<u<<"]="<<d[u]<<endl;
            for(int e=fst[u];e!=-1;e=s[e].nxt)
            {
                int v=s[e].y;
                //cout<<" d["<<v<<"]="<<d[v]<<" w="<<w[v]<<endl;
                if(d[v]<d[u]+w[v])
                {
                    d[v]=d[u]+w[v];
                    if(!flag[v])
                    {
                        if(tims[v]<m)
                        {
                            tims[v]++;
                            q.push(v);
                            flag[v]=1;
                        }
                        else
                            if(tims[v]==m)
                            {
                                d[v]=inf;
                                flag[v]=1;
                                q.push(v);
                                tims[v]++;
                            }
                    }
                }
            }
        }
        if(d[m]>0)
            printf("winnable
    ");
        else
            printf("hopeless
    ");
    }
    int main()
    {
        while(scanf("%d",&m)==1 && m!=-1)
        {
            memset(fst,-1,sizeof(fst));
            n=0;
            for(int i=1;i<=m;i++)
            {
                scanf("%d",&w[i]);
                scanf("%d",&x);
                for(int j=1;j<=x;j++)
                {
                    scanf("%d",&y);
                    add(i,y);
                }
            }
            spfa();
        }
        return 0;
    }


  • 相关阅读:
    declaration may not appear after executable statement in block
    linux 管道通信
    用c语言创建双向环形链表
    bash: ./LM35_make_fs: Permission denied 解决办法
    sunzl is not in the sudoers file.This incident will be reported
    基于嵌入式linux路由转发功能的实现
    关于eth0 eth0:1 和eth0.1关系介绍
    软重启
    Android中Serializable和Parcelable序列化对象详解
    公共技术点(Android 动画基础)
  • 原文地址:https://www.cnblogs.com/abgnwl/p/6550342.html
Copyright © 2020-2023  润新知