1111 Online Map

#include<bits/stdc++.h>
using namespace std;
#define rep(x,y,z) for(int (x)=(y);(x)<(z);i++)
#define mst(x,y) memset(x,y,sizeof(x))
#define ll long long
const int maxn=1e4+10;
#define pii pair<int,int> 
#define piii pair<int,pii>
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
int n,m;
int x,y,z,p1,q1,s,t;
pair<int,int> p[maxn];
vector<piii> g[maxn];

vector<int> rt1,rt2;
int nxt1[maxn],nxt2[maxn];
void solve1(int x){
    if(x==-1) return ;
    solve1(nxt1[x]);
    rt1.push_back(x);
}
void solve2(int x){
    if(x==-1) return ;
    solve2(nxt2[x]);
    rt2.push_back(x);
}

pii gao(pii x,pii y){
    return mk(x.fi+y.fi,x.se+y.se);
}
struct node{
    int x;
	bool operator<(const node& y) const {
        return p[x]>p[y.x];
    }
};
void bfs(int s,int flag){
    p[s]=mk(0,0);
    priority_queue<node> pq;pq.push(node{s});
    while(!pq.empty()){
        node tp=pq.top();pq.pop();
        int x=tp.x;
        if(x==t) return ;
        rep(i,0,g[x].size()){
            int v=g[x][i].fi;
            pii tmp;
            if(flag) tmp=gao(p[x],g[x][i].se);
            else tmp=gao(p[x],mk(g[x][i].se.se,1));
            if(p[v]>tmp){
                p[v]=tmp;
                if(flag) nxt1[v]=x;
                else nxt2[v]=x;
                pq.push(node{v});
            }
        }
    }
    return ;
}

pair<int,int> tp;
int main(){
    mst(nxt1,-1),mst(nxt2,-1);
    cin>>n>>m;
    rep(i,0,m){
    	cin>>x>>y>>z>>p1>>q1;
        g[x].push_back(mk(y,mk(p1,q1)));
        if(!z) g[y].push_back(mk(x,mk(p1,q1)));
    }
    cin>>s>>t;
    rep(i,0,maxn) p[i]=mk(maxn*maxn,maxn*maxn);
    bfs(s,1);solve1(t);
    tp=p[t];
    rep(i,0,maxn) p[i]=mk(maxn*maxn,maxn*maxn);
    bfs(s,0);solve2(t);
    
    if(rt1!=rt2){
        printf("Distance = %d: ",tp.fi);
        rep(i,0,rt1.size()){
            cout<<rt1[i];
            if(i!=rt1.size()-1) cout<<" -> ";
            else cout<<"\n";
        }
        printf("Time = %d: ",p[t].fi);
        rep(i,0,rt2.size()){
            cout<<rt2[i];
            if(i!=rt2.size()-1) cout<<" -> ";
            else cout<<"\n";
        }
    }else{
        printf("Distance = %d; Time = %d: ",tp.fi,p[t].fi);
        rep(i,0,rt1.size()){
            cout<<rt1[i];
            if(i!=rt1.size()-1) cout<<" -> ";
            else cout<<"\n";
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值