Codeforces Beta Round #77 (Div. 1 Only) C. Volleyball (two-dimensional shortest path & greedy)

C. 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 t i meters. Also, the cost of the ride doesn't depend on the distance and is equal to c i 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 u iv iw i (1 ≤ u i, v i ≤ n, 1 ≤ w i ≤ 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 t i and c i (1 ≤ t i, c i ≤ 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

Copy

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

Copy

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.

Main idea:

There is a taxi at each point of N points. You have to travel from S to T. Each taxi can travel up to X distances and cost Y (as long as you get on the car and stop at any time). Click to park, but you can't stop at the side, ask for the minimum cost.

solution:

For each point, he must either get out of the car now to find a new car, or continue walking with the previous car, and when running the shortest path, he can add both of these states to the queue. At the same time, the marked vis is not one-dimensional, because the current minimum cost does not mean the global minimum cost. The point is only 1e3. One more dimension is used to record the starting point of the car. This is not the end. If there are two at a certain point The same minimum cost, but different distances. Obviously, priority is given to running smaller distances . Reload the priority and this problem will be solved!

Accepted code

#pragma GCC optimize(3)
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <ll, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define MPY(x, b) memcpy(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int Mod = 1e9 + 7;
const int N = 1e3 + 100;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
inline ll dpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % Mod; b >>= 1; t = (t*t) % Mod; }return r; }
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t); b >>= 1; t = (t*t); }return r; }

struct Edge
{
	int u, prv;  // 当前点,出发点
	ll w, t;     // 距离,花费
	bool operator < (const Edge &oth) const {
		if (t == oth.t)
			return w > oth.w;   // 优先时间,其次距离
		else
			return t > oth.t;
	}
};
vector <pir> G[N];
ll d[N], c[N];
int n, m, sp, tp;
bool vis[N][N];   // 当前点,起始点

ll Dijkstra() {
	if (sp == tp)
		return 0;
	priority_queue <Edge> q;
	q.push({ sp, sp, 0, c[sp] });

	while (!q.empty()) {
		Edge now = q.top();
		q.pop();
		int u = now.u;
		int prv = now.prv;
		if (u == tp)
			return now.t;
		if (vis[u][prv])
			continue;
		vis[u][prv] = true; 

		q.push({ u, u, 0, now.t + c[u] });  // 直接下车
		for (auto it : G[u]) {
			int v = it.second; ll w = it.first;
			if (now.w + w <= d[prv])    // 如果可以继续走
				q.push({ v, prv, now.w + w, now.t });
		}
	}
	return -1;
}

int main()
{
	cin >> n >> m >> sp >> tp;
	for (int i = 0; i < m; i++) {
		int u, v; ll w;
		sc("%d %d %lld", &u, &v, &w);
		G[u].push_back({ w, v });
		G[v].push_back({ w, u });
	}
	for (int i = 1; i <= n; i++)
		sc("%lld %lld", &d[i], &c[i]);

	printf("%lld\n", Dijkstra());
	return 0;  // 改数组大小!!!用pair记得改宏定义!!!
}

 

Guess you like

Origin blog.csdn.net/weixin_43851525/article/details/108102371
Recommended