CSES - Datatähti 2016 alku - Results
Submission details
Task:Lennot
Sender:ollpu
Submission time:2015-10-10 19:51:37 +0300
Language:Python2
Status:READY
Result:61
Feedback
groupverdictscore
#1ACCEPTED24
#2ACCEPTED37
#30
Test results
testverdicttimegroup
#1ACCEPTED0.06 s1details
#2ACCEPTED0.06 s1details
#3ACCEPTED0.05 s1details
#4ACCEPTED0.06 s1details
#5ACCEPTED0.07 s1details
#6ACCEPTED0.07 s2details
#7ACCEPTED0.06 s2details
#8ACCEPTED0.05 s2details
#9ACCEPTED0.07 s2details
#10ACCEPTED0.07 s2details
#11--3details
#12--3details
#13--3details
#14ACCEPTED0.84 s3details
#15--3details
#16--3details
#17--3details

Code

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from sys import stdin
import gc

maxint = sys.maxint

def readline():
    return stdin.readline().strip()

line1 = readline().split(' ')
# Kaupunkeja
n = int(line1[0])
# Lentoja
m = int(line1[1])

# Tuple indexes
fl_from = 0
fl_to = 1
fl_price = 2

ct_total_price_even = 0
ct_even_price_set = 1
ct_total_price_odd = 2
ct_odd_price_set = 3
ct_total_price = 4
ct_calculated = 5
ct_con_to = 6
ct_in_uncalc = 7
ct_aid = 8

def minimum(a, b):
    if a < b:
        return a
    else:
        return b


def run():
    # Generate all cities
    def gen(i):
        return [maxint, False, maxint, False, maxint, False, [], False, i]
    cities = map(gen, xrange(n))
    
    # Generate all flights for the cities
    gc.disable()
    for f in xrange(m):
        row = readline().split()
        flight = (int(row[0])-1, int(row[1])-1, int(row[2]))
        cities[flight[fl_from]][ct_con_to].append(flight)
    gc.enable()
    
    # Determine start, end, and current cities
    start = cities[0]
    end = cities[-1]
    current = start
    
    # GC - kinda not needed anymore?
    gc.collect()
    
    # Define initial uncalculated-array
    uncalculated = [current]
    # Make variables for uncalculated's commonly used functions
    append_to_uncalculated = uncalculated.append
    remove_from_uncalculated = uncalculated.remove

    # Set initial data to current-node
    current[ct_total_price_even] = 0
    current[ct_even_price_set] = True
    
    
    # (This while is broken via a brake-statement)
    while True:
        if current[ct_aid] == end[ct_aid]:
            # Finished!
            print(end[ct_total_price])
            break
        
        # Store commonly used values from current
        even_set = current[ct_even_price_set]
        even_cur = current[ct_total_price_even]
        odd_set = current[ct_odd_price_set]
        odd_cur = current[ct_total_price_odd]
        # Calculate distances from current to next nodes
        for flight in current[ct_con_to]:
            city = cities[flight[fl_to]]
            price_changed = False
            # Calculate next node's odd value
            if even_set:
                new_odd_price = even_cur + flight[fl_price]
                if new_odd_price < city[ct_total_price_odd]:
                    city[ct_total_price_odd] = new_odd_price
                    city[ct_odd_price_set] = True
                    price_changed = True
            
            # Calculate next node's even value
            if odd_set:
                new_even_price = odd_cur
                if new_even_price < city[ct_total_price_even]:
                    city[ct_total_price_even] = new_even_price
                    city[ct_even_price_set] = True
                    price_changed = True
            # If price of target city was changed...
            if price_changed:
                # Calculate new minimum distance to node
                city[ct_total_price] = minimum(city[ct_total_price_even], city[ct_total_price_odd])
                # Add node to the uncalculated list, to be calculated from
                if city[ct_calculated]:
                    city[ct_calculated] = False
                    append_to_uncalculated(city)
                    city[ct_in_uncalc] = True
                elif not city[ct_in_uncalc]:
                    append_to_uncalculated(city)
                    city[ct_in_uncalc] = True
        
        # Remove current node from the uncalculated-list -- it is now calculated
        remove_from_uncalculated(current)
        current[ct_calculated] = True
        
        # Find next city to set as current
        smallest_price = maxint
        for city in uncalculated:
            cityprice = city[ct_total_price]
            if cityprice < smallest_price:
                smallest = city
                smallest_price = cityprice
        
        current = smallest
        
# import profile # debug
# profile.run('run()')
run()

Test details

Test 1

Group: 1

Verdict: ACCEPTED

input
10 20
2 1 3
7 6 4
1 6 7
1 6 1
...

correct output
8

user output
8

Test 2

Group: 1

Verdict: ACCEPTED

input
10 20
4 3 10
1 10 9
3 4 10
2 6 7
...

correct output
9

user output
9

Test 3

Group: 1

Verdict: ACCEPTED

input
10 20
5 7 4
6 1 1
7 3 8
8 4 2
...

correct output
8

user output
8

Test 4

Group: 1

Verdict: ACCEPTED

input
10 20
1 6 2
5 3 3
7 3 6
5 6 2
...

correct output
13

user output
13

Test 5

Group: 1

Verdict: ACCEPTED

input
10 20
10 8 5
2 4 7
9 4 7
9 4 1
...

correct output
4

user output
4

Test 6

Group: 2

Verdict: ACCEPTED

input
1000 2000
91 828 365044406
17 984 445675537
251 852 100987451
907 487 58830088
...

correct output
11893353673

user output
11893353673

Test 7

Group: 2

Verdict: ACCEPTED

input
1000 2000
722 939 530579090
404 606 268877348
133 750 760086153
506 46 582310443
...

correct output
30248963445

user output
30248963445

Test 8

Group: 2

Verdict: ACCEPTED

input
1000 2000
340 237 43690066
217 141 453160975
744 202 639037814
605 926 404985542
...

correct output
3126797692

user output
3126797692

Test 9

Group: 2

Verdict: ACCEPTED

input
1000 2000
88 312 190442306
480 402 411574469
29 901 397491243
636 459 323246996
...

correct output
18416073173

user output
18416073173

Test 10

Group: 2

Verdict: ACCEPTED

input
1000 2000
333 228 718389176
796 286 323493090
743 43 751876815
128 554 175625940
...

correct output
6399349335

user output
6399349335

Test 11

Group: 3

Verdict:

input
100000 200000
28264 92686 186865663
92570 33956 925976418
87377 71249 644757113
16701 81203 922125505
...

correct output
518249578675

user output
(empty)

Test 12

Group: 3

Verdict:

input
100000 200000
95740 71482 846654568
44131 16806 670712211
3967 49254 424174139
39369 53007 830346557
...

correct output
920862321580

user output
(empty)

Test 13

Group: 3

Verdict:

input
100000 200000
79947 25489 71554257
59184 25577 328436360
82945 73554 4942918
22380 92385 874250042
...

correct output
399407698440

user output
(empty)

Test 14

Group: 3

Verdict: ACCEPTED

input
100000 200000
31139 12960 580545990
27744 95556 747296719
46969 42578 840321561
5638 28960 513805324
...

correct output
165235287505

user output
165235287505

Test 15

Group: 3

Verdict:

input
99993 199980
1 3 1
3 2 1
1 4 1
4 2 1
...

correct output
2

user output
(empty)

Test 16

Group: 3

Verdict:

input
100000 149994
93867 98509 1755709
85029 99843 1347591
10305 35305 6447
75638 80585 1829972
...

correct output
1124960

user output
(empty)

Test 17

Group: 3

Verdict:

input
100000 200000
70413 71496 49
15963 40963 18635
81291 89420 1850028
8848 33848 17316
...

correct output
110298

user output
(empty)