-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdjikstra.cpp
More file actions
89 lines (85 loc) · 1.66 KB
/
djikstra.cpp
File metadata and controls
89 lines (85 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <cstdio>
#include <cctype>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <fstream>
#include <ctime>
#include <cassert>
#include <cstring>
using namespace std;
const int INF = 1000000000 ;
int n, m, s, t;
vector < vector < pair < int , int > > > g ( 10001 ) ;
vector < int > d ( 10001, INF ) , p ( 10001 ) ;
void djikstra()
{
vector < char > u(n) ;
d [ s ] = 0 ;
for ( int i = 0 ; i < n ; ++i ) {
int v = - 1 ;
for ( int j = 0 ; j < n ; ++j )
if ( !u [j] && (v == -1 || d[j] < d[v] ) )
v = j ;
if ( d[v] == INF )
break ;
u [ v ] = true ;
for ( int j = 0 ; j < g[v].size() ; ++j ) {
int to = g[v][j].first , len = g[v][j].second ;
if ( d[v] + len < d[to] ) {
d[to] = d[v] + len ;
p[to] = v ;
}
}
}
}
/*void path()
{
vector < int > path ;
for ( int v = t ; v != s ; v = p[v] )
path. push_back(v) ;
path. push_back(s) ;
reverse ( path.begin() , path.end() ) ;
for(int i=0; i<path.size(); ++i)
{
cout<<path[i]<<" ";
}
}*/
int main()
{
int test;
cin>>test;
while(test--){
int k;//two way edges
cin>>n>>m>>k>>s>>t;
for (int i=0; i<m; ++i)
{
int a,b,c;
cin>>a>>b>>c;
g[a].push_back(make_pair(b,c));
}
for (int i=0; i<k; ++i)
{
int a,b,c;
cin>>a>>b>>c;
g[a].push_back(make_pair(b,c));
g[b].push_back(make_pair(a,c));
}
djikstra();
if(d[t]<INF)
{
cout<<d[t]<<endl;
//path();
}
else
cout<<"-1"<<endl;
}
return 0;
}