-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbellman_ford.cpp
More file actions
78 lines (73 loc) · 1.34 KB
/
bellman_ford.cpp
File metadata and controls
78 lines (73 loc) · 1.34 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
#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;
struct edge {
int a, b, cost ;
};
int n, m, s, t ;
vector < edge > e (100005);
const int INF = 1000000000 ;
int bellman_ford()
{
vector < int > d ( n, INF ) ;
d[s-1] = 0 ;
for ( ;; ) {
bool any = false ;
for ( int j = 0 ; j <= m+1 ; ++j )
if ( d[e[j].a] < INF )
if ( d[e[j].b] > d[e[j].a] + e[j].cost ) {
d[e[j].b] = d[e[j].a] + e[j].cost ;
any = true ;
}
if ( ! any ) break ;
}
return d[t-1];
}
int main()
{
int test, final=INF;
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>>e[i].cost;
e[i].a=a-1;
e[i].b=b-1;
}
for (int i=0; i<k; ++i)
{
int a,b,c;
cin>>a>>b>>e[m].cost;
e[m].a=a-1;
e[m].b=b-1;
e[m+1].a=b-1;
e[m+1].b=a-1;
e[m+1].cost=e[m].cost;
int cur=bellman_ford();
if(final>cur)
final=cur;
}
if(final!=INF)
cout<<final<<endl;
else
cout<<"-1";
return 0;
}
}