PAT-1111-最短路径问题

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

谷歌中文翻译:
输入我们的当前位置和目的地,在线地图可以推荐一些路径。现在您的工作是向用户推荐两条路径:一条是最短路径,另一条是最快路径。保证任何请求都存在路径。

输入规格:
每个输入文件包含一个测试用例。对于每种情况,第一行给出两个正整数N(2≤N≤500),而M分别为地图上街道交叉点的总数和街道数。然后是M行,每行以一种格式描述一条街道:

V1 V2 单向长度 时间

其中,V1和V2是街道两端的索引(从0到N-1);如果街道是从V1到V2的单向路,则单向为1;否则,则为0;长度是街道的长度;时间就是通过街道的时间。

最后给出了一对源和目的地。

输出规格:
对于每种情况,首先以以下格式打印从源到目的地的最短路径,其距离为D:

距离= D:源-> v1-> …->目标

然后在下一行中以总时间T打印最快的路径:

时间= T:来源-> w1-> …->目的地

如果最短路径不是唯一的,请输出最短路径中最快的一条,以确保唯一。如果最快的路径不是唯一的,则输出经过最少交叉点的那条,保证是唯一的。

如果最短路径和最快路径相同,则以以下格式将它们打印在一行中:

距离= D;时间= T:来源-> u1-> …->目的地

思路:
不知道为啥。。最后一个案例点没过 。
这里借鉴了柳神的思路,给了友情链接大家参考能过的案例。
柳神PAT1111链接

#include<bits/stdc++.h>
using namespace std;
const int MAX_N = 510;
const int INF = 100000000;

int n, m;
int e[MAX_N][MAX_N];
int w[MAX_N][MAX_N];
int dis[MAX_N];	
int Time[MAX_N];	//各点最少时间 
int weight[MAX_N];	//经过各边时间 
int NodeNum[MAX_N]; //交叉点 
int Timepre[MAX_N];
bool visit[MAX_N];
vector<int> pre[MAX_N]; //此题不使用 
vector<int> dispath;	//更新的条件是路径更短,或者路径相等同时时间更短 
vector<int> Timepath, temppath;
int dispre[MAX_N];
int st, fin; 	//起点 终点 
int minnode = INF;

void init() {
	fill(e[0], e[0] + MAX_N * MAX_N, INF);
	fill(w[0], w[0] + MAX_N * MAX_N, INF);
	fill(dis, dis + MAX_N, INF);
	fill(weight, weight + MAX_N, INF);
	fill(Time, Time + MAX_N, INF);
	cin >> n >> m;
	int a, b, flag, len, t;
	for(int i = 0; i < m; i++) {
		cin >> a >> b >> flag >> len >> t;
		e[a][b] = len;
		w[a][b] = t;
		if(flag == 0) {
			//双向边处理
			e[b][a] = len;
			w[b][a] = t;
		}
	}
	cin >> st >> fin;
	for(int i = 0; i < n; i++) {
		dispre[i] = i;
	}
}

void dijkstra1() {
	dis[st] = 0;
	for(int i = 0; i < n; i++) {
		int u = -1;
		int minn = INF;
		for(int j = 0; j < n; j++) {
			if(visit[j] == false && dis[j] < minn) {
				u = j;
				minn = dis[j];
			}
		}
		if(u == -1) break;
		visit[u] = true;
		for(int v = 0; v < n; v++) {
			if(visit[v] == false && e[u][v] != INF) {
				if(dis[v] > e[u][v] + dis[u]) {
					dis[v] = e[u][v] + dis[u];
					dispre[v] = u;
					weight[v] = weight[u] + w[u][v];
				} else if(e[u][v] + dis[u] == dis[v]) {
					if(weight[v] > weight[u] + w[u][v]) {
						weight[v] = weight[u] + w[u][v];
						dispre[v] = u;
					}
				}
			}
		}
	}
}

void dfs1(int v) {
	//由于pre[v]没有不同的前驱而记为一维数组
	//此处只要将一个个结点拿出来就好 
	dispath.push_back(v);
	if(v == st) 
		return ;
	dfs1(dispre[v]);
}

void dijkstra2() {
	Time[st] = 0;
	fill(visit, visit + MAX_N, false);
	for(int i = 0 ;i < n; i++) {
		int u = -1;
		int minn = INF;
		for(int j = 0; j < n; j++) {
			if(visit[j] == false && minn > Time[j]) {
				u = j;
				minn = Time[j];
			}
		}
		if(u == -1) break;
		visit[u] = true;
		for(int v = 0; v < n; v++) {
			if(visit[v] == false && w[u][v] != INF) {
				if(w[u][v] + Time[u] < Time[v]) {
					Time[v] = w[u][v] + Time[u];
					Timepre[v] = u;
					NodeNum[v] = NodeNum[u] + 1;
				}  else if(w[u][v] + Time[u] == Time[u]) {
					if(NodeNum[u] + 1 < NodeNum[v]){
						Timepre[v] = u;
						NodeNum[v] = NodeNum[u] + 1;
					}
				}
			}
		}
	}
}

void dfs2(int v) {
	Timepath.push_back(v);
	if(v == st) 
		return ;
	dfs2(Timepre[v]);
}


int main() {
	init();
	dijkstra1();
	dfs1(fin);
	dijkstra2();
	dfs2(fin);
	printf("Distance = %d", dis[fin]);
    if(dispath == Timepath) {
        printf("; Time = %d: ", Time[fin]);
    } else {
        printf(": ");
        for(int i = dispath.size() - 1; i >= 0; i--) {
            printf("%d", dispath[i]);
            if(i != 0) printf(" -> ");
        }
        printf("\nTime = %d: ", Time[fin]);
    }
    for(int i = Timepath.size() - 1; i >= 0; i--) {
        printf("%d", Timepath[i]);
        if(i != 0) printf(" -> ");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值