#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
const int N = 105;
const int inf = 1 << 28;
int a[N][N], c[N];
int dis[N];
int cost[N];
int vis[N];
int n;
bool spfa()
{
memset(dis, 0, sizeof(dis));
memset(c, 0, sizeof(c));
memset(vis, 0, sizeof(vis));
queue<int> q;
while( !q.empty() )
q.pop();
dis[1] = 100;
q.push(1);
vis[1] = 1;
while( !q.empty() )
{
int now = q.front();
q.pop();
vis[now] = 0;
if( now == n )
{
return 1;
}
if( c[now] == n+1 )
{
continue;
}
c[now]++;
if( c[now] == n+1 )
{
dis[now] = inf;
}
for( int i = 1; i <= n; i++ )
{
if( a[now][i] )
{
if( dis[i] < dis[now] + cost[i] && dis[now] + cost[i] > 0 )
{
dis[i] = dis[now] + cost[i];
if( !vis[i] )
{
vis[i] = 1;
q.push(i);
}
}
}
}
}
return 0;
}
int main()
{
while( ~scanf("%d", &n) && n != -1 )
{
memset(a, 0, sizeof(a));
for( int i = 1; i <= n; i++ )
{
int num;
scanf("%d%d", &cost[i], &num);
for( int j = 1; j <= num; j++ )
{
int to;
scanf("%d", &to);
a[i][to] = 1;
}
}
int ans = spfa();
if( ans )
printf("winnable\n");
else
printf("hopeless\n");
}
return 0;
}
hdu 1317 XYZZY spfa判断负环
最新推荐文章于 2020-04-15 00:27:36 发布