CodeForces - 96D Volleyball 最短路

D. 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 4
1 3
1 2 3
1 4 1
2 4 1
2 3 5
2 7
7 2
1 2
7 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个地方,m条路,一个人要从x到y,他要打车走,每条路上都有出租车,出租车走的距离为t,收c元,求从x到y最小花费
样例:
4 4  //4个地方4条路
1 3 //从1到3
1 2 3//从1到2路的距离为3  以下一次类推
1 4 1
2 4 1
2 3 5 
2 7//第1条路上的出租走的距离是2 花费7元
7 2//第2条路上的出租走的距离是7 花费2元 以下一次类推
1 2
7 7
两次最短路,先求出到达目的地的最短距离,然后根据最短距离求出最小花费
#include<bits/stdc++.h>
#define maxn 1010
#define ll long long
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
struct edge
{
    int to;
    int cost;
};
typedef pair<ll,int> P;
vector<edge> V[maxn];
int a[maxn],b[maxn];
int n,m;
ll dis[1010][1010];
ll cost[maxn];
void dijkstra(int s)//求最短路
{
    priority_queue<P,vector<P>,greater<P> >que;
    dis[s][s]=0;
    que.push(P(dis[s][s],s));
    while(!que.empty())
    {
        P p=que.top();
        que.pop();
        int v=p.second;
        if(dis[s][v]<p.first)
            continue;
        for(int i=0;i<V[v].size();i++)
        {
            edge e=V[v][i];
            if(dis[s][e.to]>dis[s][v]+e.cost)
            {
                dis[s][e.to]=dis[s][v]+e.cost;
                que.push(P(dis[s][e.to],e.to));
            }
        }
    }
}
void pay(int s)//求最小花费
{
    priority_queue<P,vector<P>,greater<P> >que;
    fill(cost,cost+maxn,INF);
    cost[s]=0;
    que.push(P(cost[s],s));
    while(!que.empty())
    {
        P p=que.top();
        que.pop();
        int v=p.second;
        if(cost[v]<p.first)
            continue;
        for(int i=1;i<=n;i++)
        {
            if(dis[v][i]<=a[v]&&cost[i]>cost[v]+b[v])//如果到达i的距离小于等于在v的出租车走的距离且花费大于坐这辆车的花费
            {
                cost[i]=cost[v]+b[v];
                que.push(P(cost[i],i));
            }
        }
    }
}
int main()
{
    int s,e;
    scanf("%d%d",&n,&m);
    scanf("%d%d",&s,&e);
    for(int i=0;i<m;i++)
    {
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        edge e;
        e.to=v;
        e.cost=w;
        V[u].push_back(e);
        e.to=u;
        V[v].push_back(e);
    }
    for(int i=1;i<=n;i++)
        scanf("%d%d",&a[i],&b[i]);
    for(int i=0;i<maxn;i++)
        for(int j=0;j<maxn;j++)
            dis[i][j]=INF;
    for(int i=1;i<=n;i++)
    {
        dijkstra(i);
    }
    pay(s);
    ll ans=cost[e];
    if(ans==INF)
        printf("-1\n");
    else
        printf("%lld\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值