-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrough.cpp
More file actions
277 lines (261 loc) · 5.35 KB
/
Copy pathrough.cpp
File metadata and controls
277 lines (261 loc) · 5.35 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include<bits/stdc++.h>
#define ERROR -99999999
#define INF 999999
using namespace std;
enum Color
{
WHITE,GREY,BLACK
};
struct Node
{
int data;
Node *next;
};
//----------------------------------------------------------------Classes------------------------------------------------------------
class IntLL //Integer Linked List
{
Node *head;
Node *tail;
int size;
public:
IntLL();
//~IntLL();
void Insert(int x,int pos=-1); //By Default Insert at end
void Delete(int pos=0); //By default delete at the beginning
void Print();
int SizeLL();
void freeLL();
int Element(int pos=0);
};
class IntQueue
{
IntLL L;
public:
//IntQueue();
//~IntQueue();
void Enqueue(int x);
void Dequeue();
int Front();
bool isEmpty();
int SizeQueue();
void freeQueue();
};
//---------------------------------------------------------------------Main function-------------------------------------------------
int main()
{
int N,D,s,i,j,x,n,u,len;
int MIN=INF;
cin>>N>>D>>s;
s--;
IntQueue *AdjWeight=new IntQueue[N];
IntQueue *AdjNode=new IntQueue[N];
Color *colour=new Color[N];
int *dist=new int[N];
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
cin>>x;
if(x<MIN)
MIN=x;
if(x!=INF && i!=j)
{
AdjWeight[i].Enqueue(x);
AdjNode[i].Enqueue(j);
}
}
dist[i]=INF;
colour[i]=WHITE;
}
int countRelax,countModification;
countRelax=countModification=0;
IntQueue Q;
dist[s]=0;
Q.Enqueue(s);
colour[s]=GREY;
while(!Q.isEmpty())
{
u=Q.Front();
Q.Dequeue();
colour[u]=WHITE;
len=AdjNode[u].SizeQueue();
for(i=0;i<len;i++)
{
x=AdjWeight[u].Front();
AdjWeight[u].Dequeue();
n=AdjNode[u].Front();
AdjNode[u].Dequeue();
countRelax++;
if(dist[u]!=INF && dist[n]>dist[u]+x)
{
countModification++;
dist[n]=dist[u]+x;
if(dist[n]<MIN) //And hence graph has negative edge cycle.
{
cout<<-1<<endl;
delete[] AdjWeight;
delete[] AdjNode;
delete[] colour;
delete[] dist;
return 0;
}
if(colour[n]==WHITE)
{
colour[n]=GREY;
Q.Enqueue(n);
}
}
AdjWeight[u].Enqueue(x);
AdjNode[u].Enqueue(n);
}
}
for(int i=0;i<N;i++)
cout<<dist[i]<<" ";
cout<<countRelax<<" "<<countModification<<endl;
delete[] AdjWeight;
delete[] AdjNode;
delete[] colour;
delete[] dist;
return 0;
}
//-----------------------------------------------------Class IntLL functions-----------------------------------------------------
IntLL::IntLL()
{
head=tail=NULL;
size=0;
//cout<<"Head and Tail Initialized\n";
}
void IntLL::Insert(int x, int pos)
{
//pos =-1 is insert at tail
//pos = 0 is insert at head
if(pos>size&&pos<-1)
{
cout<<"Invalid Postion of Insertion in Linked List.\n";
return;
}
Node *newNode=new Node();
newNode->data=x;
if(size==0) //Empty Linked List
{
head=tail=newNode;
newNode->next=NULL;
}
else if(pos==0) //Insert at Head
{
newNode->next=head;
head=newNode;
}
else if(pos==size||pos==-1) //Insert at end;
{
tail=tail->next=newNode;
newNode->next=NULL;
}
else //Insert in between
{
Node* temp=head;
for(int i=0;i<pos-1;i++)
temp=temp->next;
newNode->next=temp->next;
temp->next=newNode;
}
//cout<<head<<" "<<tail<<" Inserted location and value"<<newNode<<" "<<x<<endl;
size++;
}
void IntLL::Print()
{
Node *temp=head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
int IntLL::SizeLL()
{
return size;
}
void IntLL::Delete(int pos)
{
Node *temp=head;
if(pos<0 && pos>=size)
{
cout<<"Invalid Delete Position in Linked List\n";
return;
}
if(pos==0)
{
head=temp->next;
}
else
{
for(int i=0;i<pos-1;i++)
temp=temp->next;
Node *temp2=temp;
temp=temp->next;
temp2->next=temp->next;
if(pos==size-1)
tail=temp2;
}
temp->next=NULL;
free(temp);
size--;
}
void IntLL::freeLL()
{
Node *temp=head;
Node *temp2;
head=tail=NULL;
while(temp!=NULL)
{
temp2=temp;
temp=temp->next;
temp2->next=NULL;
free(temp2);
}
}
int IntLL::Element(int pos)
{
if(pos>=size)
{
//cout<<"Invalid Position\n";
return ERROR;
}
if(pos==0)
return head->data;
if(pos==size-1)
return tail->data;
Node *temp=head;
for(int i=0;i<pos;i++)
temp=temp->next;
return temp->data;
}
//---------------------------------------------------- Class IntQueue Functions-----------------------------------------------------
void IntQueue::Enqueue(int x)
{
L.Insert(x);
}
void IntQueue::Dequeue()
{
if(!L.SizeLL())
return;
else
L.Delete();
}
int IntQueue::Front()
{
return L.Element();
}
bool IntQueue::isEmpty()
{
return !L.SizeLL();
}
int IntQueue::SizeQueue()
{
return L.SizeLL();
}
void IntQueue::freeQueue()
{
L.freeLL();
}