solution Of Pat 1111. Online Map (30)

来源:互联网 发布:python 绘制中国地图 编辑:程序博客网 时间:2024/05/21 20:24

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_MAXusing 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;}
0 0