-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuva10557.cpp
163 lines (122 loc) · 2.17 KB
/
uva10557.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
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <limits.h>
#include <list>
#include <queue>
#include <map>
#define TRvi(c, it) \
for(vector<int>::iterator it = c.begin(); it != c.end(); it++)
#define INF 2000000000
using namespace std;
int V;
vector<vector<int> > AdjList;
vector<int> energy;
vector<int> dist;
vector<int> connected;
bool bfs(int start, int dest)
{
vector<int> visited(V, 0);
queue<int> q;
q.push(start);
visited[start] = 1;
bool canReach = false;
while(!q.empty())
{
int front = q.front();
q.pop();
if(front == dest)
{
canReach = true;
break;
}
TRvi(AdjList[front], v)
{
if(!visited[*v])
{
visited[*v] = 1;
q.push(*v);
}
}
}
return canReach;
}
bool Bellman_Ford(int start) /* O(V*E) */
{
dist.assign(V, -INF);
dist[start] = 100;
for(int i = 0; i < V-1; i++)
{
for(int u = 0; u < V; u++)
{
TRvi(AdjList[u], v)
{
if(dist[u] + energy[*v] > dist[*v] && dist[u] > 0)
{
dist[*v] = dist[u] + energy[*v];
}
if(connected[u])
connected[*v] = 1;
}
}
}
bool reachWithCycle = false; /* one more pass to check for positive cycle */
for(int u = 0; u < V; u++)
{
TRvi(AdjList[u], v)
{
if(dist[u] + energy[*v] > dist[*v] && dist[u] > 0) /* means inside a positive cycle */
{
if(bfs(u, V-1) || bfs(*v, V-1))
{
reachWithCycle = true;
break;
}
}
}
}
return reachWithCycle;
}
int main()
{
int start, end, numNeighbours;
int CaseNo = 0;
while(true)
{
cin >> V;
if(V == -1)
break;
CaseNo++;
AdjList.clear();
AdjList.resize(V);
energy.clear();
energy.resize(V);
connected.clear();
connected.resize(V, 0);
connected[0] = 1;
for(int u = 0; u < V; u++)
{
cin >> energy[u];
cin >> numNeighbours;
for(int i = 0; i < numNeighbours; i++)
{
cin >> end;
AdjList[u].push_back(end-1);
}
}
bool reachWithCycle = Bellman_Ford(0);
if(reachWithCycle)
{
cout << "winnable" << endl;
}
else if(dist[V-1] > 0)
cout << "winnable" << endl;
else
cout << "hopeless" << endl;
}
return 0;
}