• HDU 1317XYZZY spfa+判断正环+链式前向星(感觉不对,但能A)


    XYZZY

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 5304    Accepted Submission(s): 1510


    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
     思路:
    单向路径。判断是否存在正环,初始化距离数组为负无穷小,进入n次,说明存在正环,将距离改为无穷大。进入n+1次,直接跳过。
    代码:
     1 #include<iostream>
     2 #include<string>
     3 #include<algorithm>
     4 #include<vector>
     5 #include<queue>
     6 #include<cstdlib>
     7 #include<cstring>
     8 #include<cstdio>
     9 #include<cmath>
    10 using namespace std;
    11 const int maxn=105;
    12 const int maxm=10005;
    13 const int INF=0x3f3f3f3f;
    14 struct edgenode {
    15     int to,w,next;
    16 }edges[maxm];
    17 bool vis[maxn];
    18 int dist[maxn],du[maxn],head[maxn];
    19 int n,cnt;
    20 void init() {
    21     for(int i=0;i<maxn;++i) head[i]=-1;
    22     for(int i=0;i<maxm;++i) edges[i].next=-1;
    23     cnt=0;
    24 }
    25 void addedge(int u, int v, int w) {
    26     edges[cnt].to=v;
    27     edges[cnt].w=w;
    28     edges[cnt].next=head[u];
    29     head[u]=cnt++;
    30 }
    31 bool spfa() {
    32     memset(vis,false,sizeof(vis));
    33     memset(du,0,sizeof(du));
    34     for(int i=0;i<maxn;++i) dist[i]=-INF;
    35     queue<int> q;
    36     dist[1]=100;vis[1]=true;
    37     q.push(1);
    38     while(!q.empty()) {
    39         int now=q.front();q.pop();
    40         vis[now]=false;
    41         du[now]++;
    42         if(du[now]>n) continue;
    43         if(du[now]==n) dist[now]=INF;
    44         for(int i=head[now];~i;i=edges[i].next) {
    45             if(dist[edges[i].to]<dist[now]+edges[i].w&&dist[now]+edges[i].w>0) {
    46                 dist[edges[i].to]=dist[now]+edges[i].w;
    47                 if(edges[i].to==n) return true;
    48                 if(!vis[edges[i].to]) {
    49                     vis[edges[i].to]=true;
    50                     q.push(edges[i].to);
    51                 }
    52             }
    53         }
    54     }
    55     return false;
    56 }
    57 int main() {
    58     while(scanf("%d",&n)&&n!=-1) {
    59         int w,num,id;
    60         init();
    61         for(int i=1;i<=n;++i) {
    62             scanf("%d%d",&w,&num);
    63             for(int j=1;j<=num;++j) {
    64                 scanf("%d",&id);
    65                 addedge(i,id,w);
    66             }
    67         }
    68         if(spfa()) printf("winnable
    ");
    69         else printf("hopeless
    ");
    70     }
    71     return 0;
    72 }
    View Code
  • 相关阅读:
    ngx_os_init解析
    cacheline相关优化手段
    std::thread_local
    std::initializer_list<T>
    MySQL group by 注意事项
    MySQL IFNULL函数
    python nginx不同参数压测脚本
    完全卸载oracle11g步骤
    Maven常用命令(转载)
    项目SVN的IP地址发生变化时修改SVN为新的IP地址
  • 原文地址:https://www.cnblogs.com/lemonbiscuit/p/7775976.html
Copyright © 2020-2023  润新知