zoukankan      html  css  js  c++  java
  • Floyd+Bellman-ford求正环 hdu1317 XYZZY

    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

    先用floyd判断连通性,然后在用bellman-ford判断是否有正环

    #include <iostream>
    #include <cstring>
    #include <stdio.h>
    #include <algorithm>
    #include <vector>
    #include <map>
    using namespace std;
    typedef long long ll;
    const int INF=0x3f3f3f3f;
    int d[105][105],w[105];
    int dp[105],_begin[6005],_end[6005];
    int n,len;
    bool Bellman_ford()
    {
        for(int i=1;i<=n;i++)
                dp[i]=-INF;
            dp[1]=100;
            for(int k=0;k<n-1;k++)
            {
                for(int i=0;i<len;i++)
                    {
                        int x=_begin[i],y=_end[i];
                        if(dp[x]+w[y]>0)             //注意进入下一个房间时精力一定为正
                        dp[y]=max(dp[y],dp[x]+w[y]);
                    }
            }
            for(int i=0;i<len;i++)
            {
                int x=_begin[i],y=_end[i];
                if(dp[x]+w[y]>0&&dp[y]<dp[x]+w[y]&&d[y][n])   //存在正环,且正环能到达n
                    return 1;
            }
            return dp[n]>0;
    }
    int main()
    {
        while(cin>>n)
        {
            if(n==-1)
                break;
            memset(d,0,sizeof(d));
            int k,to;
            len=0;
            for(int i=1;i<=n;i++)
            {
                cin>>w[i]>>k;
                for(int j=0;j<k;j++)
                {
                    cin>>to;
                    d[i][to]=1;
                    _begin[len]=i;
                    _end[len++]=to;
                }
            }
            for(int k=1;k<=n;k++)
                for(int i=1;i<=n;i++)
                    for(int j=1;j<=n;j++)
                        d[i][j]=d[i][j]||(d[i][k]&&d[k][j]);
            if(d[1][n]==0)             //如果不连通直接结束
            {
                cout<<"hopeless"<<endl;
                continue;
            }
            if(Bellman_ford())
                cout<<"winnable"<<endl;
            else
                cout<<"hopeless"<<endl;
        }
        return 0;
    }
  • 相关阅读:
    物理机异常断电,linux虚拟机系统磁盘mount失败,导致无法启动; kubectl 连接失败
    [Docker] 制作并运行 Nginx 镜像
    [Docker] 在CentOS6.8 安装 Docker
    47.DOM例题
    46.脚本化css2
    44 脚本化操作css
    43.操作标签属性
    42回顾
    41:例题、知识点复习
    40.数组字符串例题
  • 原文地址:https://www.cnblogs.com/acagain/p/9180726.html
Copyright © 2011-2022 走看看