2019.西安邀请赛 M.Travel(二分+bfs)

博客围绕M.Travel星际航行问题展开,MOT星系有n个行星,通过m条传输通道相连。居民乘飞船航行,飞船可升级,每次升级花费c,增加传输距离d和传输通道数e。Alice从行星1到行星n,求最小成本,还给出了输入输出格式及样例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

M.Travel

There are nn planets in the MOT galaxy, and each planet has a unique number from 1 \sim n1∼n. Each planet is connected to other planets through some transmission channels. There are mm transmission channels in the galaxy. Each transmission channel connects two different planets, and each transmission channel has a length.

The residents of the galaxy complete the interplanetary voyage by spaceship. Each spaceship has a level. The spacecraft can be upgraded several times. It can only be upgraded 11 level each time, and the cost is cc. Each upgrade will increase the transmission distance by dd and the number of transmissions channels by ee to the spacecraft. The spacecraft can only pass through channels that are shorter than or equal to its transmission distance. If the number of transmissions is exhausted, the spacecraft can no longer be used.

Alice initially has a 00-level spacecraft with transmission distance of 00 and transmission number of 00. Alice wants to know how much it costs at least, in order to transfer from planet 11 to planet nn.

Input

Each test file contains a single test case. In each test file:
The first line contains nn, mm, indicating the number of plants and the number of transmission channels
The second line contains cc, dd, ee, representing the cost, the increased transmission distance, and the increased number of transmissions channels of each upgrade, respectively.
Next mm lines, each line contains u,v,wu,v,w, meaning that there is a transmission channel between uu and vv with a length of ww.

(The graph has no self-loop , no repeated edges , and is connected)

Output
Output a line for the minimum cost. Output -1−1 if she can’t reach.

样例输入
5 7
1 1 1
1 2 1
1 3 5
1 4 1
2 3 2
2 4 5
3 4 3
3 5 5
样例输出
5

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
int n, m, c, d, e;
struct node{
	int to;
	int w;
};
struct Node{
	int x;
	int maxs;//最大长度
	int s;//走几次
};
vector<node>vec[maxn];
int vis[maxn];
int dis[maxn];
bool bfs(int x,int s){
	memset(dis, 0x3f3f3f3f, sizeof dis);
	memset(vis, 0, sizeof vis);
	Node now;
	queue<Node>q;
	now.x = 1, now.s = 0, now.maxs = 0;
	dis[1] = 0;
	q.push(now);
	while (!q.empty()){
		now = q.front();
		q.pop();
		if (now.x == n)
			return 1;
		if (vis[now.x]) continue;
		vis[now.x] = 1;
		for (int i = 0; i < vec[now.x].size(); i++){
			if (dis[now.x] + vec[now.x][i].w < dis[vec[now.x][i].to] && now.s < s&&vec[now.x][i].w <= x){
				dis[vec[now.x][i].to] = dis[now.x] + vec[now.x][i].w;
				q.push({ vec[now.x][i].to, max(now.maxs, vec[now.x][i].w), now.s + 1 });
			}
		}
	}
	return 0;
}
int main(){
	cin >> n >> m;
	cin >> c >> d >> e;
	for (int i = 1; i <= m; i++){
		int v, u, w;
		cin >> u >> v >> w;
		vec[u].push_back({ v, w });
		vec[v].push_back({ u, w });
	}
	int L = 0, R = 10000;
	int ans = -1;
	while (L <= R){
		int mid = (L + R) / 2;
		if (bfs(mid*d, mid*e)) ans = mid, R = mid - 1;
	 	else L = mid + 1;
	}
	if (ans == -1) cout << -1 << endl;
	else cout << ans*c << endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值