[SOLUTION] Weird full graph solution codechef 2021

 Weird full graph solution codechef 2021

                    SOLUTION

” CLICK HERE “

 You are given a weighted directed graph of 

N vertices and N2 edges, and an array A of length N. Initially, every ordered pair of vertices (u,v) is connected by a directed edge uv of cost Au (this also includes self-loops).

Then there were M changes in edges' weights such that the weight of the directed edge uv is now equal to c. You are asked to find the sum of the lengths of the shortest paths over all ordered pairs of distinct vertices.

Input  Weird full graph solution codechef 2021

  • The first line contains a single integer T denoting the number of test cases. The description of T test cases follows.
  • The first line of each test case contains a single integer N.
  • The second line contains N space-separated integers A1,A2,,AN.
  • The third line of each test case contains a single integer M.
  • The next M lines contain information about the changed edges in the u v c format.

Output  Weird full graph solution codechef 2021

For each test case, output the sum of the lengths of the shortest paths over all ordered pairs of vertices.

Constraints  Weird full graph solution codechef 2021

  • 1N106
  • 0M2000
  • 0Ai,c106
  • 1u,vN
  • The sum of N over all tests does not exceed 106.
  • The sum of M over all tests does not exceed 2000.
  • It is guaranteed that the weight of the edge uv was changed at most once.

Subtasks  Weird full graph solution codechef 2021

Subtask #1 (8 points, time limit 1 second): The sum N for all tests does not exceed 500.

Subtask #2 (22 points, time limit 1 second): Each vertex belongs to at most one changed edge (in other words, the edges do not overlap).

Subtask #3 (30 points, time limit 1 second): The sum M for all tests does not exceed 500.

Subtask #4 (40 points, time limit 4.5 seconds): original constraints.

Sample Input

3
3
2 4 1
0
4
1 2 3 4
2
1 2 1
3 4 2
5
2 6 5 1 7
5
1 3 4
2 4 3
3 5 4
4 1 2
5 2 3

Sample Output

14
29
72

Explanation  Weird full graph solution codechef 2021

Example case 1: The lengths of the shortest paths between vertices u and v are Au and the answer is 2(2+4+1)=14.

Example case 2: The lengths of the shortest paths between the vertices u and v are equal to Au except for the pair of vertices (3,4) for which the distance is 2.


                    SOLUTION

” CLICK HERE “


Weird full graph solution codechef 2021Weird full graph solution codechef 2021Weird full graph solution codechef 2021Weird full graph solution codechef 2021




Reactions