Codeforces Beta Round #77 (Div. 1 Only) C. Volleyball (二维最短路&贪心)

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.

题目大意:

N个点每个点上都有一辆出租车,你要从S到达T,每辆出租车可以最多行驶X个距离,花费Y(只要上了车,不管什么时候停下),其中只可以在点停车,边不可停,求最小花费。

解法:

对于每个点来说,他要么现在下车找新车,要么接着上一辆车继续走,跑最短路时,可以将这两种状态都加入队列。同时标记的vis就不是一维的了,因为当前最小花费不代表全局最小花费,点只有1e3,多开一维记录这辆车的起始点,这样还不算完,如果某个点有两个相同的最小花费,但是距离不同,很显然优先跑距离小的,重载一下优先级,这道题就解决了!

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记得改宏定义!!!
}

猜你喜欢

转载自blog.csdn.net/weixin_43851525/article/details/108102371