Petya loves volleyball very much. One day he was running late for a volleyball match. Petya hasn't bought his own car yet, that's why he had to take a taxi. The city has n junctions, some of which are connected by two-way roads. The length of each road is defined by some positive integer number of meters; the roads can have different lengths.
Initially each junction has exactly one taxi standing there. The taxi driver from the i-th junction agrees to drive Petya (perhaps through several intermediate junctions) to some other junction if the travel distance is not more than ti meters. Also, the cost of the ride doesn't depend on the distance and is equal to ci bourles. Taxis can't stop in the middle of a road. Each taxi can be used no more than once. Petya can catch taxi only in the junction, where it stands initially.
At the moment Petya is located on the junction x and the volleyball stadium is on the junction y. Determine the minimum amount of money Petya will need to drive to the stadium.
The first line contains two integers n and m (1 ≤ n ≤ 1000, 0 ≤ m ≤ 1000). They are the number of junctions and roads in the city correspondingly. The junctions are numbered from 1 to n, inclusive. The next line contains two integers x and y (1 ≤ x, y ≤ n). They are the numbers of the initial and final junctions correspondingly. Next m lines contain the roads' description. Each road is described by a group of three integers ui, vi, wi (1 ≤ ui, vi ≤ n, 1 ≤ wi ≤ 109) — they are the numbers of the junctions connected by the road and the length of the road, correspondingly. The next n lines contain n pairs of integers ti and ci (1 ≤ ti, ci ≤ 109), which describe the taxi driver that waits at the i-th junction — the maximum distance he can drive and the drive's cost. The road can't connect the junction with itself, but between a pair of junctions there can be more than one road. All consecutive numbers in each line are separated by exactly one space character.
If taxis can't drive Petya to the destination point, print "-1" (without the quotes). Otherwise, print the drive's minimum cost.
Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator.
4 4
1 3
1 2 3
1 4 1
2 4 1
2 3 5
2 7
7 2
1 2
7 7
9
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<math.h>
#include<string.h>
typedef long long ll;
using namespace std;
const int mod = 10;
const int N = 1010;
const long long INF = 1e18; //注意初始化的INF要很大
typedef pair<long long, int> par; //注意距离开long long
int n, m, x, y;
int cnt, cnt2, head[N], head2[N];
long long d[N][N], D[N];
struct edge
{
int end;
ll len;
int next;
}e[N*2] ,e2[N*N];
void add_edge(int u, int v, int w)
{
e[cnt].end = v;
e[cnt].len = w;
e[cnt].next = head[u];
head[u] = cnt ++;
}
void Add_edge(int u, int v, int w)
{
e2[cnt2].end = v;
e2[cnt2].len = w;
e2[cnt2].next = head2[u];
head2[u] = cnt2 ++;
}
void dijstra(int k) //求k点到其他所有点的最短路
{
for(int i = 1; i <= n; i ++) {
d[k][i] = INF;
}
d[k][k] = 0;
priority_queue< pair<long long, int> > q;
q.push(make_pair(0, k));
while(!q.empty()) {
par cur;
cur = q.top();
q.pop();
long long a = -cur.first;
int b = cur.second;
for(int i = head[b]; i != -1; i = e[i].next) {
int en = e[i].end;
long long len = a + e[i].len;
if(d[k][en] > len) {
d[k][en] = len;
q.push(make_pair(-len, en));
}
}
}
}
void Dijstra(int k) //最后所建图的最短路
{
for(int i = 1; i <= n; i ++) {
D[i] = INF;
}
D[k] = 0;
priority_queue< pair<long long, int> > q;
q.push(make_pair(0, k));
while(!q.empty()) {
par cur;
cur = q.top();
q.pop();
long long a = -cur.first;
int b = cur.second;
for(int i = head2[b]; i != -1; i = e2[i].next) {
int en = e2[i].end;
long long len = a + e2[i].len;
if(D[en] > len) {
D[en] = len;
q.push(make_pair(-len, en));
}
}
}
}
int main()
{
int a, b ,c;
while(~scanf("%d%d", &n, &m)) {
memset(head, -1, sizeof(head));
memset(head2, -1, sizeof(head2));
cnt = cnt2 = 0;
scanf("%d%d", &x, &y);
for(int i = 0; i < m; i ++) {
scanf("%d%d%d", &a, &b, &c);
add_edge(a, b, c);
add_edge(b, a, c);
}
for(int i = 1; i <= n; i ++) { //求每个点的最短路,复杂度为O(n*n*logn)
dijstra(i);
}
for(int i = 1; i <= n; i ++) { //建坐车图
scanf("%d%d", &a, &b);
for(int j = 1; j <= n; j ++) {
if(d[i][j] <= a && i != j) { //满足条件,点之间距离小于车行驶距离
Add_edge(i, j, b);
}
}
}
Dijstra(x);
if(D[y] == INF) printf("-1\n");
else printf("%lld\n", D[y]);
}
return 0;
}