【Codeforces】CF 2014E

Rendez-vous de Marian et Robin

#图论 #最短路 #分层图

题目描述

In the humble act of meeting, joy doth unfold like a flower in bloom.

Absence makes the heart grow fonder. Marian sold her last ware at the Market at the same time Robin finished training at the Major Oak. They couldn’t wait to meet, so they both start without delay.

The travel network is represented as n n n vertices numbered from 1 1 1 to n n n and m m m edges. The i i i-th edge connects vertices u i u_i ui and v i v_i vi, and takes w i w_i wi seconds to travel (all w i w_i wi are even). Marian starts at vertex 1 1 1 (Market) and Robin starts at vertex n n n (Major Oak).

In addition, h h h of the n n n vertices each has a single horse available. Both Marian and Robin are capable riders, and could mount horses in no time (i.e. in 0 0 0 seconds). Travel times are halved when riding. Once mounted, a horse lasts the remainder of the travel. Meeting must take place on a vertex (i.e. not on an edge). Either could choose to wait on any vertex.

Output the earliest time Robin and Marian can meet. If vertices 1 1 1 and n n n are disconnected, output − 1 -1 1 as the meeting is cancelled.

输入格式

The first line of the input contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1\leq t \leq 10^4 1t104) — the number of test cases.

The first line of each test case consists of three integers n n n, m m m, h h h ( 2 ≤ n ≤ 2 ⋅ 1 0 5 ,    1 ≤ m ≤ 2 ⋅ 1 0 5 ,    1 ≤ h ≤ n 2 \le n \le 2 \cdot 10^5, \;1 \le m \le 2 \cdot 10^5, \; 1 \le h \le n 2n2105,1m2105,1hn) — the number of vertices, the number of edges and the number of vertices that have a single horse.

The second line of each test case contains h h h distinct integers a 1 , a 2 , … , a h a_1, a_2, \ldots, a_h a1,a2,,ah ( 1 ≤ a i ≤ n 1 \le a_i \le n 1ain) — the vertices that have a single horse available.

Then follow m m m lines of each test case, each with three integers u i u_i ui, v i v_i vi, w i w_i wi ( 1 ≤ u i , v i ≤ n ,    2 ≤ w i ≤ 1 0 6 1\le u_i,v_i \le n, \; 2\le w_i \le 10^6 1ui,vin,2wi106) — meaning that there is an edge between vertices u i u_i ui and v i v_i vi with travel cost w i w_i wi seconds without a horse.

There are no self loops or multiple edges. By good fortune, all w i w_i wi are even integers.

It is guaranteed that the sums of both n n n and m m m over all test cases do not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

输出格式

For each test case, output a single integer, the earliest time Robin and Marian can meet. If it is impossible for them to meet, output − 1 -1 1.

样例 #1

样例输入 #1

6
2 1 1
1
1 2 10
3 1 2
2 3
1 2 10
3 3 1
2
1 2 4
1 3 10
2 3 6
4 3 2
2 3
1 2 10
2 3 18
3 4 16
3 2 1
2
1 2 4
1 3 16
7 7 1
3
1 5 2
2 6 12
1 2 12
6 4 8
7 3 4
6 3 4
7 6 4

样例输出 #1

5
-1
6
19
14
12

解题思路

算是比较经典的分层图了:

对于一些特定的节点,我们可以骑上马,然后速度会增快。

因此我们建两层图,第一层图就是普通的图,有普通的速度,然后在有马的节点连接上另一层的图,这样我们跑出来的最短路就是这两层的图的最短路了。

我们只需要在 1 1 1号点跑一次分层 d i j k s t r e a dijkstrea dijkstrea,然后再在 n n n号点跑一次分层 d i j k s t r e a dijkstrea dijkstrea,最后枚举每一个点作为见面的点,取最小最为答案即可。

代码

const int N = 1e6 + 7;
struct edge {
	int v, dis, t; //终点,边权
	bool operator<(const edge& x) const { //重构<符号
		return x.dis < dis;
	}
};
void solve() {
	int n, m, k;
	cin >> n >> m >> k;
	vector<vector<edge>>e(n + 1);
	vector<int>h(n + 1, 2);
	for (int i = 1; i <= k; ++i) {
		int x;
		cin >> x;
		h[x] = 1;
	}
 
	for (int i = 1; i <= m; ++i) {
		int u, v, w;
		cin >> u >> v >> w;
		e[u].push_back({ v,w / 2 });
		e[v].push_back({ u,w / 2 });
	}
 
	vector<vector<int>>dis(n + 1, vector<int>(3)), vis(n + 1, vector<int>(3));
	auto dijkstra = [&](int s) {
		priority_queue<edge>pq; //利用到STL优先队列
		for (int i = 1; i <= n; i++) {
			dis[i][2] = dis[i][1] = inf;
			vis[i][2] = vis[i][1] = 0;
		}
		pq.push({ s,0,h[s] }); dis[s][h[s]] = 0; //初始化
 
		while (!pq.empty()) {
			int u = pq.top().v;
			int t = pq.top().t;
			int dd = pq.top().dis;
			pq.pop();//取出队头
			if (vis[u][t]) continue;
			vis[u][t] = 1;
			for (auto& x : e[u]) { //遍历图
				int v = x.v;
				int w = x.dis;
				if (dd + w * t < dis[v][t]) { //更新距离
					dis[v][t] = dd + w * t;
					pq.push({ v,dis[v][t],h[v] });
					pq.push({ v,dis[v][t],t });
				}
			}
		}
		};
 
	dijkstra(1);
 
	if (dis[n][1] == inf && dis[n][2] == inf) {
		std::cout << -1 << "\n";
		return;
	}
 
	auto dis1(dis);
 
	dijkstra(n);
 
	auto dis2(dis);
 
	int res = inf;
	for (int i = 1; i <= n; ++i) {
		int d1 = std::min(dis1[i][2], dis1[i][1]);
		int d2 = std::min(dis2[i][2], dis2[i][1]);
 
		res = std::min(std::max(d1, d2), res);
	}
 
	std::cout << res << "\n";
 
}
 
signed main() {
	ios::sync_with_stdio(0);
	std::cin.tie(0);
	std::cout.tie(0);
	int t = 1;
	cin >> t;
	while (t--) {
		solve();
	}
};```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值