1111 Online Map (30分)

34 篇文章 0 订阅

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

也就几个小时吧,两次dij,两次dfs。因为图方便,把距离和时间都用node存了,oneway是0的话证明是双向的。就是一个点卡了很久,我一直以为时间一样看距离哪个短,鬼知道时间一样看的是结点个数哪个少,*你 *
邻接表的dij必须多练几遍,如果不是这道题我一定用邻接矩阵,才500个结点,邻接表的dfs也必须好好练练,结点和下标总是弄错!!dij中下标为i的结点序号其实是Adj[u][i].end,不是i!
用前驱结点算时间时,从temp.size()-1结点遍历到1号结点,分别定义id和nextID,邻接表就可以直接用,邻接矩阵的话要遍历当前id结点的子结点们,直到找到id某个子结点的end是nextId才可以用

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
struct node{
    int end;
    int length;
    int time;
};
const int maxn = 510;
const int INF = 1000000000;
int n, m, start, e;
vector<node> Adj[maxn];//不用初始化
int dis[maxn];//存放start结点到每个结点的最短距离
int cost[maxn];//存放start结点到每个结点的最短时间
bool vis[maxn];
vector<int> preLength[maxn];
vector<int> preTime[maxn];
vector<int> temp;
vector<int> disWay;
vector<int> timeWay;
int minTime = INF;
int minLength = INF;

void dijkstraLength(int s){
    fill(vis, vis+maxn, false);
    fill(dis, dis + maxn, INF);
    dis[s] = 0;
    for(int i = 0; i < n; i++){
        int u = -1, minn = INF;
        for(int j = 0; j < n; j++){
            if(vis[j] == false && dis[j] < minn){
                minn = dis[j];
                u = j;
            }
        }
        if(u == -1) return;//证明start结点已经别的路可以走了
        vis[u] = true;
        for(int k = 0; k < Adj[u].size(); k++){
        	int v = Adj[u][k].end;
            if(vis[v] == false){
                if(Adj[u][k].length + dis[u] < dis[v]){
                    dis[v] = dis[u] + Adj[u][k].length;
                    preLength[v].clear();
                    preLength[v].push_back(u);
                }
                else if(Adj[u][k].length + dis[u] == dis[v]){
                    preLength[v].push_back(u);
                }
            }
        }
    }
}

void dijkstraTime(int s){
    fill(cost, cost+maxn, INF);
    fill(vis, vis + maxn, false);
    cost[s] = 0;
    for(int i = 0; i < n; i++){
        int u = -1, minn = INF;
        for(int j = 0; j < n; j++){
            if(vis[j] == false && cost[j] < minn){
                minn = cost[j];
                u = j;
            }
        }
        if(u == -1) return;
        vis[u] = true;
        for(int i = 0; i < Adj[u].size(); i++){
        	int v = Adj[u][i].end;
            if(vis[v] == false){
                if(cost[u] + Adj[u][i].time < cost[v]){
                    preTime[v].clear();
                    preTime[v].push_back(u);
                    cost[v] = cost[u] + Adj[u][i].time;
                }
                else if(cost[u] + Adj[u][i].time == cost[v]){
                    preTime[v].push_back(u);
                }
            }
        }
    }
}

void dfsLength(int v){
    temp.push_back(v);
    if(v == start){
        int sum = 0;
        for(int i = temp.size() - 1; i > 0; i--){//要比较这条路径的总时间和目前最短时间哪个更短
            int id = temp[i];
            int nextId = temp[i-1];
            for(int j = 0; j < Adj[id].size(); j++){
                if(Adj[id][j].end == nextId){
                    sum += Adj[id][j].time;
                    break;
                }
            }
        }
        if(sum < minTime){
            disWay = temp;
            minTime = sum;
        }
        temp.pop_back();
        return;
    }
    for(int i = 0; i < preLength[v].size(); i++){
        dfsLength(preLength[v][i]);
    }
    temp.pop_back();
}

void dfsTime(int v){
    temp.push_back(v);
    if(v == start){
        if(temp.size() < minLength){
            timeWay = temp;
            minLength = temp.size();
        }
        temp.pop_back();
        return;
    }
    for(int i = 0; i < preTime[v].size(); i++){
        dfsTime(preTime[v][i]);
    }
    temp.pop_back();
}

int main(){
    cin>>n>>m;
    int u, v, oneway, l, t;
    for(int i = 0; i < m; i++){
        cin>>u>>v>>oneway>>l>>t;
        node temp;
        temp.end = v;
        temp.length = l;
        temp.time = t;
        Adj[u].push_back(temp);
        if(oneway == 0){
            temp.end = u;
            Adj[v].push_back(temp);
        }
    }
    cin>>start>>e;
    dijkstraLength(start);
    dijkstraTime(start);
    dfsLength(e);//找最短路径中时间最短的
    temp.clear();
    dfsTime(e);//找时间最短路径中路程最短的
    if(disWay == timeWay){
        printf("Distance = %d; Time = %d: ",dis[e],cost[e]);
        for(int i = disWay.size() - 1; i >= 0; i--){
            if(i!=disWay.size() - 1) cout<<" -> ";
            cout<<disWay[i];
        }
    }
    else{
        printf("Distance = %d: ",dis[e]);
        for(int i = disWay.size() - 1; i >= 0; i--){
            if(i!=disWay.size() - 1) cout<<" -> ";
            cout<<disWay[i];
        }
        cout<<endl;
        printf("Time = %d: ",cost[e]);
        for(int i = timeWay.size() - 1; i >= 0; i--){
            if(i!=timeWay.size() - 1) cout<<" -> ";
            cout<<timeWay[i];
        }
    }
    cout<<endl;
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值