Mock CCC '18 Contest 1 S4 - A Graph Problem

View as PDF

Submit solution


Points: 12 (partial)
Time limit: 4.5s
Memory limit: 1G

Problem type

You are given four sequences a, b, c, and d, each consisting of exactly M positive integers.

Let graph g(k) be the graph consisting of vertices labeled from 1 to N where there is a directed edge from vertex u to vertex v if and only if there exists some index i such that ai=u, bi=v, and cikdi.

Define f(s,t,k) to be 1 if there is a path from vertex s to vertex t in g(k), and 0 otherwise.

Given S, T, and K, compute k=1Kf(S,T,k): the sum of f(S,T,k) as k ranges over all positive integers from 1 to K.

Constraints

2N1000

1M5000

1K109

1S,TN,ST

1ai,biN,aibi

1cidiK

For any pair (x,y) with xy, there is at most one index j such that aj=x and bj=y.

Input Specification

The first line contains three space-separated integers N, M, and K.

The second line contains two space-separated integers S and T.

The next M lines each contain four space-separated integers. Specifically, line i of the input contains ai2, bi2, ci2, and di2 in order for 3iM+2.

Output Specification

Print, on a single line,

k=1Kf(S,T,k)

Sample Input 1

Copy
4 5 10
3 2
1 2 4 7
3 1 1 6
3 4 7 10
2 4 3 5
4 2 8 9

Sample Output 1

Copy
5

Sample Input 2

Copy
4 5 9
1 4
1 2 3 5
1 3 6 7
1 4 2 3
2 4 4 6
3 4 7 9

Sample Output 2

Copy
5

Comments


  • 0
    insect  commented on Feb. 6, 2018, 2:21 a.m.

    Can someone help find the bug in my program?


    • 0
      aeternalis1  commented on Feb. 6, 2018, 3:01 a.m.

      Haven't exactly found the bug, but I changed the dfs to a bfs and got 14/15 instead. Still failed the last case, but it's something. I'll edit if I find something else.


      • 1
        insect  commented on Feb. 6, 2018, 3:12 a.m.

        That's weird. shouldn't dfs work just as well as bfs for testing connectivity?


        • 3
          aeternalis1  commented on Feb. 6, 2018, 3:32 a.m.

          Ok, I should've noticed this earlier, but you need to reread the problem statement. Your code AC's upon removal of one line.


          • 0
            insect  commented on Feb. 6, 2018, 3:37 a.m.

            lol it's a directed graph. Thanks!