Problem description
Garlic is playing a very interesting game. There are up to 100 maps in this game. Map 1 is the starting point and room n is the end point. Some maps are replenishment stations, which can add ki point physical strength, while some maps have monsters, which need to consume ki point physical strength. There are some one-way channel links between maps and maps. Starting from map 1, Garlic Tau Jun has 100 initial physical strength. Each time you enter a map, you need to deduct or increase the corresponding physical strength. This process lasts until the end, or when the physical strength is zero, Game Over will occur. However, he can go through the same map any time, and each time he needs to accept the physical value of the map.
Input format
Line 1 is an integer n (n < 100). Lines 2 - N + 1, the first integer of each line represents the change of physical strength of the map. Next comes the list of rooms that can be reached from that room. The first integer represents the number of rooms, followed by the number of rooms that can be reached.
Output format
If the player can reach the end point, output Yes, otherwise output No.
sample input
5
0 1 2
-60 1 3
-60 1 4
20 1 5
0 0
sample output
No
#include<bits/stdc++.h> using namespace std; const int MAX_N=110; const int MAX_M=1e6; struct edge{ int v,next,w; }e[2*MAX_M]; int p[MAX_N],eid; int dst[MAX_N]; bool inq[MAX_N]; int cnt[MAX_N]; int n; void init(){ memset(p,-1,sizeof(p)); eid=0; } int flag=0; void insert(int w,int u,int v){ e[eid].v=v; e[eid].w=w; e[eid].next=p[u]; p[u]=eid++; } void spfa(int s){ for(int i=1;i<=n;i++){ dst[i]=-0x3f3f3f3f; } memset(cnt,0,sizeof(cnt)); dst[s]=100; queue<int>q; q.push(s); inq[s]=1; while(!q.empty()){ int u=q.front(); q.pop(); inq[u]=0; for(int i=p[u];i+1;i=e[i].next){ int x=e[i].v; if(dst[x]<dst[u]+e[i].w&&dst[u]+e[i].w>=0){ dst[x]=dst[u]+e[i].w; if(!inq[x]){ q.push(x); cnt[x]++; inq[x]=1; } } if(cnt[x]>n){ flag=1; return; } } } } int main(){ scanf("%d",&n); init(); int w,nn,v; for(int i=1;i<=n;i++){ scanf("%d%d",&w,&nn); for(int j=1;j<=nn;j++){ int xx; scanf("%d",&xx); insert(w,i,xx); } } spfa(1); if(dst[n]>0||flag){ cout<<"Yes"<<endl; }else{ cout<<"No"<<endl; } return 0; }