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
An optimal way — ride from the junction 1 to 2 (via junction 4), then from 2 to 3. It costs 7+2=9 bourles.
#include <cstdio>
#include <queue>
using namespace std;
typedef long long LL;
const int M=1200000;
const LL inf=9187201950435737471LL;
int n,m,x,y;
struct ShortestWay{
int head[M],next[M],to[M],val[M],cnt;
queue<int> Q;
int vis[M];
LL f[M];
void init_edge(){
for (long i=1;i<=n;++i)
head[i]=-1;
cnt=0;
}
void add_edge(long u,long v,long c){
next[++cnt]=head[u]; head[u]=cnt; to[cnt]=v;
val[cnt]=c;
}
void spfa(long rt){
for (long i=1;i<=n;++i){
vis[i]=0; f[i]=inf;
}
Q.push(rt); vis[rt]=1; f[rt]=0;
while (!Q.empty()){
long u=Q.front(); Q.pop();
for (long i=head[u];i!=-1;i=next[i]){
long v=to[i];
if (f[u]+val[i]<f[v]){
f[v]=f[u]+val[i];
if (!vis[v]){ vis[v]=1; Q.push(v);}
}
}
vis[u]=0;
}
}
};
ShortestWay G1,G2;
int main(){
scanf("%d%d%d%d",&n,&m,&x,&y);
G1.init_edge(); G2.init_edge();
for (long i=1;i<=m;++i){
long u,v,c;
scanf("%d%d%d",&u,&v,&c);
G1.add_edge(u,v,c); G1.add_edge(v,u,c);
}
for (long i=1;i<=n;++i){
long t,c;
scanf("%d%d",&t,&c);
G1.spfa(i);
for (long j=1;j<=n;++j)
if (G1.f[j]<=t) G2.add_edge(i,j,c);
}
G2.spfa(x);
if (G2.f[y]==inf) printf("%d\n",-1);
else printf("%I64d\n",G2.f[y]);
return 0;
}