Skip to content

Instantly share code, notes, and snippets.

@CliffLin
Created September 20, 2015 08:44
Show Gist options
  • Save CliffLin/a330e1084f0a64b1f431 to your computer and use it in GitHub Desktop.
Save CliffLin/a330e1084f0a64b1f431 to your computer and use it in GitHub Desktop.
global m
def short(data,start,end,time,n,cost,array):
global m
if start > end:
s = end
e = start
else:
s = start
e = end
if len(data[s])>0:
if e in data[s].keys():
if m==-1 or m > cost + data[s][e][time]:
m = cost + data[s][e][time]
for i in range(1,len(data)+1):
if i in array:
continue
if i in data[s].keys():
array.append(i)
cost += data[s][i][time]
time += data[s][i][time]
if time>=24:
time-=24
short(data,i,end,time,n+1,cost,array)
f = open('test.in')
n = f.readline()
n = int(n)
for i in range(n):
print "Case #"+str(i+1)+":",
temp = f.readline()
temp = temp.split(' ')
city = int(temp[0])
road = int(temp[1])
ask = int(temp[2])
data = {}
for t in range(city):
data[t+1] = {};
for j in range(road):
temp = f.readline()
temp = temp.split(' ')
tempTime = f.readline()
tempTime = tempTime.split(' ')
for t in range(24):
tempTime[t] = int(tempTime[t])
data[int(temp[0])][int(temp[1])]=tempTime
start = 1
for k in range(ask):
temp =f.readline()
temp = temp.split(' ')
end = int(temp[0])
time = int(temp[1])
m = -1
short(data,start,end,time,1,0,[start,end])
print m,
print "\n",
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment