天梯赛补题 - 周游世界(记录路径的最短路)

硬生生被我写出来了, 这个记录路径我绕了半天= =。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <set>
#include <vector>
#include <map>
#include <sstream>
#include <queue>
#define LL long long
const LL INF = 0x3f3f3f3f;
const int maxn = 200000 + 5;
using namespace std;
int a[maxn];
int d[maxn];
bool vis[maxn];
struct node{
    int line;
    int id;
    int nowid;
    int c;
    node(){}
    node(int l, int idd, int cc, int now):line(l),id(idd),c(cc),nowid(now){}//线路,哪个点过来的,路径长度,现在在哪个点
    bool operator < (const node &p) const{
        return c > p.c;
    }
};
vector<node> vec[maxn];
vector<node> pre[maxn];
vector<node> ans;
void dij(int s, int e){//白书优先队列写法
    memset(d, INF, sizeof(d));
    memset(vis, false, sizeof(vis));
    d[s] = 0;
    priority_queue<node> q;
    q.push(node(0,s,0,s));
    while(!q.empty()){
        node p = q.top();
        q.pop();
        int u = p.id;//换乘点
        if(vis[u]) continue;
        vis[u] = true;
        for(int i=0; i<vec[u].size(); i++) {
            int line = vec[u][i].line;//当前这个点在哪条线上
            int v = vec[u][i].id;//当前点
            int c = vec[u][i].c;
            if(d[u] + c < d[v]) {//白书记录路径
                d[v] = d[u] + c;
                q.push(node(0,v,d[v],u));//这个抛进去的线路无关答案
                pre[v].push_back(node(line,u,0,v));//记录前驱
            }
        }
    }
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1; i<=n; i++){
        int m;
        scanf("%d",&m);
        for(int j=0; j<m; j++){
            scanf("%d",&a[j]);
        }
        for(int j=0; j<m; j++){
            for(int k=0; k<m; k++){
                if(j == k) continue;
                vec[a[j]].push_back(node(i,a[k],abs(j-k),0));//两个点之间建边 总复杂度O NM²
            }
        }
    }
    int q;
    scanf("%d",&q);
    while(q--){//应该Q<=10 所以可以跑10次
        int st,ed;
        for(int i=0; i<10000; i++)
            pre[i].clear();
        scanf("%d%d",&st,&ed);
        dij(st,ed);
        if(d[ed] == INF){
            printf("Sorry, no line is available.\n");
        }
        else{
            printf("%d\n",d[ed]);
            node cur;
            ans.clear();
            int sz = (int)pre[ed].size();//记录前驱的vector只需要最上面的, 即是最短的
            for(cur = pre[ed][sz-1];;){
                ans.push_back(cur);
                if(cur.line == 0) break;//最后一站之前就没线路了 即 0
                int size = (int)pre[cur.id].size();
                if(size == 0) break;//没有前一站了
                cur = pre[cur.id][size-1];
            }
            sz = (int)ans.size();
            for(int i=sz-1; i>=0; i--){
                node temp = ans[i];
                printf("Go by the line of company #%d from %04d to %04d.\n",temp.line,temp.id,temp.nowid);
            }
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值