Skip to content

Commit 0c90cdf

Browse files
committed
added some spoj problem and problem sums from poi
1 parent db66b8c commit 0c90cdf

13 files changed

+876
-35
lines changed

New folder/dijkstra.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from collections import defaultdict
2+
3+
import heapq
4+
5+
# initialisation
6+
7+
n = 5
8+
9+
graph = defaultdict(list)
10+
11+
dist = [float('inf')]*n
12+
13+
heap = []
14+
15+
# adding the following edges
16+
17+
edges = [(0, 1), (1, 2), (2, 4), (4, 3), (3, 0), (3, 1), (1, 4)]
18+
19+
weight = [6, 5, 5, 1, 1, 2, 2];
20+
21+
for i in range (0, len(edges)):
22+
23+
x = edges[i][0]
24+
25+
y = edges[i][1]
26+
27+
w = weight[i];
28+
29+
#Adding the weighted edges
30+
31+
graph[x].append((w, y))
32+
33+
graph[y].append((w, x))
34+
35+
36+
37+
src = 0
38+
39+
# add edge to heap with edge weight priority
40+
41+
heapq.heappush(heap, (0, src))
42+
43+
dist[0] = 0
44+
45+
46+
47+
while len(heap) > 0:
48+
49+
# heap[0] gives the top element in the min heap
50+
51+
node = heap[0][1]
52+
53+
heapq.heappop(heap)
54+
55+
for i in range(0, len(graph[node])):
56+
57+
nextnode = graph[node][i][1]
58+
59+
w = graph[node][i][0]
60+
61+
62+
63+
if dist[nextnode] > dist[node] + w:
64+
65+
dist[nextnode] = dist[node] + w;
66+
67+
heapq.heappush(heap, (dist[nextnode], nextnode))
68+
69+
70+
71+
print("Shortest distance from source vertex " + str(src) + " to vertex")
72+
73+
for i in range(0,n):
74+
75+
print(str(i) + ": " + str(dist[i]))

codechefcycles.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
n = int(input())
2+
3+
perm = [0] + list(map(int, input().split()))
4+
5+
6+
7+
index = 1
8+
9+
last = 1
10+
11+
dim = 0
12+
13+
14+
15+
ans = []
16+
17+
ans.append([])
18+
19+
visited = [True, True] + [False]*(n-1)
20+
21+
22+
23+
def first_unvisited():
24+
25+
for i in range(n+1):
26+
27+
if not visited[i]:
28+
29+
return i
30+
31+
else:
32+
33+
return -1
34+
35+
36+
37+
38+
39+
while True:
40+
41+
ans[dim].append(index)
42+
43+
index = perm[index]
44+
45+
visited[index] = True
46+
47+
48+
49+
if index == last:
50+
51+
unvisited_ind = first_unvisited()
52+
53+
ans[dim].append(index)
54+
55+
56+
57+
if unvisited_ind == -1:
58+
59+
break
60+
61+
else:
62+
63+
index = unvisited_ind
64+
65+
last = index
66+
67+
ans.append([])
68+
69+
dim+=1
70+
71+
72+
73+
print(dim+1)
74+
75+
for i in range(dim+1):
76+
77+
print(" ".join(map(str, ans[i])))

codechefwealthdisparity.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
arr=list(map(int,input().split()))
2+
n=arr[0]
3+
graph=[[]for i in range(n+1)]
4+
cost=[]
5+
for i in range(1,n+1):
6+
cost.append(arr[i])
7+
cost=[0]+cost
8+
manager=[]
9+
for i in range(n+1,2*n+1):
10+
manager.append(arr[i])
11+
manager=[0]+manager
12+
for i in range(1,n+1):
13+
k=manager[i]
14+
if manager[i]!=-1:
15+
graph[k].append(i)
16+
17+
18+
idx=manager.index(-1)
19+
20+
max1=0
21+
zx=idx
22+
zy=-1
23+
for i in range(1,n+1):
24+
zz=cost[idx]
25+
if manager[i]!=-1:
26+
zz=zz-cost[i]
27+
if zz>max1:
28+
zy=i
29+
max1=zz
30+
31+
for i in range(1,n+1):
32+
if i!=idx:
33+
for k in graph[i]:
34+
newcost=cost[i]-cost[k]
35+
if newcost>max1:
36+
zx=i
37+
zy=k
38+
max1=newcost
39+
40+
41+
print(max1)

codeforcesnewreform.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
n,m=map(int,input().split())
2+
3+
graph=[[]for i in range(n)]
4+
for i in range(m):
5+
a,b=map(int,input().split())
6+
a=a-1
7+
b=b-1
8+
graph[a].append(b)
9+
graph[b].append(a)
10+
11+
visited=[0 for i in range(n)]
12+
tree=True
13+
def dfs(src,par):
14+
visited[src]=1
15+
16+
for child in graph[src]:
17+
if visited[child]==0:
18+
19+
dfs(child,src)
20+
elif child!=par:
21+
tree=False
22+
return
23+
24+
ans=0
25+
for src in range(n):
26+
if visited[src]==0:
27+
if dfs(src,-1):
28+
ans=ans+1
29+
30+
print(ans)
31+
32+

hackerrankroadsandlibrary.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
for _ in range(int(input())):
2+
n,m,lib,road=map(int,input().split())
3+
graph =[[]for i in range(n)]
4+
for i in range(m):
5+
a,b=map(int,input().split())
6+
a=a-1
7+
b=b-1
8+
graph[a].append(b)
9+
graph[b].append(a)
10+
count=[0 for i in range(n)]
11+
visited=[0 for i in range(n)]
12+
countroad=0
13+
def dfs(src):
14+
global countroad
15+
visited[src]=1
16+
17+
for child in graph[src]:
18+
if visited[child]==0:
19+
countroad+=1
20+
dfs(child)
21+
count=0
22+
for i in range(n):
23+
if visited[i]==0:
24+
count+=1
25+
dfs(i)
26+
chuchi=0
27+
if lib<=road:
28+
print(n*lib)
29+
else:
30+
chuchi=lib*count+countroad*road
31+
print(chuchi)
32+
33+

priorityqueue-master/LICENSE.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2015 Matthew Westcott
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

priorityqueue-master/README.txt

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
priorityqueue.py
2+
3+
Priority Queue Implementation with a O(log n) Remove Method
4+
5+
This project implements min- amd max-oriented priority queues based on binary
6+
heaps. I found the need for a priority queue with a O(log n) remove method.
7+
This can't be achieved with any of Python's built in collections including
8+
the heapq module, so I built my own. The heap is arranged according to a given
9+
key function.
10+
11+
Usage:
12+
>>> from priorityqueue import MinHeapPriorityQueue
13+
>>> items = [4, 0, 1, 3, 2]
14+
>>> pq = MinHeapPriorityQueue(items)
15+
>>> pq.pop()
16+
0
17+
18+
A priority queue accepts an optional key function.
19+
>>> items = ['yy', 'ttttttt', 'z', 'wwww', 'uuuuuu', 'vvvvv', 'xxx']
20+
>>> pq = MinHeapPriorityQueue(items, key=len)
21+
>>> pq.pop()
22+
'z'
23+
>>> pq.pop()
24+
'yy'
25+
26+
Internally, the queue is a list of tokens of type 'Locator', which contain
27+
the priority value, the item itself, and its current index in the heap.
28+
The index field is updated whenever the heap is modified. This is what
29+
allows us to remove in O(log n). Appending an item returns it's Locator.
30+
>>> token = pq.append('a')
31+
>>> token
32+
Locator(value=1, item='a', index=0)
33+
>>> pq.remove(token)
34+
'a'
35+
36+
If we want to be able to remove any item in the list we can maintain an
37+
auxiliary dictionary mapping items to their Locators. Here's a simple
38+
example with unique items:
39+
>>> items = [12, 46, 89, 101, 72, 81]
40+
>>> pq = MinHeapPriorityQueue()
41+
>>> locs = {}
42+
>>> for item in items:
43+
... locs[item] = pq.append(item)
44+
>>> locs[46]
45+
Locator(value=46, item=46, index=1)
46+
>>> pq.remove(locs[46])
47+
46
48+
49+
Iterating with 'for item in pq' or iter() will produce the items, not the
50+
Locator instances used in the internal representation. The items will be
51+
generated in sorted order.
52+
>>> items = [3, 1, 0, 2, 4]
53+
>>> pq = MinHeapPriorityQueue(items)
54+
>>> for item in pq:
55+
... print(item)
56+
0
57+
1
58+
2
59+
3
60+
4
Binary file not shown.

0 commit comments

Comments
 (0)