Language: C++


out = open('out3.txt','a')
class graph:
    INF = 10000
    G = []
    Path = []
    E = 0
    V = 0
    def __init__(self):
        dataRaw = raw_input()
        raw = dataRaw.strip().split(' ')
        self.E = int(raw[0])+1
        self.V = int(raw[1])
        self.G = [[self.INF] * (self.E) for i in range(self.E)]
        self.Path = [[0] * (self.E) for i in range(self.E)]

        # check unicom component
        def find(root, parent):
            while root != parent[root]:
                parent[root] = parent[parent[root]]
                root = parent[root]
            return root

        def connect( u, v, parent) :
            # ru = find(u, parent)
            rv = find(v, parent)
            parent[u] = rv


        # father = [0]* (self.E)

        # for i in range(1,self.E):
        #    father[i] = i


        for _ in range(0,self.V):
            dataRaw = raw_input()
            raw = dataRaw.strip().split(' ')
            i = int(raw[0])
            j = int(raw[1])
            k = int(raw[2])
            self.G[i][j] = k
            self.G[j][i] = k
            # connect(i,j,father)

        # compent = set()
        # for i in range(1,self.E):
        #    compent.add(find(i,father))

        #print "compoent : ",
        #print compent
        #print father

        for i in range(1,self.E):
            self.G[i][i] = 0

        for k in range(0,self.E):
            for i in range(0, self.E):
                for j in range(0, self.E):
                    if self.G[i][k]+self.G[k][j] < self.G[i][j]:
                        self.G[i][j] = self.G[i][k]+self.G[k][j]
                        self.Path[i][j] = k
        #for i in self.G :
        #    print i

    def shortPath(self,i,j):
        path = []

        def core(i,j,path):
            if self.Path[i][j] == 0:
                if len(path) == 0 or path[-1] != i:
                    path.append(i)
                path.append(j)
                return
            else :
                core(i, self.Path[i][j],path)
                core(self.Path[i][j], j, path)
        core(i, j,path)
        path.pop(0)
        return path

class driver :
    plan = []
    order = set()
    location = 1
    nextSegmentLeftTime = 0
    target = 1

    def __init__(self,g):
        self.map = g

    def routeBaseline(self):
        # choose target
        # route
        # print self.order,
        # print self.plan
        if len(self.order)==0 and len(self.plan) == 0:
            self.order.add(1)

        last = self.location

        if len(self.plan) > 0:
            last = self.plan[-1]

        # out.write("| last : " + str(last) +" "+ str(type(last)) )
        if len(self.order) > 0:
            for i in self.order:
                path = self.map.shortPath(last,i)
                for j in path:
                    self.plan.append(j)
                last = i
            self.order = set()

        # print self.order,
        # print self.plan
        if len(self.plan) > 0:
            self.target = self.plan[0]
            self.nextSegmentLeftTime = self.map.G[self.location][self.target]
            self.plan.pop(0)

    def routeLazy(self,shopOrder):

        # choose every time choose nearest
        if len(self.order)==0 and len(self.plan) == 0:
            self.order.add(1)

        if shopOrder == True:
            self.order.add(1)

        last = self.location
        if len(self.plan) > 0:
            last = self.plan[-1]

        # out.write("| last : " + str(last) +" "+ str(type(last)) )
        if len(self.order) > 0:
            id = 1
            dis = 1e5
            for i in self.order:
                if self.map.G[i][last] < dis:
                    dis = self.map.G[i][last]
                    id = i
            path = self.map.shortPath(last,id)
            for j in path:
                self.plan.append(j)
            self.order.remove(id)

        # print self.order,
        # print self.plan
        if len(self.plan) > 0:
            self.target = self.plan[0]
            self.nextSegmentLeftTime = self.map.G[self.location][self.target]
            self.plan.pop(0)

    def move(self,method):
        #out.write('-1\n')
        #return

        if self.location == self.target:
            #stay
            #out.write('-1\n')
            print (-1)
            return

        assert(self.nextSegmentLeftTime > 0)
        self.nextSegmentLeftTime = self.nextSegmentLeftTime - 1
        #out.write(str(self.target)+'\n')
        print (self.target)
        if self.nextSegmentLeftTime == 0:
            self.location = self.target

            if method == "lazy":
                self.order.discard(self.location)


    def onRoute(self):
        return self.nextSegmentLeftTime != 0



class shop :
    order = set()

class timer :
    orders = []
    times = []
    orderIndex = 0
    currTime = 0
    Tmax = 0
    def __init__(self):
        Tmax = raw_input()
        self.Tmax = int(Tmax)
        for i in range(0, self.Tmax):
            n = raw_input()
            n = int(n)

            if n==0 :
                continue
            else :
                dataRaw = raw_input()
                data = dataRaw.strip().split(' ')
                self.times.append(i+1)
                self.orders.append(int(data[1]))
        #print self.times

    def run(self,shop,driver):
        while self.currTime < self.Tmax:
            self.currTime = self.currTime + 1
            '''
            out.write ("================================\n")
            out.write ("curr time : " + str(self.currTime))

            out.write ("status before : ")
            out.write ("shop status : " + str(shop.order))
            out.write ("driver status : ")
            out.write ("order : " + str(driver.order),)
            out.write ("| plan : " + str(driver.plan),)
            out.write ("| on route : " + str(driver.onRoute()))
            out.write ("| location : " + str(driver.location),)
            out.write ("| target : " + str(driver.target),)
            out.write ("| left time : " + str(driver.nextSegmentLeftTime) + '\n')
            '''
            # add goods
            if self.orderIndex < len(self.times) and self.currTime == self.times[self.orderIndex]:
                if driver.map.G[1][self.orders[self.orderIndex]] < 10000:
                    shop.order.add(self.orders[self.orderIndex])
                self.orderIndex = self.orderIndex + 1

            if driver.onRoute() == False and driver.location == 1:
                driver.order = driver.order | shop.order
                # if len(driver.order)>0:
                #    print "order size : ", len(driver.order)
                shop.order = set()

            if driver.onRoute() == False:
                driver.routeLazy(len(shop.order)>0)

            driver.move("lazy")

            # debug
            '''
            out.write ("status after : ")
            out.write ("shop status : " + str(shop.order))
            out.write ("driver status : ")
            out.write ("order : " + str(driver.order),)
            out.write ("| plan : " + str(driver.plan),)
            out.write ("| on route : " + str(driver.onRoute()))
            out.write ("| location : " + str(driver.location),)
            out.write ("| target : " + str(driver.target),)
            out.write ("| left time : " + str(driver.nextSegmentLeftTime) + '\n')
            out.write ("================================\n")
            '''

if __name__=="__main__":

    shop = shop()
    g = graph()

    #print(g.shortPath(1,6))

    driver = driver(g)
    ti = timer()
    ti.run(shop,driver)


Compiler output:
a.cc:2:12: warning: multi-character literal with 8 characters exceeds 'int' size of 4 bytes
    2 | out = open('out3.txt','a')
      |            ^~~~~~~~~~
a.cc:17:11: error: invalid preprocessing directive #check
   17 |         # check unicom component
      |           ^~~~~
a.cc:25:15: error: invalid preprocessing directive #ru
   25 |             # ru = find(u, parent)
      |               ^~
a.cc:30:11: error: invalid preprocessing directive #father
   30 |         # father = [0]* (self.E)
      |           ^~~~~~
a.cc:32:11: error: invalid preprocessing directive #for
   32 |         # for i in range(1,self.E):
      |           ^~~
a.cc:33:14: error: invalid preprocessing directive #father
   33 |         #    father[i] = i
      |              ^~~~~~
a.cc:44:15: error: invalid preprocessing directive #connect
   44 |             # connect(i,j,father)
      |               ^~~~~~~
a.cc:46:11: error: invalid preprocessing directive #compent
   46 |         # compent = set()
      |           ^~~~~~~
a.cc:47:11: error: invalid preprocessing directive #for
   47 |         # for i in range(1,self.E):
      |           ^~~
a.cc:48:14: error: invalid preprocessing directive #compent
   48 |         #    compent.add(find(i,father))
      |              ^~~~~~~
a.cc:50:10: error: invalid preprocessing directive #print
   50 |         #print "compoent : ",
      |          ^~~~~
a.cc:51:10: error: invalid preprocessing directive #print
   51 |         #print compent
      |          ^~~~~
a.cc:52:10: error: invalid preprocessing directive #print
   52 |         #print father
      |          ^~~~~
a.cc:63:10: error: invalid preprocessing directive #for
   63 |         #for i in self.G :
      |          ^~~
a.cc:64:14: error: invalid preprocessing directive #print
   64 |         #    print i
      |              ^~~~~
a.cc:93:11: error: invalid preprocessing directive #choose
   93 |         # choose target
      |           ^~~~~~
a.cc:94:11: error: invalid preprocessing directive #route
   94 |         # route
      |           ^~~~~
a.cc:95:11: error: invalid preprocessing directive #print
   95 |         # print self.order,
      |           ^~~~~
a.cc:96:11: error: invalid preprocessing directive #print
   96 |         # print self.plan
      |           ^~~~~
a.cc:105:11: error: invalid preprocessing directive #out
  105 |         # out.write("| last : " + str(last) +" "+ str(type(last)) )
      |           ^~~
a.cc:114:11: error: invalid preprocessing directive #print
  114 |         # print self.order,
      |           ^~~~~
a.cc:115:11: error: invalid preprocessing directive #print
  115 |         # print self.plan
      |           ^~~~~
a.cc:123:11: error: invalid preprocessing directive #choose
  123 |         # choose every time choose nearest
      |           ^~~~~~
a.cc:134:11: error: invalid preprocessing directive #out
  134 |         # out.write("| last : " + str(last) +" "+ str(type(last)) )
      |           ^~~
a.cc:147:11: error: invalid preprocessing directive #print
  147 |         # print self.order,
      |           ^~~~~
a.cc:148:11: error: invalid preprocessing directive #print
  148 |         # print self.plan
      |           ^~~~~
a.cc:155:10: error: invalid preprocessing directive #out
  155 |         #out.write('-1\n')
      |          ^~~
a.cc:156:10: error: invalid preprocessing directive #return
  156 |         #return
      |          ^~~~~~
a.cc:159:14: error: invalid preprocessing directive #stay
  159 |             #stay
      |              ^~~~
a.cc:160:14: error: invalid preprocessing directive #out
  160 |             #out.write('-1\n')
      |              ^~~
a.cc:166:10: error: invalid preprocessing directive #out
  166 |         #out.write(str(self.target)+'\n')
      |          ^~~
a.cc:203:10: error: invalid preprocessing directive #print
  203 |         #print self.times
      |          ^~~~~
a.cc:208:13: error: empty character constant
  208 |             '''
      |             ^~
a.cc:208:15: warning: missing terminating ' character
  208 |             '''
      |               ^
a.cc:208:15: error: missing terminating ' character
a.cc:221:13: error: empty character constant
  221 |             '''
      |             ^~
a.cc:221:15: warning: missing terminating ' character
  221 |             '''
      |               ^
a.cc:221:15: error: missing terminating ' character
a.cc:222:15: error: invalid preprocessing directive #add
  222 |             # add goods
      |               ^~~
a.cc:230:25: error: missing binary operator before token "("
  230 |                 # if len(driver.order)>0:
      |                         ^
a.cc:240:15: warning: missing terminating ' character
  240 |             '''
      |               ^
a.cc:251:15: warning: missing terminating ' character
  251 |             '''
      |               ^
a.cc:230: error: unterminated #if
  230 |                 # if len(driver.order)>0:
a.cc:2:1: error: 'out' does not name a type
    2 | out = open('out3.txt','a')
      | ^~~