solution Of Pat 1111. Online Map (30)

本文介绍了一种在线地图推荐最短和最快路径的方法。利用Dijkstra算法计算两点间最短距离及其对应的最短时间,并确保输出路径的唯一性和经过交叉点的最小数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1111. Online Map (30)

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


结题思路
题意要求我们找到确定起点和终点的最短路。
要求1:N,M分别表示道路交汇点的个数和道路的条数;
要求2:从题意理解oneWay代表该道路是单行道;
要求3:最短路不唯一,输出用时最短的那条;最短用时不唯一,输出经过道路交汇点最少的那条。
(道路不存在小于0的权值,用Dijkstra算法即可。)

程序步骤:
第一步、我们建立二维数组存储链接信息;
第二步、Dijkstra查找确定终点和起点的最短路(比一般的Dijkstra多添加了一个用时数组);
第三步、取出上一步确定的路径,用栈将路径转化为正向存储;
第四步、由于题目要求我们求用时最短的最短路,将二维数组改造后即可用相同的Dijkstra算法来处理。
第五步、取出上一步确定的路径,用栈将路径转化为正向存储;
第六步、比较输出即可。

具体程序(AC)如下:

#include <iostream>
#include <vector>
#include <stack>
#include <climits>
#define inf INT_MAX
using namespace std;
struct street{
    int end;
    int dis;
    int cost;   
};
int Dijkstra(vector<vector<street> >& map,int start,int end,int n,vector<int>& preNode)
{//n代表城市个数
    //dis
    vector<int> fromStartDis;//所走路径长度存储数组
    vector<int> fromStartCost;//用时存储数组
    vector<bool> visited;//访问标记数组
    //初始化
    fromStartDis.resize(n,inf);
    fromStartCost.resize(n,inf);
    preNode.resize(n,-1);
    visited.resize(n,false);
    visited[start]=true;
    fromStartDis[start]=0;
    fromStartCost[start]=0;
    //初始化结束
    while(true)
    {
        for(int i=0;i<map[start].size();++i)//更新路径
        {
            if(fromStartDis[map[start][i].end]>fromStartDis[start]+map[start][i].dis)
            {//找到更短的路径,更新路径长度和时间花费
                fromStartDis[map[start][i].end]=fromStartDis[start]+map[start][i].dis;
                fromStartCost[map[start][i].end]=fromStartCost[start]+map[start][i].cost;
                preNode[map[start][i].end]=start;
            }
            else if(fromStartDis[map[start][i].end]==fromStartDis[start]+map[start][i].dis)
            {//找到花费更少的路径,更新时间花费
                if(fromStartCost[map[start][i].end]>fromStartCost[start]+map[start][i].cost)
                {
                    fromStartCost[map[start][i].end]=fromStartCost[start]+map[start][i].cost;
                    preNode[map[start][i].end]=start;
                }
            }
        }
        int min=inf;
        int minIndex=-1;
        for(int i=0;i<n;++i)
        {
            if(!visited[i]&&fromStartDis[i]<min)
            {
                min=fromStartDis[i];
                minIndex=i;
            }
        }
        if(minIndex==-1)//结束,未找到路径
            break;
        start=minIndex;
        visited[start]=true;
        if(start==end)//结束,找到路径
            break;
    }
    return fromStartDis[end];

}
void reversePreNode(vector<int>& pre,int cur,vector<int>& remain)//剔除数组中不在最短路中的节点
{//remain保存最后start->end的路径
    if(cur==-1)
        return;
    stack<int> s;
    while(!s.empty())s.pop();
    remain.clear();
    while(cur!=-1)
    {
        s.push(cur);
        cur=pre[cur];
    }
    while(!s.empty())
    {
        remain.push_back(s.top());
        s.pop();
    }
}
void prePrint(vector<int>& remain)
{//格式化输出
    if(remain.size()>0)
        cout<<remain[0];
    if(remain.size()>1)
        for(int i=1;i<remain.size();++i)
            cout<<" -> "<<remain[i];
}
int main()
{
    int n,m;
    cin>>n>>m;
    vector<vector<street> > map;//二维数组map
    map.resize(n);
    street tmp;
    int start,oneWay;
    for(int i=0;i<m;++i)
    {
        cin>>start>>tmp.end>>oneWay>>tmp.dis>>tmp.cost;
        map[start].push_back(tmp);
        if(!oneWay)
        {
            int st=start;
            start=tmp.end;
            tmp.end=st;
            map[start].push_back(tmp);
        }
    }//buildTable
    int end;
    cin>>start>>end;
    vector<int>preDis;
    vector<int>preCost;
    int disSum=Dijkstra(map,start,end,n,preDis);
    cout<<"Distance = "<<disSum;
    vector<int>remainDis;//保存最短路的整个路径
    reversePreNode(preDis,end,remainDis);//剔除数组中不在最短路中的节点
    //至此,关于选取最短距离的最短路计算结束
    for(int i=0;i<n;++i)//改造map数组,直接用之前的Dijkstra进行计算
        for(int j=0;j<map[i].size();++j)
        {
            map[i][j].dis=map[i][j].cost;
            map[i][j].cost=1;
        }
    int disCost=Dijkstra(map,start,end,n,preCost);
    vector<int>remainCost;//保存最短路的整个路径
    int i;
    reversePreNode(preCost,end,remainCost);//剔除数组中不在最短路中的节点
    //以下部分用于比较,然后进行格式化的输出
    if(remainCost.size()==remainDis.size())
    {
        for(i=0;i<remainCost.size();++i)
            if(remainCost[i]!=remainDis[i])
                break;
        if(i>=remainCost.size())
        {
            cout<<"; "<<"Time = "<<disCost<<": ";
            prePrint(remainCost);
            cout<<endl;
        }
        else
        {
            cout<<": ";prePrint(remainDis);cout<<endl;
            cout<<"Time = "<<disCost<<": ";
            prePrint(remainCost);cout<<endl;
        }
    }
    else
    {
        cout<<": ";prePrint(remainDis);cout<<endl;
        cout<<"Time = "<<disCost<<": ";
        prePrint(remainCost);cout<<endl;
    }
    return 0;
}
(c++题解,代码运行时间小于200ms,内存小于64MB,代码不能有注释)It’s time for the company’s annual gala! To reward employees for their hard work over the past year, PAT Company has decided to hold a lucky draw. Each employee receives one or more tickets, each of which has a unique integer printed on it. During the lucky draw, the host will perform one of the following actions: Announce a lucky number x, and the winner is then the smallest number that is greater than or equal to x. Ask a specific employee for all his/her tickets that have already won. Declare that the ticket with a specific number x wins. A ticket can win multiple times. Your job is to help the host determine the outcome of each action. Input Specification: The first line contains a positive integer N (1≤N≤10 5 ), representing the number of tickets. The next N lines each contains two parts separated by a space: an employee ID in the format PAT followed by a six-digit number (e.g., PAT202412) and an integer x (−10 9 ≤x≤10 9 ), representing the number on the ticket. Then the following line contains a positive integer Q (1≤Q≤10 5 ), representing the number of actions. The next Q lines each contain one of the following three actions: 1 x: Declare the ticket with the smallest number that is greater than or equal to x as the winner. 2 y: Ask the employee with ID y all his/her tickets that have already won. 3 x: Declare the ticket with number x as the winner. It is guaranteed that there are no more than 100 actions of the 2nd type (2 y). Output Specification: For actions of type 1 and 3, output the employee ID holding the winning ticket. If no valid ticket exists, output ERROR. For actions of type 2, if the employee ID y does not exist, output ERROR. Otherwise, output all winning ticket numbers held by this employee in the same order of input. If no ticket wins, output an empty line instead. Sample Input: 10 PAT000001 1 PAT000003 5 PAT000002 4 PAT000010 20 PAT000001 2 PAT000008 7 PAT000010 18 PAT000003 -5 PAT102030 -2000 PAT000008 15 11 1 10 2 PAT000008 2 PAT000001 3 -10 1 9999 1 -10 3 2 1 0 3 1 2 PAT000001 3 -2000 Sample Output: PAT000008 15 ERROR ERROR PAT000003 PAT000001 PAT000001 PAT000001 1 2 PAT102030
最新发布
08-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值