1111. Online Map (30)-PAT甲级真题 (dij + dfs)

文章介绍了如何在一个带有两个权值(长度和时间)的图中找到起点到终点的最短长度和最短时间路径。使用Dijkstra算法和邻接矩阵/邻接表来构建和解决该问题,同时考虑了路径不唯一时的特殊处理策略。作者通过不断优化算法,如堆优化和DFS,来提高时间和空间效率。
摘要由CSDN通过智能技术生成

题意

给一个图,其边表有两个权值,长度len、时间tim,找到起点sv到终点ev最短长度的路径和最短时间的路径。如果两个路径相同,则合并输出。以下英语问题需要重视,没看懂下面这些条件,凭着运气花了快1hAC。

  • one-way is 1 if the street is one-way from V1 to V2 : 有一条从v1到v2的单行道(one-way)
  • In case the shortest path is not unique, output the fastest one among the shortest paths, which is guaranteed to be unique. :对于最短路径不唯一的情况,输出其中的最快的路径,题目保证这样的路径唯一。
  • In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.:对于最快路径不唯一的情况,输出其中的到过最少点的路径,题目保证这样的路径唯一

思路

  • 首先构造长度图d、时间图t。
  • 对d通过dijkstra找到长度最短的路径,同时反向构图d2。再对d2通过dijkstra找时间最短的路径,存到ans1数组。
  • 对t通过dijkstra找到时间最短的路径,同时反向构图d2。再对d2通过DFS找通过结点数最短的路径,存到ans2数组。
  • 对比两个数组,进行输出。

总结

  • 最开始邻接矩阵法 + 两层4个dijkstra,用时104ms,修改为两次分别dijkstra + dfs(解法3)后时间没有变慢,空间仍未3300KB
  • 然后改用邻接表法 + 3个堆优化dijkstra + dfs(解法2),时间达到209ms,空间26000KB,分析是稠密图不适用于堆优化。
    • 因为堆优化dij对应时间复杂度: O ( m l o g 2 n ) O(mlog_2n) O(mlog2n)在稠密图中 = O ( n 2 ∗ l o g 2 n ) O(n^2*log_2n) O(n2log2n)
    • 朴素dij对应时间复杂度: O ( n 2 ) O(n^2) O(n2)
  • 改用(dij + 堆优化dij) + (dij + dfs),时间达到250ms,空间26000KB,所以不是稠密图的问题,而是用了哈希表map(用这个是为了方便把邻接矩阵改为邻接表后,对d2构图)。
  • 换回邻接矩阵 + 3个堆优化dijkstra + dfs,时间也只到92ms,空间2400KB,代码为解法1,没啥变化,阿巴阿巴,柳神只用了40ms,明天仔细看看。

解法1

//19:30 - 20:28
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int N = 501;
int n, m, v1, v2, oneway, len, tim, sv, ev, min_dist, min_time;
vector<int> ans, ans1, ans2;
int d[N][N], t[N][N], dist[N], times[N], pre[N];
bool st[N];
vector<vector<int>> d2;
void dij1(int s){
	memset(st, false, sizeof st), memset(dist, 0x3f, sizeof dist);
	priority_queue<pii, vector<pii>, greater<pii>> que; que.push({0, sv});
	dist[s] = 0;
	while(que.size()){
		int tar = que.top().second; que.pop();
		if(st[tar]) continue;
		if(tar == ev) break;
		st[tar] = true;
		for(int i = 0; i < n; i ++){
			if(!st[i] && d[tar][i]){
				if(dist[i] > dist[tar] + d[tar][i]){
					d2[i].clear(), d2[i].push_back(tar);
					dist[i] = dist[tar] + d[tar][i];
					que.push({dist[i], i});
				}
				else if(dist[i] == dist[tar] + d[tar][i])
					d2[i].push_back(tar);
			}
		}
	}
	memset(st, false, sizeof st), memset(times, 0x3f, sizeof times);
	while(que.size()) que.pop();
	que.push({0, ev});
	times[ev] = 0;
	while(que.size()){
		int tar = que.top().second; que.pop();
		if(st[tar]) continue;
		if(tar == sv) break;
		st[tar] = true;
		for(auto j : d2[tar]){
			if(times[j] > times[tar] + t[j][tar]){
				times[j] = times[tar] + t[j][tar];
				que.push({times[j], j});
				pre[j] = tar;
			}
		}
	}
	pre[ev] = -1;
	int start = sv;
	ans1.push_back(sv);
	while(start != ev){
		ans1.push_back(pre[start]);
		start = pre[start];
	}
	min_dist = dist[ev];
}
int min_cnt = INT_MAX;
void DFS(int s, int cnt){
	if(s == sv){
		if(cnt < min_cnt)
			ans2 = ans, min_cnt = cnt;
		return;
	}
	for(auto i : d2[s]){
		if(!st[i]){
			ans.push_back(i), st[i] = true;
			DFS(i, cnt + 1);
			ans.pop_back(), st[i] = false;			
		}
	}
}
void dij2(int s){
	memset(st, false, sizeof st), memset(times, 0x3f, sizeof times);
	priority_queue<pii, vector<pii>, greater<pii>> que; que.push({0, sv});
	times[s] = 0;
	d2.resize(n);
	while(que.size()){
		int tar = que.top().second; que.pop();
		if(st[tar]) continue;
		if(tar == ev) break;
		st[tar] = true;
		for(int i = 0; i < n; i++){
			if(!st[i] && t[tar][i]){
				if(times[i] > times[tar] + t[tar][i]){
					d2[i].clear();
					d2[i].push_back(tar);
					times[i] = times[tar] + t[tar][i];
					que.push({times[i], i});
				}else if(times[i] == times[tar] + t[tar][i])
					d2[i].push_back(tar);
			}	
		}
	}
	memset(st, false, sizeof st);
	st[ev] = true, ans.push_back(ev);
	DFS(ev, 0);
	reverse(ans2.begin(), ans2.end());
	min_time = times[ev];
}
int main(){
	cin>>n>>m;
	d2.resize(n);
	while(m--){
		cin>>v1>>v2>>oneway>>len>>tim;
		d[v1][v2] = len, t[v1][v2] = tim;
		if(!oneway) d[v2][v1] = len, t[v2][v1] = tim;
	}
	cin>>sv>>ev;
	dij1(sv);
	dij2(sv);
	if(ans1.size() == ans2.size()){
		int i = 0;
		while(i < ans1.size() && ans1[i] == ans2[i]) i++;
		if(i == ans1.size()){
			printf("Distance = %d; Time = %d: ", min_dist, min_time);
			for(int j = 0; j < ans1.size(); j ++)
				printf("%d%s", ans1[j], j == ans1.size() - 1 ? "\n" : " -> ");
			return 0;
		}
	}
	printf("Distance = %d: ", min_dist);
	for(int j = 0; j < ans1.size(); j ++)
		printf("%d%s", ans1[j], j == ans1.size() - 1 ? "\n" : " -> ");
	printf("Time = %d: ", min_time);
	for(int j = 0; j < ans2.size(); j ++)
		printf("%d%s", ans2[j], j == ans2.size() - 1 ? "\n" : " -> ");
	return 0;
}

解法2(邻接表 + 哈希 + 堆优化的dij * 3 + dfs * 1)

#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int N = 501;
int n, m, v1, v2, oneway, len, tim, sv, ev, min_dist1, min_time1, min_dist2, min_time2;
vector<int> ans, ans1, ans2;
int dist[N], times[N], pre[N];
bool st[N];
vector<vector<pair<int, int>>> d, d2, t;
unordered_map<int, unordered_map<int, int>> mp_t, mp_d;
void dij1(int s){
	memset(st, false, sizeof st), memset(dist, 0x3f, sizeof dist);
	priority_queue<pii, vector<pii>, greater<pii>> que; que.push({0, sv});
	dist[s] = 0;
	while(que.size()){
		int tar = que.top().second; que.pop();
		if(st[tar]) continue;
		if(tar == ev) break;
		st[tar] = true;
		for(auto j : d[tar]){
			if(dist[j.first] > dist[tar] + j.second){
				d2[j.first].clear();
				d2[j.first].push_back({tar, mp_t[tar][j.first]});
				dist[j.first] = dist[tar] + j.second;
				que.push({dist[j.first], j.first});
			}else if(dist[j.first] == dist[tar] + j.second)
				d2[j.first].push_back({tar, mp_t[tar][j.first]});
		}
	}
	memset(st, false, sizeof st), memset(times, 0x3f, sizeof times);
	while(que.size()) que.pop();
	que.push({0, ev});
	times[ev] = 0;
	while(que.size()){
		int tar = que.top().second; que.pop();
		if(st[tar]) continue;
		if(tar == sv) break;
		st[tar] = true;
		for(auto j : d2[tar]){
			if(times[j.first] > times[tar] + j.second){
				times[j.first] = times[tar] + j.second;
				que.push({times[j.first], j.first});
				pre[j.first] = tar;
			}
		}
	}
	pre[ev] = -1;
	int start = sv;
	ans1.push_back(sv);
	while(start != ev){
		ans1.push_back(pre[start]);
		start = pre[start];
	}
	min_dist1 = dist[ev], min_time1 = times[sv];
}
int min_cnt = INT_MAX;
void DFS(int s, int cnt){
	if(s == sv){
		if(cnt < min_cnt)
			ans2 = ans, min_cnt = cnt;
		return;
	}
	for(auto i : d2[s]){
		if(!st[i.first]){
			ans.push_back(i.first), st[i.first] = true;
			DFS(i.first, cnt + 1);
			ans.pop_back(), st[i.first] = false;			
		}
	}
}
void dij2(int s){
	memset(st, false, sizeof st), memset(times, 0x3f, sizeof times);
	priority_queue<pii, vector<pii>, greater<pii>> que; que.push({0, sv});
	times[s] = 0;
	d2.resize(n);
	while(que.size()){
		int tar = que.top().second; que.pop();
		if(st[tar]) continue;
		if(tar == ev) break;
		st[tar] = true;
		for(auto j : t[tar]){
			if(times[j.first] > times[tar] + j.second){
				d2[j.first].clear();
				d2[j.first].push_back({tar, mp_d[tar][j.first]});
				times[j.first] = times[tar] + j.second;
				que.push({times[j.first], j.first});
			}else if(times[j.first] == times[tar] + j.second)
				d2[j.first].push_back({tar, mp_d[tar][j.first]});
		}
	}
	min_time2 = times[ev];
	memset(st, false, sizeof st);
	st[ev] = true, ans.push_back(ev);
	DFS(ev, 0);
	reverse(ans2.begin(), ans2.end());
	min_dist2 = dist[sv], min_time2 = times[ev];
}
int main(){
	cin>>n>>m;
	d.resize(n), t.resize(n), d2.resize(n);
	while(m--){
		cin>>v1>>v2>>oneway>>len>>tim;
		d[v1].push_back({v2, len}), t[v1].push_back({v2, tim});
		mp_t[v1][v2] = tim, mp_d[v1][v2] = len;
		if(!oneway){
			d[v2].push_back({v1, len}), t[v2].push_back({v1, tim});
			mp_t[v2][v1] = tim, mp_d[v2][v1] = len;
		} 
	}
	cin>>sv>>ev;
	dij1(sv);
	dij2(sv);
	if(ans1.size() == ans2.size()){
		int i = 0;
		while(i < ans1.size() && ans1[i] == ans2[i]) i++;
		if(i == ans1.size()){
			printf("Distance = %d; Time = %d: ", min_dist1, min_time2);
			for(int j = 0; j < ans1.size(); j ++)
				printf("%d%s", ans1[j], j == ans1.size() - 1 ? "\n" : " -> ");
			return 0;
		}
	}
	printf("Distance = %d: ", min_dist1);
	for(int j = 0; j < ans1.size(); j ++)
		printf("%d%s", ans1[j], j == ans1.size() - 1 ? "\n" : " -> ");
	printf("Time = %d: ", min_time2);
	for(int j = 0; j < ans2.size(); j ++)
		printf("%d%s", ans2[j], j == ans2.size() - 1 ? "\n" : " -> ");
	return 0;
}

解法3 - 错误逻辑的AC代码

//19:30 - 
#include<bits/stdc++.h>
using namespace std;
const int N = 501;
int n, m, v1, v2, tmp, len, tim, sv, ev, min_dist1, min_time1, min_dist2, min_time2;
vector<int> ans, ans1, ans2;
int d[N][N], t[N][N], g[N][N], dist[N], times[N], pre[N];
bool st[N];
vector<vector<int>> d2;
void dij1(int s){
	memset(st, false, sizeof st);
	memset(dist, 0x3f, sizeof dist);
	dist[s] = 0;
	d2.resize(n);
	for(int i = 0; i < n; i ++){
		int tar = -1;
		for(int j = 0; j < n; j ++)
			if(!st[j] && (tar == -1 || dist[j] < dist[tar])) tar = j;
		st[tar] = true;
		for(int j = 0; j < n; j ++){
			if(!st[j] && d[tar][j]){
				if(dist[j] > dist[tar] + d[tar][j]){
					d2[j].clear();
					d2[j].push_back(tar);
					dist[j] = dist[tar] + d[tar][j];
				}else if(dist[j] == dist[tar] + d[tar][j])
					d2[j].push_back(tar);
			}
		}
	}
	memset(st, false, sizeof st);
	memset(times, 0x3f, sizeof times);
	times[ev] = 0;
	for(int i = 0; i < n; i ++){
		int tar = -1;
		for(int j = 0; j < n; j ++)
			if(!st[j] && (tar == -1 || times[j] < times[tar])) tar = j;
		st[tar] = true;
		if(tar == sv) break;
		for(auto j : d2[tar])
			if(!st[j] && times[j] > times[tar] + t[tar][j]){
				times[j] = times[tar] + t[tar][j];
				pre[j] = tar;
			}	
	}
	pre[ev] = -1;
	int start = sv;
	ans1.push_back(sv);
	while(start != ev){
		ans1.push_back(pre[start]);
		start = pre[start];
	}
	min_dist1 = dist[ev], min_time1 = times[sv];
}
int max_cnt = INT_MAX;
void DFS(int s, int cnt){
	if(s == sv){
		if(cnt < max_cnt){
			max_cnt = cnt;
			ans2 = ans;
		}
		return;
	}
	for(auto i : d2[s]){
		if(!st[i]){
			ans.push_back(i); st[i] = true;
			DFS(i, cnt + 1);
			ans.pop_back(); st[i] = false; 	
		}
	}
}
void dij2(int s){
	memset(st, false, sizeof st);
	memset(times, 0x3f, sizeof times);
	times[s] = 0;
	d2.resize(n);
	for(int i = 0; i < n; i ++){
		int tar = -1;
		for(int j = 0; j < n; j ++)
			if(!st[j] && (tar == -1 || times[j] < times[tar])) tar = j;
		st[tar] = true;
		for(int j = 0; j < n; j ++){
			if(!st[j] && t[tar][j]){
				if(times[j] > times[tar] + t[tar][j]){
					d2[j].clear();
					d2[j].push_back(tar);
					times[j] = times[tar] + t[tar][j];
				}else if(times[j] == times[tar] + t[tar][j])
					d2[j].push_back(tar);
			}
		}
	}
	memset(st, false, sizeof st);
	memset(dist, 0x3f, sizeof dist);
	dist[ev] = 0;
	ans.push_back(ev); st[ev] = true;
	DFS(ev, 0);
	reverse(ans2.begin(), ans2.end());
	min_time2 = times[ev];
}
int main(){
	cin>>n>>m;
	while(m--){
		cin>>v1>>v2>>tmp>>len>>tim;
		d[v1][v2] = d[v2][v1] = len;
		t[v1][v2] = t[v2][v1] = tim;
		g[v1][v2] = g[v2][v1] = tmp;
	}
	cin>>sv>>ev;
	dij1(sv);
	dij2(sv);
	int i = 0;
	if(ans1.size() == ans2.size()){
		int i = 0;
		while(i < ans1.size() && ans1[i] == ans2[i]) i++;
		if(i == ans1.size()){
			printf("Distance = %d; Time = %d: ", min_dist1, min_time1);
			for(int j = 0; j < ans1.size(); j ++)
				printf("%d%s", ans1[j], j == ans1.size() - 1 ? "\n" : " -> ");
			return 0;
		}
	}
	printf("Distance = %d: ", min_dist1);
	for(int j = 0; j < ans1.size(); j ++)
		printf("%d%s", ans1[j], j == ans1.size() - 1 ? "\n" : " -> ");
	printf("Time = %d: ", min_time2);
	for(int j = 0; j < ans2.size(); j ++)
		printf("%d%s", ans2[j], j == ans2.size() - 1 ? "\n" : " -> ");
	return 0;
}

题目

Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (2≤N≤500), and M, being the total number of streets intersections on a map, and the number of streets, respectively. Then M lines follow, each describes a street in the format:

V1 V2 one-way length time

where V1 and V2 are the indices (from 0 to N−1) of the two ends of the street; one-way is 1 if the street is one-way from V1 to V2, or 0 if not; length is the length of the street; and time is the time taken to pass the street.

Finally a pair of source and destination is given.

Output Specification:

For each case, first print the shortest path from the source to the destination with distance D in the format:

Distance = D: source -> v1 -> ... -> destination

Then in the next line print the fastest path with total time T:

Time = T: source -> w1 -> ... -> destination

In case the shortest path is not unique, output the fastest one among the shortest paths, which is guaranteed to be unique. In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.

In case the shortest and the fastest paths are identical, print them in one line in the format:

Distance = D; Time = T: source -> u1 -> ... -> destination

Sample Input 1:

10 15
0 1 0 1 1
8 0 0 1 1
4 8 1 1 1
3 4 0 3 2
3 9 1 4 1
0 6 0 1 1
7 5 1 2 1
8 5 1 2 1
2 3 0 2 2
2 1 1 1 1
1 3 0 3 1
1 4 0 1 1
9 7 1 3 1
5 1 0 5 2
6 5 1 1 2
3 5

Sample Output 1:

Distance = 6: 3 -> 4 -> 8 -> 5
Time = 3: 3 -> 1 -> 5

Sample Input 2:

7 9
0 4 1 1 1
1 6 1 1 3
2 6 1 1 1
2 5 1 2 2
3 0 0 1 1
3 1 1 1 3
3 2 1 1 2
4 5 0 2 2
6 5 1 1 2
3 5

Sample Output 2:

Distance = 3; Time = 4: 3 -> 2 -> 5
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值