• H - XYZZY(spfa最大路径,判断正环)


    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

    InputcopyOutputcopy
     
    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 
     
    hopeless hopeless winnable winnable
    1. #include <iostream>
    2. #include <vector>
    3. #include <utility>
    4. #include <cstring>
    5. #include <queue>
    6. using namespace std;
    7. int n,nl[105],dist[105],cnt[105];
    8. vector<int>adj[105];
    9. bool st[105];
    10. int spfa(){
    11. memset(dist,0,sizeof(dist));
    12. memset(cnt,0,sizeof(cnt));
    13. memset(st,0,sizeof(st));
    14. dist[1]=100;
    15. queue<int>q;
    16. q.push(1);
    17. while(!q.empty()){
    18. int t=q.front();
    19. q.pop();
    20. if(cnt[t]>=n+1) continue;
    21. st[t]=0;
    22. cnt[t]++;
    23. if(cnt[t]==n+1) dist[t] = 1000000;
    24. for(auto it:adj[t]){
    25. if(dist[it]<dist[t]+nl[it]&&dist[t]+nl[it]>0){
    26. if(it==n) return 1;
    27. dist[it]=dist[t]+nl[it];
    28. if(!st[it])
    29. q.push(it);
    30. st[it]=1;
    31. }
    32. }
    33. }
    34. return false;
    35. }
    36. int main() {
    37. while (cin >> n&&n!=-1) {
    38. for (int i = 1; i <= n; i++) {
    39. adj[i].clear();
    40. }
    41. for (int i = 1; i <= n; i++) {
    42. scanf("%d", &nl[i]);
    43. int temp;
    44. scanf("%d", &temp);
    45. while (temp--) {
    46. int v;
    47. scanf("%d",&v);
    48. adj[i].push_back(v);
    49. }
    50. }
    51. if(spfa()) printf("winnable\n");
    52. else printf("hopeless\n");
    53. }
    54. }

     

  • 相关阅读:
    js:Class对象中的函数,在使用 this 时理解
    python 基础知识点(蓝桥杯python科目个人复习计划63)
    项目测试排期的正确方法是什么?
    【车间调度】遗传算法求解车间调度问题(含甘特图)【含Matlab源码 2216期】
    Web3:价值投资的范式转移
    机器学习和数据科学的最佳公共数据集机器学习、数据科学、情感分析、计算机视觉、自然语言处理 (NLP)、临床数据等的最佳公共数据集。
    【Linux】Linux权限
    传统制造业如何进行数据分析?_光点科技
    深度学习中softmax激活函数的用法
    RLChina2022-强化学习暑期课程-博弈搜索算法
  • 原文地址:https://blog.csdn.net/q619718/article/details/128007324