选择最佳路线(单源最短路扩展应用)

有一天,琪琪想乘坐公交车去拜访她的一位朋友。

由于琪琪非常容易晕车,所以她想尽快到达朋友家。

现在给定你一张城市交通路线图,上面包含城市的公交站台以及公交线路的具体分布。

已知城市中共包含 n 个车站(编号1~n)以及 m 条公交线路。

每条公交线路都是 单向的,从一个车站出发直接到达另一个车站,两个车站之间可能存在多条公交线路。

琪琪的朋友住在 s 号车站附近。

琪琪可以在任何车站选择换乘其它公共汽车。

请找出琪琪到达她的朋友家(附近的公交车站)需要花费的最少时间。

输入格式
输入包含多组测试数据。

每组测试数据第一行包含三个整数 n,m,s,分别表示车站数量,公交线路数量以及朋友家附近车站的编号。

接下来 m 行,每行包含三个整数 p,q,t,表示存在一条线路从车站 p 到达车站 q,用时为 t。

接下来一行,包含一个整数 w,表示琪琪家附近共有 w 个车站,她可以在这 w 个车站中选择一个车站作为始发站。

再一行,包含 w 个整数,表示琪琪家附近的 w 个车站的编号。

输出格式
每个测试数据输出一个整数作为结果,表示所需花费的最少时间。

如果无法达到朋友家的车站,则输出 -1。

每个结果占一行。

数据范围
n≤1000,m≤20000,
1≤s≤n,
0<w<n,
0<t≤1000
输入样例:

5 8 5
1 2 2
1 5 3
1 3 4
2 4 7
2 5 6
2 3 5
3 5 1
4 5 1
2
2 3
4 3 4
1 2 3
1 3 4
2 3 2
1
1


输出样例:

1
-1

题目意思是有多个起点可以选择,求到终点的最短距离,暴力每个起点到终点的最短距离再取最小值显然会超时。

但是我们反过来从终点求到各个起点的最短距离这只需要跑一次最短路,这是一种可行的方法。代码如下:

#include<bits/stdc++.h>
using namespace std; using ll = long long;
int dir[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
//using lll = __int128; template <class T> istream& read(T& x, istream& cin = std::cin) { T num = 0; bool f = 0; char ch = 0; while (!isdigit(ch)) { f |= ch == '-'; if (!cin.get(ch)) return cin; }while (isdigit(ch)) { num = (num << 3) + (num << 1) + (ch ^ 48); if (!cin.get(ch)) break; }x = f ? -num : num; return cin; }template <class T> ostream& write(T x, ostream& cout = std::cout) { if (x < 0) cout.put('-'), x = -x; if (x > 9) write(x / 10); cout.put(x % 10 + '0'); return cout; }ostream& operator<<(ostream& cout, lll x) { write(x); return cout; }istream& operator>>(istream& cin, lll& x) { return read(x); }bool check(int i, int j);
bool check(int i, int j);

const int N = 1e5 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f;

int n, m, S, mw, h[N], w[N], e[N], ne[N], idx, dist[N];

vector<int> tar;

inline void add(int a, int b, int c) {
	e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}

void init() {
	memset(h, -1, sizeof h);
	tar.clear();
	cin >> m >> S;
	while (m--) {
		int a, b, c;
		cin >> a >> b >> c;
		add(b, a, c);
	}
	cin >> mw;
	while (mw--) {
		int x;
		cin >> x;
		tar.emplace_back(x);
	}
	//cout << "init successful" << endl;
	return;
}

bool st[N];

int spfa() {
	queue<int> qu;
	qu.emplace(S);
	memset(dist, 0x3f, sizeof dist);
	dist[S] = 0;
	while (qu.size()) {
		int x = qu.front();
		qu.pop();
		st[x] = false;
		for (int i = h[x]; ~i; i = ne[i]) {
			int nx = e[i], td = w[i];
			if (dist[nx] > dist[x] + td) {
				dist[nx] = dist[x] + td;
				if (st[nx]) continue;
				st[nx] = true;
				qu.emplace(nx);
			}
		}
	}
	int res = INF;
	for (auto& ed : tar) {
		res = min(res, dist[ed]);
	}
	return res > INF / 2 ? -1 : res;
}

void solve() {
	cout << spfa();
	//cout << "all successful" << endl;
	return;
}

int main(void) {
	ios::sync_with_stdio(0); cin.tie(0); cout << setprecision(6) << fixed;
	tar.reserve(N);
	int TT = 1;
	//cin >> TT;
	for (int ii = 1; cin >> n; init(), solve(), ii++, cout << "\n") {}
	return 0;
}

其次我们可以创建一个到各个起点距离都是 0 的点,再以该点作为起点跑最短路也是一种可行的方案。代码如下:

#include<bits/stdc++.h>
using namespace std; using ll = long long;
int dir[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
//using lll = __int128; template <class T> istream& read(T& x, istream& cin = std::cin) { T num = 0; bool f = 0; char ch = 0; while (!isdigit(ch)) { f |= ch == '-'; if (!cin.get(ch)) return cin; }while (isdigit(ch)) { num = (num << 3) + (num << 1) + (ch ^ 48); if (!cin.get(ch)) break; }x = f ? -num : num; return cin; }template <class T> ostream& write(T x, ostream& cout = std::cout) { if (x < 0) cout.put('-'), x = -x; if (x > 9) write(x / 10); cout.put(x % 10 + '0'); return cout; }ostream& operator<<(ostream& cout, lll x) { write(x); return cout; }istream& operator>>(istream& cin, lll& x) { return read(x); }bool check(int i, int j);
bool check(int i, int j);

const int N = 1e5 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f;

int n, m, S, mw, h[N], w[N], e[N], ne[N], idx, dist[N];

inline void add(int a, int b, int c) {
	e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}

void init() {
	memset(h, -1, sizeof h);
	cin >> m >> S;
	while (m--) {
		int a, b, c;
		cin >> a >> b >> c;
		add(a, b, c);
	}
	cin >> mw;
	while (mw--) {
		int x;
		cin >> x;
		add(0, x, 0);
	}
	//cout << "init successful" << endl;
	return;
}

bool st[N];

int spfa() {
	queue<int> qu;
	qu.emplace(0);
	memset(dist, 0x3f, sizeof dist);
	dist[0] = 0;
	while (qu.size()) {
		int x = qu.front();
		qu.pop();
		st[x] = false;
		for (int i = h[x]; ~i; i = ne[i]) {
			int nx = e[i], td = w[i];
			if (dist[nx] > dist[x] + td) {
				dist[nx] = dist[x] + td;
				if (st[nx]) continue;
				st[nx] = true;
				qu.emplace(nx);
			}
		}
	}
	return dist[S] > INF / 2 ? -1 : dist[S];
}

void solve() {
	cout << spfa();
	//cout << "all successful" << endl;
	return;
}

int main(void) {
	ios::sync_with_stdio(0); cin.tie(0); cout << setprecision(6) << fixed;
	int TT = 1;
	//cin >> TT;
	for (int ii = 1; cin >> n; init(), solve(), ii++, cout << "\n") {}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值