http://acm.hdu.edu.cn/showproblem.php?pid=1317
1 #include <cstdio> 2 #include <queue> 3 #include <cstring> 4 #include <algorithm> 5 #define maxn 1001 6 using namespace std; 7 const int inf=1<<28; 8 9 int dis[maxn]; 10 bool vis[maxn]; 11 int n,m,x; 12 int a[maxn]; 13 bool g[maxn][maxn]; 14 int cnt[maxn]; 15 16 bool spfa() 17 { 18 queue<int>q; 19 memset(vis,false,sizeof(vis)); 20 memset(cnt,0,sizeof(cnt)); 21 for(int i=1; i<=n; i++) dis[i]=0; 22 dis[1]=100; 23 q.push(1); 24 vis[1]=true; 25 while(!q.empty()) 26 { 27 int u=q.front();q.pop(); 28 vis[u]=false; 29 if(u==n) 30 { 31 return true; 32 } 33 if(cnt[u]==n+1) 34 { 35 continue; 36 } 37 cnt[u]++; 38 if(cnt[u]==n+1) 39 { 40 dis[u]=inf; 41 } 42 for(int i=1; i<=n; i++) 43 { 44 if(g[u][i]) 45 { 46 if(dis[i]<dis[u]+a[i]&&(dis[u]+a[i]>0)) 47 { 48 dis[i]=dis[u]+a[i]; 49 if(!vis[i]) 50 { 51 vis[i]=true; 52 q.push(i); 53 } 54 } 55 } 56 } 57 } 58 return false; 59 } 60 61 int main() 62 { 63 while(scanf("%d",&n)!=EOF) 64 { 65 if(n==-1) break; 66 memset(g,false,sizeof(g)); 67 for(int i=1; i<=n; i++) 68 { 69 scanf("%d",&a[i]); 70 scanf("%d",&m); 71 for(int j=0; j<m; j++) 72 { 73 scanf("%d",&x); 74 g[i][x]=true; 75 } 76 } 77 if(spfa()) 78 { 79 printf("winnable "); 80 } 81 else 82 printf("hopeless "); 83 } 84 return 0; 85 }