HDU 1317 xy133

Source: Internet
Author: User

Whether 1 to N can ensure that the energy at each point is positive.


The initial energy of START 1 is 100.

Input is from 1 ~ N, respectively, is energy, can reach m rooms, namely a1, a2, a3 ,..., Am

The edge generated by each point that can be reached can be authorized, that is, the energy value.


SPFA is used to find the longest path deformation, and the negative ring is not afraid. A slight change is required when the positive ring appears.

Vis [] indicates whether to enter the queue, d [] indicates energy, and que [] indicates the number of queues.

If a positive ring (que [v]> = n) appears, it indicates that positive energy is guaranteed to reach every point.

At this time, d [v] is assigned a maximum value (because positive energy can be obtained continuously) and no longer entered next time.


D [n]> 0 indicates that the operation is successful.

//C++ 0ms#include
 
  #include
  
   #include
   
    #include
    
     #include#include
     
      #include
      #include
       
        #include
        
         #include
         
          #include
          
           #include
           
            #define INF 0xfffffff#define eps 1e-6using namespace std;int n,m;struct lx{ int v,en;};vector
            
              g[101];int d[101];bool vis[101];int que[101];void SPFA(){ for(int i=1; i<=n; i++) d[i]=-INF,vis[i]=0,que[i]=0; queue
             
              q; d[1]=100,vis[1]=1,que[1]=1; q.push(1); while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=0; if(que[u]>n)continue;// >= WA for(int j=0; j
              
               0) { d[v]=d[u]+en; if(!vis[v]) { vis[v]=1; q.push(v); if(++que[v]>=n)d[v]=INF; } } } } if(d[n]>0)puts("winnable"); else puts("hopeless");}int main(){ while(scanf("%d",&n),n!=-1) { int en,v,u; for(int i=0; i<=n; i++) g[i].clear(); for(int i=1; i<=n; i++) { u=i; scanf("%d%d",&en,&m); lx now; now.en=en; while(m--) { scanf("%d",&v); now.v=v; g[u].push_back(now); } } SPFA(); }}
              
             
            
           
          
         
        
       
     
    
   
  
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.