zoukankan      html  css  js  c++  java
  • HDU XYZZY (SPFA最长路有向环+floyd判断连通性)

    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.

    InputThe 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.
    OutputIn 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

    如果碰到环 判断当前环是否可以到达N 

    否则跑SPFA 判断 dis[N]是否>0

    code:

    //
    #include<bits/stdc++.h>
    using namespace std;
    #define maxnn 500
    int dis[2000];
    int mapp[500][500];
    int mappp[500][500];
    int n;
    int ener[400];
    queue <int > Q;
    int mark[2000];
    int cnt[2000];
    bool spfa()
    {
        Q.push(1);
        for(int i=1;i<=n;i++)
        dis[i]=-100000000,mark[i]=0;
            dis[1]=100;
            mark[1]=1;
        while(Q.size())
        {
            int s=Q.front();
            Q.pop();
            mark[s]=0;
            for(int j=1;j<=n;j++)
            {
                int i=s;
                if(j!=s)
                {
                    if(mappp[i][j]&&dis[j]<dis[i]+ener[j]&&dis[i]+ener[j]>0)
                    {
                        dis[j]=dis[i]+ener[j];
                        if(!mark[j])
                        {
                            mark[j]=1;
                            Q.push(j);
                            cnt[j]++;
                            if(cnt[j]==n-1)
                            {
                                if(mapp[j][n])
                                {
                                    cout<<"winnable"<<endl;
                                    return 1;
                                }
                            return 0; 
                            }
                        }
                    }
                }
            } 
        }
             return 0;
    }
    int main()
    {
        while(1)
        {
            cin>>n;
            if(n==-1)
            return 0;
            int k;
            int x;    
            for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
            {
                mapp[i][j]=0;
                mappp[i][j]=0;
                ener[i]=0;
                cnt[i]=0;
            }
            for(int i=1;i<=n;i++)
            {
                cin>>ener[i];
                cin>>k;
                for(int j=1;j<=k;j++)
                {
                    cin>>x;
                    mapp[i][x]=1;
                    mappp[i][x]=1;
                }
            }    
            for(k=1;k<=n;k++)
            for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
            {
                if((mapp[i][k]==1)&&(mapp[k][j]==1))
                mapp[i][j]=1;
            }
            if(spfa())
            continue;
            if(!mapp[1][n])
            {
            cout<<"hopeless"<<endl;
            continue;
            }
            if(dis[n]>0)
            cout<<"winnable"<<endl;
            else
            cout<<"hopeless"<<endl;
        }
    }
    刀剑映出了战士的心。而我的心,漆黑且残破
  • 相关阅读:
    极大似然估计理解与应用
    逻辑回归理解及代码实现
    《剑指offer》---数值的整数次方
    线性回归理解及代码实现
    二叉搜索树(BST)---python实现
    《剑指offer》---顺时针打印矩阵
    《剑指offer》---二进制中1的个数
    《剑指offer》---输出链表倒数第k个结点
    版本控制-Git服务器搭建和常用命令使用
    hbase伪分布式平台搭建(centos 6.3)
  • 原文地址:https://www.cnblogs.com/OIEREDSION/p/11266300.html
Copyright © 2011-2022 走看看