• 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. }

     

  • 相关阅读:
    【Python 千题 —— 基础篇】2 的 N 次方
    tensorflow-serving docker模型部署(以mnist为例)
    数藏向左,NFT向右
    效果最大化广告系列的优势所在!
    【WPF应用30】WPF中的ListBox控件详解
    【Rust】Rust环境配置与语法基础
    如何使用责任链默认优雅地进行参数校验?
    C++ 编译报错error: invalid new-expression of abstract class type
    51单片机的简易计算器数码管显示仿真设计( proteus仿真+程序+原理图+报告+讲解视频)
    微信小程序实战十四:小程序及APP端实现客服功能
  • 原文地址:https://blog.csdn.net/q619718/article/details/128007324