zoukankan      html  css  js  c++  java
  • poj 1932 XYZZY(spfa最长路+判断正环+floyd求传递闭包)

    XYZZY
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 4154   Accepted: 1185

    Description

    The prototypical computer adventure game, first designed by Will Crowther on the PDP-10 in the mid-1970s as an attempt at computer-refereed fantasy gaming, and expanded into a puzzle-oriented game by Don Woods at Stanford in 1976. (Woods had been one of the authors of INTERCAL.) Now better known as Adventure or Colossal Cave Adventure, but the TOPS-10 operating system permitted only six-letter filenames in uppercase. See also vadding, Zork, and Infocom.

    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

    题目意思:
    n个点,每个点有一个权值,存在负权值
    构成一个有向图,点从1到n编号,起点是1,终点是n
    起点和终点的权值都为0
    每个点可以走多次
    现在又一个人,从起点开始走,他开始具有100的能量,走到一个点,就加上这个点的权值
    如果能量小于等于0的话就会死去,不能到达终点
    问你他是否可以到达终点
    分析:
    每个点可以走多次,意味着只要存在正环且正环上的某个点到n是可达的(所以需要求传递闭包)
    那么他一定可以到达n点,这是第一种情况
    第二种情况就是跑个最长路,然后dis[n]是大于0的
    这两种情况都是可以到达n的
    注意最长路dis初始化0......
     
    #include<stdio.h>
    #include<iostream>
    #include<math.h>
    #include<string.h>
    #include<set>
    #include<map>
    #include<list>
    #include<math.h>
    #include<queue>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    #define INF 99999999
    #define me(a,x) memset(a,x,sizeof(a))
    int mon1[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
    int mon2[13]= {0,31,29,31,30,31,30,31,31,30,31,30,31};
    int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};
    
    int getval()
    {
        int ret(0);
        char c;
        while((c=getchar())==' '||c=='
    '||c=='
    ');
        ret=c-'0';
        while((c=getchar())!=' '&&c!='
    '&&c!='
    ')
            ret=ret*10+c-'0';
        return ret;
    }
    void out(int a)
    {
        if(a>9)
            out(a/10);
        putchar(a%10+'0');
    }
    
    #define max_v 105
    int vis[max_v];
    int dis[max_v];
    int a[max_v];
    int G[max_v][max_v];
    int cnt[max_v];
    int n;
    int kk;
    
    void init()
    {
        kk=0;
        me(a,0);
        me(vis,0);
        me(G,0);
        me(cnt,0);
        me(dis,0);
    }
    void floyd()
    {
        for(int k=1;k<=n;k++)
        {
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    if(i==j)
                        continue;
                    if(G[i][k]&&G[k][j])
                        G[i][j]=1;
                }
            }
        }
    }
    int spfa(int s)
    {
        queue<int> q;
        q.push(s);
        vis[s]=1;
        dis[s]=a[s];
        cnt[s]++;
    
        while(!q.empty())
        {
            int u=q.front();
            q.pop();
            vis[u]=0;
    
            for(int i=1;i<=n;i++)
            {
                if(G[u][i]&&dis[i]<dis[u]+a[i])
                {
                    dis[i]=dis[u]+a[i];
                    if(vis[i]==0)
                    {
                        q.push(i);
                        vis[i]=1;
                        cnt[i]++;
                        if(cnt[i]>n)
                        {
                            kk=i;
                            return 0;
                        }
                    }
                }
            }
        }
        return 1;
    }
    int main()
    {
        while(~scanf("%d",&n))
        {
            if(n<0)
                break;
            int x,y,z;
            init();
            for(int i=1;i<=n;i++)
            {
                scanf("%d %d",&x,&y);
                a[i]=x;
                while(y--)
                {
                    scanf("%d",&z);
                    G[i][z]=1;
                }
            }
            a[1]=100;
            int flag=spfa(1);
            floyd();
            if((flag==0&&G[kk][n])||dis[n]>0)
            {
                printf("winnable
    ");
            }else
            {
                printf("hopeless
    ");
            }
        }
        return 0;
    }
    /*
    题目意思:
    n个点,每个点有一个权值,存在负权值
    构成一个有向图,点从1到n编号,起点是1,终点是n
    起点和终点的权值都为0
    每个点可以走多次
    现在又一个人,从起点开始走,他开始具有100的能量,走到一个点,就加上这个点的权值
    如果能量小于等于0的话就会死去,不能到达终点
    问你他是否可以到达终点
    
    分析:
    每个点可以走多次,意味着只要存在正环且正环上的某个点到n是可达的(所以需要求传递闭包)
    那么他一定可以到达n点,这是第一种情况
    第二种情况就是跑个最长路,然后dis[n]是大于0的
    这两种情况都是可以到达n的
    
    注意最长路dis初始化0......
    
    */
  • 相关阅读:
    iOS 远程推送
    iOS 本地推送
    iOS 循环利用的注意事项
    iOS 通知代理执行代理方式时,代理为nil的解决办法
    iOS SSZipArchive
    iOS PushMebaby
    Objective
    Objective
    Objective
    Objective
  • 原文地址:https://www.cnblogs.com/yinbiao/p/10008341.html
Copyright © 2011-2022 走看看