Codeforces 95C-Volleyball

来源:互联网 发布:免费报表软件 .net 编辑:程序博客网 时间:2024/05/16 06:56

Volleyball
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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 uiviwi (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.

Output

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.

Examples
input
4 41 31 2 31 4 12 4 12 3 52 77 21 27 7
output
9
Note

An optimal way — ride from the junction 1 to 2 (via junction 4), then from 2 to 3. It costs 7+2=9 bourles.


题意:该市有n个路口,路口由双向道路连接。每条道路的长度是正整数,道路可以具有不同的长度。最初每个交叉点都有一个出租车站在那里。从第i个交叉点的出租车司机同意如果行驶距离不超过ti米,则带Petya(可能通过几个中间路口)到另一个交叉口,并且花费ci。出租车不能停在一条路的中间。每辆出租车只能使用一次。 Petya只能在路口处上车下车。目前Petya位于路口x,排球场位于路口y。问Petya需要乘车到体育场的最低金额。

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <algorithm>#include <queue>#include <stack>#include <vector>using namespace std;#define ll long longconst ll INF=0x3f3f3f3f3f3f3f3f;int n,m,x,y;ll edge[1005][1005],map[1005][1005];ll dis[1005],visit[1005];vector<int>G[1005];struct node{    ll t,c;} xx[1005];struct node1{    int id;    ll val;    friend bool operator<(node1 a,node1 b)    {        return a.val>b.val;    }} s,pre,next;void dijkstra(int o){    memset(visit,0,sizeof visit);    s.id=o;    s.val=0;    priority_queue<node1>q;    q.push(s);    memset(map[o],INF,sizeof map[o]);    map[o][o]=0;    while(!q.empty())    {        pre=q.top();        q.pop();        visit[pre.id]=1;        int Size=G[pre.id].size();        for(int i=0; i<Size; i++)        {            if(!visit[G[pre.id][i]]&&pre.val+edge[pre.id][G[pre.id][i]]<map[o][G[pre.id][i]])            {                map[o][G[pre.id][i]]=pre.val+edge[pre.id][G[pre.id][i]];                next.id=G[pre.id][i];                next.val=pre.val+edge[pre.id][G[pre.id][i]];                q.push(next);            }        }    }}void dijkstra1(int o){    memset(visit,0,sizeof visit);    int flag=0;    s.id=o;    s.val=0;    priority_queue<node1>q;    q.push(s);    while(!q.empty())    {        pre=q.top();        q.pop();        if(pre.id==y)        {            flag=1;            printf("%I64d\n",pre.val);            break;        }        visit[pre.id]=1;        for(int i=1; i<=n; i++)        {            if(!visit[i]&&map[pre.id][i]<=xx[pre.id].t)            {                next.id=i;                next.val=pre.val+xx[pre.id].c;                q.push(next);            }        }    }    if(!flag) printf("-1\n");}int main(){    while(~scanf("%d %d",&n,&m))    {        for(int i=0;i<1003;i++)            G[i].clear();        scanf("%d %d",&x,&y);        memset(edge,INF,sizeof edge);        ll u,v,w;        for(int i=0; i<m; i++)        {            scanf("%I64d %I64d %I64d",&u,&v,&w);            G[u].push_back(v);            G[v].push_back(u);            if(w<edge[u][v])                edge[u][v]=edge[v][u]=w;        }        for(int i=1; i<=n; i++)            dijkstra(i);        for(int i=1;i<=n;i++)            scanf("%I64d %I64d",&xx[i].t,&xx[i].c);        dijkstra1(x);    }    return 0;}

0 0