zoukankan      html  css  js  c++  java
  • PAT (Advanced Level) Practice 1111 Online Map (30分) (两次迪杰斯特拉混合)

    1.题目

    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

    2.题目分析

    1.输出最短路径,如果最短路径不唯一就输出时间最短的路径:可以看这里(数据结构与算法题目集(中文)7-9 旅游规划 (25分) (迪杰斯特拉算法)

    2.输出用时最少的路径,不唯一就输出经过节点最少的路径(所以要记录经过的路径,可以看这道题 数据结构与算法题目集(中文)7-35 城市间紧急救援 (25分)) 

    3.说明:已经将无法联通的节点的时间设为INF,所以在判断时间的时候就不用再判断彼此之间的路径是不是INF了

    3.代码

    代码比较冗杂(见谅)

    #include<iostream>
    #include<vector>
    #include<stack>
    using namespace std;
    #define INF 1000000
    struct node
    {
    	int length;
    	int time;
    };
    int visited[510];
    int visited2[510];
    node edges[510][510];
    int n, m,s,e;
    int dist[510];
    int cost[510];
    int cost2[510];
    int amount[510];
    int path1[501];
    int path2[501];
    int Dijkstra()
    {
    	visited[s] = 1;
    	visited2[s] = 1;
    	for (int i = 0; i < n; i++)
    	{
    		dist[i] = edges[s][i].length;
    		cost[i] = edges[s][i].time;
    		cost2[i] = edges[s][i].time;
    		amount[i] = 1;
    	}
    	int min, u=-1,costs;
    	int min2, u2 = -1, amounts;
    	for (int i = 0; i < n - 1; i++)
    	{
    		min = INF; min2 = INF;
    		for (int j = 0; j < n; j++)
    		{
    			if (visited[j] == 0 && dist[j] < min)
    			{
    				min = dist[j];
    				u = j;
    				costs = cost[j];
    			}
    		}
    		for (int j = 0; j < n; j++)
    		{
    			if (visited2[j] == 0 &&cost2[j] < min2)
    			{
    				min2 = cost2[j];
    				u2 = j;
    				amounts=amount[j]+1;
    			}
    		}
    		if (u == -1)return - 1;
    		if (u2 == -1)return -1;
    		visited[u] = 1;
    		visited2[u2] = 1;
    		for (int j = 0; j < n; j++)
    		{
    			if (visited[j] == 0 && edges[u][j].length + min < dist[j])
    			{
    				dist[j] = min + edges[u][j].length;
    				cost[j] = costs +edges[u][j].time;
    				path1[j] = u;
    			}
    			else if (visited[j] == 0 && edges[u][j].length + min == dist[j] && edges[u][j].time + costs < cost[j])
    			{
    				cost[j] = costs + edges[u][j].time;
    				path1[j] = u;
    			}
    		}
    		for (int j = 0; j < n; j++)
    		{
    			if (visited2[j] == 0 && edges[u2][j].time + min2 < cost2[j])
    			{
    				cost2[j] = min2 + edges[u2][j].time;
    				amount[j] = amounts + 1;
    				path2[j] = u2;
    			}
    			else if (visited2[j] == 0&& edges[u2][j].time + min2 == cost2[j] && amounts+1 < amount[j])
    			{
    				amount[j] = amounts +1;
    				path2[j] = u2;
    			}
    		}
    	}
    }
    
    int main()
    {
    	int a, b, c;
    	scanf("%d %d", &n, &m);
    	for (int i = 0; i < n; i++)
    	{
    		for (int j = 0; j < n; j++)
    		{
    			 edges[i][j].length = INF; edges[i][j].time = INF; edges[j][i].length = INF; edges[j][i].time = INF;
    		}
    	}
    
    	for (int i = 0; i < m; i++)
    	{
    		scanf("%d %d %d", &a, &b, &c);
    		scanf("%d %d", &edges[a][b].length, &edges[a][b].time);
    		if (c == 0)
    		{
    			edges[b][a].length = edges[a][b].length;
    			edges[b][a].time = edges[a][b].time;
    		}
    	}
    	scanf("%d %d", &s, &e);
    	Dijkstra();
    	int temp = e;
    	int origin = e;
    	stack<int>ss;
    	ss.push(e);
    	while (path1[e] != 0)
    	{
    		ss.push(path1[e]);
    		e = path1[e];
    	}
    	stack<int>ss2;
    	ss2.push(temp);
    	while (path2[temp] != 0)
    	{
    		ss2.push(path2[temp]);
    		temp = path2[temp];
    	}
    	if (ss == ss2)
    	{
    		printf("Distance = %d; Time = %d: ", dist[origin], cost2[origin]);
    		cout << s;
    		while (!ss.empty())
    		{
    			cout << " ->" << " " << ss.top() ; ss.pop();
    		}
    		cout << endl;
    		return 0;
    	}
    	
    	printf("Distance = %d: ", dist[origin]);
    	cout << s;
    	while (!ss.empty())
    	{
    		cout << " ->" << " " << ss.top(); ss.pop();
    	}
    	cout << endl;
    	printf("Time = %d: ", cost2[origin]);
    	cout << s;
    	while (!ss2.empty())
    	{
    		cout << " ->" << " " << ss2.top(); ss2.pop();
    	}
    	cout << endl;
    }
  • 相关阅读:
    读取web.xml中设置的参数
    在服务端中,读取properties资源文件中的数据
    window下,nodejs安装http-server,并开启HTTP服务器
    跨域请求资源的方式
    IView 给Submenu增加click事件失效解决方案
    Vue -- mounted方法中调用methods的方法(并取出mounted方法中回调函数的值)
    IView 使用Table组件时实现给某一列添加click事件
    物联网协议CoAP协议学习
    电脑操作
    物联网协议
  • 原文地址:https://www.cnblogs.com/Jason66661010/p/12788851.html
Copyright © 2011-2022 走看看