Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
2 views5 pages

Lab Handout

Uploaded by

jehadalam123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Lab Handout

Uploaded by

jehadalam123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Islamic University of Technology

Department of Computer Science and Engineering

Lab 4: Single Source Shortest Path in Graphs

CSE 4404: Algorithms Lab


Summer 2023-24
Islamic University of Technology
CSE 4404 Department of Computer Science and Engineering Lab 4

Task A. Shortest Signal Path


Time Limit: 1 second | Memory Limit: 512 MB

A research facility has established a network of n outposts, connected by m one-way signal links. Each link
transmits signals in one direction and has a certain delay associated with it.
Outpost 1, known as Command Center, initiates all signal transmissions. Your task is to compute the shortest
signal delay from the Command Center to each of the other outposts in the network.
It is guaranteed that every outpost is reachable from the Command Center.

Input Format

The first line contains two integers n and m (1 ≤ n ≤ 105 , 1 ≤ m ≤ 2 · 105 ) — the number of outposts and signal
links.
The next m lines each contain three integers a, b, and c (1 ≤ a, b ≤ n, 1 ≤ c ≤ 109 ) — meaning there is a one-way
signal link from outpost a to outpost b with a delay of c units.

Output Format

Print n space-separated integers: the minimum signal delay from the Command Center (outpost 1) to each outpost
1, 2, . . . , n.

Examples

Sample Input Sample Output


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

Page 1 of 4
Islamic University of Technology
CSE 4404 Department of Computer Science and Engineering Lab 4

Task B. Bicycles
Time Limit: 4 seconds | Memory Limit: 512 MB

Rifat and his friends are going to a festival in another city. All of them have bicycles — except Rifat. The festival
is held in city n, and all of them live in city 1. The region has n cities connected by m two-way roads, forming an
undirected graph.
Each city has exactly one bicycle for sale. The bicycle in city i takes si time to travel one unit of distance.
Rifat can buy any bicycle from any city, and he can buy multiple bicycles along the way. Once he owns a bicycle,
he can use it to travel along any road, and the time taken to traverse a road of length w with a bicycle from city
j is w · sj .
Rifat cannot travel without a bicycle. Since he wants to reach the festival as quickly as possible, he asks you to
help determine the shortest time it would take him to travel from city 1 to city n.
It is guaranteed that it is possible to reach any city from city 1.

Input Format

The first line contains a single integer t (1 ≤ t ≤ 100) — the number of test cases.
Each test case begins with a line containing two integers n and m (2 ≤ n ≤ 1000, n − 1 ≤ m ≤ 1000) — the
number of cities and roads.
The next m lines each contain three integers ui , vi , and wi (1 ≤ ui , vi ≤ n, ui ̸= vi , 1 ≤ wi ≤ 105 ) — describing a
two-way road between cities ui and vi with length wi . Multiple roads may exist between the same pair of cities.
The final line of each test case contains n integers s1 , s2 , . . . , sn (1 ≤ si ≤ 1000) — the time required per unit
distance for the bicycle in each city.

Output Format

For each test case, print a single integer — the minimum amount of time Rifat needs to reach city n from city 1.

Examples

Sample Input Sample Output

Page 2 of 4
Islamic University of Technology
CSE 4404 Department of Computer Science and Engineering Lab 4

3 19
5 5 36
1 2 2 14
3 2 1
2 4 5
2 5 7
4 5 1
5 2 1 3 3
5 10
1 2 5
1 3 5
1 4 4
1 5 8
2 3 6
2 4 3
2 5 2
3 4 1
3 5 8
4 5 2
7 2 8 4 1
7 10
3 2 8
2 1 4
2 5 7
2 6 4
7 1 2
4 3 5
6 4 2
6 7 1
6 7 4
4 5 9
7 6 5 4 3 2 1

Page 3 of 4
Islamic University of Technology
CSE 4404 Department of Computer Science and Engineering Lab 4

Marks Distribution

Task Marks
Task A 90%
Task B 10%

Practice Problems

Problem Links
Shortest Path Library Checker: https://judge.yosupo.jp/problem/shortest path
Vjudge: https://vjudge.net/problem/Yosupo-shortest path
Dijkstra? Codeforces: https://codeforces.com/problemset/problem/20/C
Vjudge: https://vjudge.net/problem/CodeForces-20C
Jzzhu and Cities Codeforces: https://codeforces.com/problemset/problem/449/B
Vjudge: https://vjudge.net/problem/CodeForces-449B
Graph and Graph Codeforces: https://codeforces.com/contest/2059/problem/D
Vjudge: https://vjudge.net/problem/CodeForces-2059D

Page 4 of 4

You might also like