-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10557.cpp
158 lines (151 loc) · 3.09 KB
/
10557.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
10557 - XYZZY
*/
#include<bits/stdc++.h>
using namespace std;
vector<int>graph[105];
bool visit[105];
int n;
int dist[105];
int energy[105];
bool bfs(int u)
{
visit[u]=true;
queue<int>q;
q.push(u);
while(!q.empty())
{
int u = q.front();
q.pop();
if(u==n)
return true;
for(int i=0; i<graph[u].size(); i++)
{
if(!visit[graph[u][i]])
{
visit[graph[u][i]]=true;
q.push(graph[u][i]);
}
}
}
return false;
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
while(cin>>n && n>0)
{
int e,room;
for(int i=1; i<=n; i++)
{
cin>>e>>room;
graph[i].clear();
energy[i]=e;
for(int j=0; j<room; j++)
{
int x;
cin>>x;
graph[i].push_back(x);
}
}
dist[1]=100;
for(int i=2; i<=n; i++)
{
dist[i]=-(1e6+7);
}
///here we find all distance based cost from 1 to n
for(int i=0; i<n-2; i++)
{
for(int u=1; u<=n; u++)
{
for(int j=0; j<graph[u].size(); j++)
{
int v = graph[u][j];
int d = energy[u];
if(dist[u]+d>0)
{
dist[v]=max(dist[v],dist[u]+d);
}
}
}
}
///if cost is still positive then we have a clear path
if(dist[n]>0)
{
cout<<"winnable"<<endl;
}///else we must go for further steps
else
{
bool possible =false;
///again checking if the cost is still update_able
///which means we have a cycle
///and after finding a cycle if we able to reach n
///then this is winnable
for(int u=1; u<=n && !possible ; u++)
{
for(int j=0; j<graph[u].size(); j++)
{
int v = graph[u][j];
int d = energy[u];
if(dist[u]+d>0 && dist[v]<dist[u]+d)
{
for(int i=1; i<=n; i++)
{
visit[i]=false;
}
if(bfs(u))
{
possible=true;
break;
}
}
}
}
if(possible)
{
cout<<"winnable"<<endl;
}
else
{
cout<<"hopeless"<<endl;
}
}
}
return 0;
}
/*
Sample Input
5
0 1 2
-60 1 3
-60 1 4
20 1 5
0 0
5
0 1 2
20 1 3
-60 1 4
-60 1 5
0 0
5
0 1 2
21 1 3
-60 1 4
-60 1 5
0 0
5
0 1 2
20 2 1 3
-60 1 4
-60 1 5
0 0
-1
Sample Output
hopeless
hopeless
winnable
winnable
*/