标签:
Description
Input
Output
Sample Input
Sample Output
1 # include<iostream> 2 # include<cstdio> 3 # include<algorithm> 4 # include<cstring> 5 # include<queue> 6 using namespace std; 7 const int INF=1<<29; 8 struct edge 9 { 10 int to,w,nxt; 11 }; 12 edge e[5500]; 13 int mp[105][105],ey[105],inque[105],outque[105]; 14 int n,head[105],cnt,dis[105]; 15 void add(int u,int v,int w) 16 { 17 e[cnt].to=v; 18 e[cnt].w=w; 19 e[cnt].nxt=head[u]; 20 head[u]=cnt++; 21 } 22 void maketree() 23 { 24 cnt=0; 25 memset(head,-1,sizeof(head)); 26 for(int i=1;i<=n;++i){ 27 for(int j=1;j<=n;++j){ 28 if(i==j) 29 continue; 30 if(mp[i][j]) 31 add(i,j,ey[j]); 32 } 33 } 34 } 35 bool spfa() 36 { 37 fill(dis,dis+n+1,-INF); 38 fill(inque,inque+n+1,0); 39 fill(outque,outque+n+1,0); 40 queue<int>q; 41 q.push(1); 42 dis[1]=100; 43 inque[1]=1; 44 while(!q.empty()) 45 { 46 int u=q.front(); 47 q.pop(); 48 inque[u]=0; 49 ++outque[u]; 50 if(outque[u]>n) 51 continue; 52 if(outque[u]==n) 53 dis[u]=INF; 54 for(int i=head[u];i!=-1;i=e[i].nxt){ 55 if(dis[e[i].to]<dis[u]+e[i].w&&dis[u]+e[i].w>0){ 56 dis[e[i].to]=dis[u]+e[i].w; 57 if(e[i].to==n) 58 return true; 59 else if(!inque[e[i].to]){ 60 inque[e[i].to]=1; 61 q.push(e[i].to); 62 } 63 } 64 } 65 } 66 return false; 67 } 68 int main() 69 { 70 while(scanf("%d",&n)) 71 { 72 if(n==-1) 73 break; 74 int a,b; 75 memset(mp,0,sizeof(mp)); 76 for(int i=1;i<=n;++i){ 77 scanf("%d%d",ey+i,&a); 78 while(a--){ 79 scanf("%d",&b); 80 mp[i][b]=1; 81 } 82 } 83 maketree(); 84 if(spfa()) 85 printf("winnable\n"); 86 else 87 printf("hopeless\n"); 88 } 89 return 0; 90 }
标签:
原文地址:http://www.cnblogs.com/20143605--pcx/p/4681789.html