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(); }}