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

     

  • 相关阅读:
    微服务TraceId设计(SpringCloud OpenFeign)
    WebGoat通关攻略之 SQL Injection (intro)
    MySQL加密的几种常见方式
    微服务网关选型
    安装yolov3(Anaconda)
    JS的数组与字符串方法脑图总结
    分支创建&查看&切换
    高校教师资格证备考
    1个小时!从零制作一个! AI图片识别WEB应用!
    ubuntu 18.04换内核后找不到 /dev/ttyUSB0问题
  • 原文地址:https://blog.csdn.net/q619718/article/details/128007324