Volleyball

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jnxxhzz/article/details/80977335

2181: Volleyball

Time Limit: 2 Sec   Memory Limit: 256 MB
Submit: 2   Solved: 1
[ Submit][ Status][ Web Board]

Description

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,yn). 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,vin,1≤wi≤109) − they are the numbers of the junctions connected by the road and the length of the road, correspondingly. The next nlines 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 C++. 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条路,起点和终点,第i个城市的出租车最多可以开几公里,开车需要多少钱

问起点到终点最便宜多少钱...两遍最短路,第一遍最短路求城市之间的最短路,第二遍求一下钱...

【代码】

#include<bits/stdc++.h>
using namespace std;
struct edge{
    int v2,w;
    edge(int v,int wei){
        v2=v;
        w=wei;
    }
};
vector<edge> adj1[1005],adj2[1005];
long long dis[1005][1005],dis2[1005];
int t[1005],c[1005];
void dijkstra1(int s){
    dis[s][s]=0;
    priority_queue<pair<long long,int> > pq;
    pq.push(make_pair(-dis[s][s],s));
    while(!pq.empty()){
        int v=pq.top().second;
        pq.pop();
        if(dis[s][v]>t[s])
            return;
        adj2[s].push_back(edge(v,c[s]));
        vector<edge> ::iterator itv;
        for(itv=adj1[v].begin();itv!=adj1[v].end();itv++){
            if(dis[s][itv->v2]==-1||dis[s][itv->v2]>dis[s][v]+itv->w){
                dis[s][itv->v2]=dis[s][v]+itv->w;
                pq.push(make_pair(-dis[s][itv->v2],itv->v2));
            }
        }
    }
}
void dijkstra2(int s,int y){
    dis2[s]=0;
    priority_queue<pair<long long,int> > pq;
    pq.push(make_pair(-dis2[s],s));
    while(!pq.empty()){
        int v=pq.top().second;
        pq.pop();
        if(v==y)
            return;
        vector<edge> ::iterator itv;
        for(itv=adj2[v].begin();itv!=adj2[v].end();itv++){
            if(dis2[itv->v2]==-1||dis2[itv->v2]>dis2[v]+itv->w){
                dis2[itv->v2]=dis2[v]+itv->w;
                pq.push(make_pair(-dis2[itv->v2],itv->v2));
            }
        }

    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m,x,y,i;
    cin>>n>>m>>x>>y;
    memset(dis,-1,sizeof(dis));
    memset(dis2,-1,sizeof(dis2));
    for(i=0;i<m;i++){
        int a,b,r;
        cin>>a>>b>>r;
        adj1[a-1].push_back(edge(b-1,r));
        adj1[b-1].push_back(edge(a-1,r));
    }
    for(i=0;i<n;i++){
        cin>>t[i]>>c[i];
    }
    for(i=0;i<n;i++)
        dijkstra1(i);
    dijkstra2(x-1,y-1);
    cout<<dis2[y-1];
}


猜你喜欢

转载自blog.csdn.net/jnxxhzz/article/details/80977335