Algorithm


Problem link- https://www.spoj.com/problems/TRAFFICN/

TRAFFICN - Traffic Network

 

The city traffic network consists of n nodes numbered from 1 to n and m one-way roads connecting pairs of nodes. In order to reduce the length of the shortest path between two different critical nodes s and t, a list of k two-way roads are proposed as candidates to be constructed. Your task is to write a program to choose one two-way road from the proposed list in order to minimize the resulting shortest path between s and t.

Input

The input file consists of several data sets. The first line of the input file contains the number of data sets which is a positive integer and is not bigger than 20. The following lines describe the data sets.

For each data set, the first line contains five positive integers n (n ≤ 10 000), m (m ≤ 100 000), k (k < 300), s (1 ≤ s ≤ n), t (1 ≤ t ≤ n) separated by space. The ith line of the following m lines contains three integers di, ci, li separated by space, representing the length li ( 0< li ≤ 1000) of the ith one-way road connecting node di to ci. The jth line of the next k lines contains three positive integers uj, vj and qj (qj ≤ 1000) separated by space, representing the jth proposed two-way road of length qj connecting node uj to vj.

Output

For each data set, write on one line the smallest possible length of the shortest path after building the chosen one two-way road from the proposed list. In case, there does not exist a path from s to t, write -1.

Example

Sample Input
1
4 5 3 1 4
1 2 13
2 3 19
3 1 25
3 4 17
4 1 18
1 3 23
2 3 5
2 4 25	

Sample Output
35

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <sstream>
#include <fstream>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>

using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
#define se second
#define fi first
#define pb push_back
int n,m,k,s,t;

vector<vii> dist;
vector<vii> rdist;

const int INF = 0x3f3f3f3f;

vi dists(10002,INF); //distance from source s to node
vi distt(10002,INF); //distance from node to target

void dijkstras(int source, vector<vii> &v,vector<int> &distance){
	priority_queue<ii,vii,greater<ii> > pq;
	pq.push(ii(0,source));
	distance[source]=0;
	while(!pq.empty()){
		ii top = pq.top();
		pq.pop();
		int node = top.se;
		int d = top.fi;
		for(int i=0;i<v[node].size();++i){
			ii neighbor = v[node][i];
			if(distance[neighbor.fi]>d+neighbor.se){
				distance[neighbor.fi]=d+neighbor.se;
				pq.push(ii(distance[neighbor.fi],neighbor.fi));
			}
		}
	}
}

int main(){
	int cases;
	scanf("%d",&cases);
	while(cases--){
		dists = vi(10002,INF);
		distt = vi(10002, INF);
		scanf("%d %d %d %d %d",&n,&m,&k,&s,&t);
		dist = vector<vii> (n+1,vector<ii>());
		rdist = vector<vii> (n+1,vector<ii>());
		int u,v,d;
		for(int i=0;i<m;++i){
			scanf("%d %d %d",&u,&v,&d);
			dist[u].pb(ii(v,d));
			rdist[v].pb(ii(u,d));
		}
		dijkstras(s,dist,dists);
		dijkstras(t,rdist,distt);
		int ans = dists[t];
		for(int i=0;i<k;++i){
			scanf("%d %d %d",&u,&v,&d);
			ans=min(ans,min(dists[u]+d+distt[v],dists[v]+d+distt[u]));
		}
		if(ans==INF)
			ans=-1;
		printf("%d\n",ans);
	}
	return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
1
4 5 3 1 4
1 2 13
2 3 19
3 1 25
3 4 17
4 1 18
1 3 23
2 3 5
2 4 25

Output

x
+
cmd
35
Advertisements

Demonstration


SPOJ Solution-TRAFFICN Traffic Network-Solution in C, C++, Java, Python

Previous
SPOJ Solution - Test Life, the Universe, and Everything - Solution in C, C++, Java, Python