uva 10557 - XYZZY (最长路)

本文深入探讨了经典的计算机冒险游戏及其发展历程,包括其规则、玩法及如何在现代设备上运行开源软件。通过分析游戏机制,揭示了如何通过SPFA算法解决最长路径问题,并提供了实现该算法的代码实例。
部署运行你感兴趣的模型镜像

Problem D: XYZZY


ADVENT: /ad�vent/, n.
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.

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.

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

Output for Sample Input

hopeless
hopeless
winnable
winnable

G. V. Cormack

用spfa求最长路。因为可能有正圈,所以对于这种情况要特别判断。如果这个圈可以到 n ,则win。所以可以反向建图,从n出发搜索可以到达的点。

#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<stack>
#include<iostream>
#include<queue>
#include<cmath>
#include<string>
#include<set>
#include<map>
using namespace std;
const int maxn = 100 + 5;
const int mod = 1000000000 + 7;
const double eps = 1e-7;
const int INF = 1000000000;
typedef long long LL;
typedef pair<LL, int> P;

vector<int> G[maxn], rG[maxn];
int val[maxn];
int n;
int can[maxn];

void dfs(int x){
    can[x] = 1;
    for(int i = 0;i < rG[x].size();i++){
        int to = rG[x][i];
        if(!can[to])
            dfs(to);
    }
}

LL dis[maxn], cnt[maxn];
priority_queue<P> pq;

bool bfs(){
    while(!pq.empty())
        pq.pop();
    pq.push(P(100, 1));
    memset(dis, 0, sizeof dis);
    memset(cnt, 0, sizeof cnt);
    dis[1] = 100;
    while(!pq.empty()){
        P p = pq.top();
        pq.pop();
        LL eng = p.first;
        int pos = p.second;
        if(eng < dis[pos])
            continue;
        if(cnt[pos] > n || pos == n){
            return true;
        }

        for(int i = 0;i < G[pos].size();i++){
            int to = G[pos][i];
            if(can[to] && dis[pos]+val[to] > dis[to]){
                dis[to] = dis[pos]+val[to];
                pq.push(P(dis[to], to));
                cnt[to]++;
            }
        }
    }
    return false;
}

int main(){
    while(scanf("%d", &n)){
        if(n == -1)
            break;
        for(int i = 0;i < maxn;i++){
            G[i].clear();
            rG[i].clear();
        }
        for(int i = 1;i <= n;i++){
            int num;
            scanf("%d%d", &val[i], &num);
            while(num--){
                int x;
                scanf("%d", &x);
                G[i].push_back(x);
                rG[x].push_back(i);
            }
        }
        memset(can, 0, sizeof can);
        dfs(n);

        if(bfs())
            printf("winnable\n");
        else
            printf("hopeless\n");

    }
    return 0;
}
/*
7
0 1 2
0 2 3 5
-100 1 4
-100 1 7
1 1 6
0 1 5
0 0

5
0 1 4
100 1 5
-80 1 2
-90 1 3
0 0

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
7
0 1 2
-98 2 3 5
-1 1 4
101 1 1
-60 1 6
-60 1 7
0 0
8
0 1 2
0 1 3
-5 1 4
-50 1 5
-40 2 6 7
110 1 3
0 1 8
0 0
1
0 0

7
0 2 2 6
-60 1 3
100 1 4
100 1 5
100 1 2
-120 1 7
0 0

15
0 2 2 11
-10 1 3
-10 1 4
-10 1 5
-10 1 6
-10 1 7
-10 1 8
-10 1 9
-10 1 10
-10 1 13
50 1 12
50 1 11
11 2 10 14
-100 1 15
0 0


*/


您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

基于数据驱动的 Koopman 算子的递归神经网络模型线性化,用于纳米定位系统的预测控制研究(Matlab代码实现)内容概要:本文围绕“基于数据驱动的Koopman算子的递归神经网络模型线性化”展开,旨在研究纳米定位系统的预测控制方法。通过结合数据驱动技术与Koopman算子理论,将非线性系统动态近似为高维线性系统,进而利用递归神经网络(RNN)建模并实现系统行为的精确预测。文中详细阐述了模型构建流程、线性化策略及在预测控制中的集成应用,并提供了完整的Matlab代码实现,便于科研人员复现实验、优化算法并拓展至其他精密控制系统。该方法有效提升了纳米级定位系统的控制精度与动态响应性能。; 适合人群:具备自动控制、机器学习或信号处理背景,熟悉Matlab编程,从事精密仪器控制、智能制造或先进控制算法研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①实现非线性动态系统的数据驱动线性化建模;②提升纳米定位平台的轨迹跟踪与预测控制性能;③为高精度控制系统提供可复现的Koopman-RNN融合解决方案; 阅读建议:建议结合Matlab代码逐段理解算法实现细节,重点关注Koopman观测矩阵构造、RNN训练流程与模型预测控制器(MPC)的集成方式,鼓励在实际硬件平台上验证并调整参数以适应具体应用场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值