【未完成】【笨方法学PAT】1111 Online Map (30 分)

版权声明:欢迎转载,请注明来源 https://blog.csdn.net/linghugoolge/article/details/84867980

一、题目

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

二、题目大意

求最短路径和最时间。

三、考点

DFS、Dijkstra

四、注意

1、单独使用DFS会有一个测试点超时;

2、该代码单独使用DFS;

3、Dijkstra比DFS节省时间。

五、代码

#include<iostream>
#include<string>
#include<map>
#include<vector>
#define N 510
#define INF 9999999
using namespace std;
int mstart=1, mend;
vector<int> vec_happy;
int e[N][N],t[N][N];
bool visit[N] = { false };
int out_dis=INF, out_time=INF, out_dis_time=INF;
vector<int> vec_out_dis,vec_out_time, vec_path;
int n, m;

void dfs(int root, int dis, int time) {
	//return
	if (dis > out_dis && time>out_time)
		return;

	//arrive at mend
	if (root == mend) {
		//dis
		if (dis < out_dis || dis == out_dis && time<out_dis_time) {
			out_dis = dis;
			vec_out_dis = vec_path;
			out_dis_time = time;
		}
		
		//time
		if (time < out_time || vec_path.size() <vec_out_time.size() && time==out_time) {
			out_time = time;
			vec_out_time = vec_path;
		}
		return;
	}

	//next dfs
	for (int i = 0; i < n; ++i) {
		if (visit[i] == false && e[root][i] != INF) {
			visit[i] = true;
			vec_path.push_back(i);
			dfs(i, dis + e[root][i], time + t[root][i]);
			visit[i] = false;
			vec_path.pop_back();
		}
	}

	return;
}

int main() {
	//init
	fill(e[0], e[0] + N*N, INF);
	fill(t[0], t[0] + N*N, INF);
	fill(visit, visit + N, false);

	//read
	cin >> n >> m;
	for (int i = 0; i < m; ++i) {
		int a, b, c, d, f;
		//cin >> a >> b >> c >> d >> f;
		scanf("%d %d %d %d %d", &a, &b, &c, &d, &f);
		if (c == 1) {
			e[a][b] = d;
			t[a][b] = f;
		}
		else {
			e[a][b] =e[b][a]= d;
			t[a][b] =t[b][a]= f;
		}
	}

	cin >> mstart >> mend;

	//dfs
	vec_path.push_back(mstart);
	visit[mstart] = true;
	dfs(mstart, 0, 0);

	//output
	if (vec_out_dis != vec_out_time) {
		printf("Distance = %d: ", out_dis);
		for (int i = 0; i < vec_out_dis.size(); ++i) {
			if (i != 0)
				cout << " -> ";
			cout << vec_out_dis[i];
		}
		printf("\nTime = %d: ", out_time);
		for (int i = 0; i < vec_out_time.size(); ++i) {
			if (i != 0)
				cout << " -> ";
			cout << vec_out_time[i];
		}
	}
	else {
		printf("Distance = %d; Time = %d: ", out_dis, out_time);
		for (int i = 0; i < vec_out_dis.size(); ++i) {
			if (i != 0)
				cout << " -> ";
			cout << vec_out_dis[i];
		}
	}
	cout << endl;

	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/linghugoolge/article/details/84867980