## 风一样的自由，色彩斑斓的雨季

If you do , you can.

# codeforces95C 最短路套最短路

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <iomanip>

using namespace std;
#define maxn 100050
#define MOD 1000000007
#define p 1000000007
#define mem(a , b) memset(a , b , sizeof(a))
#define LL long long
#define ULL unsigned long long
#define FOR(i , n) for(int i = 1 ;  i<= n ; i ++)
typedef pair<int , int> pii;
const long long INF = 0x3fffffff;
int n , m , st , ed;
int vis[1005];
LL cost[1005];
LL ans;

struct node
{
int v ;
LL w;
int flag;
node(){};
node(int _v , int _w ):v(_v),w(_w){};
bool friend operator<(node n1 , node n2)
{
return n1.w >= n2.w;
}
}cur , tmp;
vector<node>v[1005];
vector<node>e[1005];

void dfs(int rt , int s ,LL cost , LL now , LL dis)
{
//sort(v[s].begin() , v[s].end());
for(int i = 0 ; i < v[s].size() ; i ++)
{
node cur2 = v[s][i];
if(vis[cur2.v] > 5) continue;
if(cur2.w + now <= dis)
{
// cout << cur2.v << endl;
e[rt].push_back(node(cur2.v , cost));
vis[cur2.v] ++;
dfs(rt , cur2.v , cost , cur2.w + now , dis);
}
//else break;
}
}

void spfa2(int root)
{
for(int i = 0 ; i < 1005  ; i ++) cost[i] = INF*1000000;
cost[root] = 0;
queue<int>Q;
while(!Q.empty()) Q.pop();
Q.push(root);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
for(int i = 0 ; i < v[u].size() ; i ++)
{
node cur2 = v[u][i];
if(cost[cur2.v] > cost[u] + cur2.w)
{
cost[cur2.v] = cost[u] + cur2.w;
Q.push(cur2.v);
}
}
}

}

void spfa(int root)
{
for(int i = 0 ; i < 1005  ; i ++) cost[i] = INF*1000000;
cost[root] = 0;
queue<int>Q;
while(!Q.empty()) Q.pop();
Q.push(root);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
for(int i = 0 ; i < e[u].size() ; i ++)
{
node cur2 = e[u][i];
if(cost[cur2.v] > cost[u] + cur2.w)
{
cost[cur2.v] = cost[u] + cur2.w;
Q.push(cur2.v);
}
}
}

}

void BFS()
{
priority_queue<node>q;
while(!q.empty()) q.pop();
mem(vis , 0);
for(int i = 0 ; i < e[st].size() ; i ++) q.push(e[st][i]);
vis[st] = 1;
while(!q.empty())
{

cur = q.top();
q.pop();
if(ans >0 && cur.w > ans) break;
if(cur.v  == ed)
{
if(ans != -1)ans = min(cur.w , ans);
else ans = cur.w;
//break;
}
for(int i = 0 ; i < e[cur.v].size() ; i ++)
{
tmp = e[cur.v][i];
if(!vis[tmp.v])
{
q.push(node(tmp.v , cur.w + tmp.w));
vis[tmp.v] ++;
}
}
}
}

int main()
{
while(scanf("%d %d" , &n , &m) != EOF)
{
for(int i = 0 ; i < 1005 ;i ++)
{
v[i].clear();
e[i].clear();
}
scanf("%d %d" , &st , &ed);
int a , b ,c;
for(int i = 0 ; i < m ; i ++)
{
scanf("%d %d %d" , &a , &b , &c);
v[a].push_back(node(b ,c));
v[b].push_back(node(a ,c));
}
for(int i = 1 ; i <= n ; i ++)
{
scanf("%d %d" , &a , &b);
/*  mem(vis ,0);
vis[i] = 1;
e[i].push_back(node(i , 0));
dfs(i , i, b , 0 , a);*/
e[i].push_back(node(i , 0));
spfa2(i);
for(int j = 1 ; j <= n ; j ++)
{
if(cost[j] <= a)
{
e[i].push_back(node(j , b));
}
}
}
if(st == ed)
{
printf("0\n");
continue;
}
ans = -1;
//BFS();
spfa(st);
if(cost[ed] != INF*1000000) printf("%lld\n" , cost[ed]);
else printf("-1\n");

}
return 0;
}

03-08 49

03-05 437

04-03 59

06-26 947

12-08 571

02-23 497

08-30 393

10-17 592

01-21 241

05-08 85