题解 | #最短路径问题#

最短路径问题

https://www.nowcoder.com/practice/e372b623d0874ce2915c663d881a3ff2

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int maxn=1001;
struct edge{
    int to;
    int dis;
    int waste;
    edge(int a,int c,int d):to(a),
    dis(c),waste(d){};
    bool operator<(edge y) const{
        return dis>y.dis;
    }
};
struct point{
    int number;
    int dis;
    int waste;
    point(int a,int b,int c):number(a),dis(b),waste(c){};
    bool operator< (point y) const{
        if(dis==y.dis) return waste>y.waste;
        else return dis>y.dis;

    }
};
int dist[maxn];
bool visit[maxn];
int w[maxn];
vector<edge> G[maxn];
priority_queue<point> q;
void init(){
    for(int i=0;i<maxn;i++) {
        G[i].clear();
        dist[i]=1000;
        visit[i]=false;
        w[i]=0;
    }
    while(!q.empty()) q.pop();
}
int main() {
    int n,m;
    while(cin>>n>>m){
        if(n==0 && m==0) break;
        init();
        for(int i=0;i<m;i++){
            int a,b,d,p;
            cin>>a>>b>>d>>p;
            G[a].push_back(edge(b, d, p));
            G[b].push_back(edge(a,d,p));
        }
        int start,end;
        cin>>start>>end;
        dist[start]=0;
        q.push(point(start,0,0));
        while(!q.empty()){
            point tem=q.top();
            q.pop();
            for(int i=0;i<G[tem.number].size();i++){
                edge tp=G[tem.number][i];
                if((dist[tp.to]>(dist[tem.number]+tp.dis)) ||
               ( dist[tp.to]==(dist[tem.number]+tp.dis)&& (w[tp.to]
               >w[tem.number]+tp.waste))){
                    dist[tp.to]=dist[tem.number]+tp.dis;
                    q.push(point(tp.to,dist[tp.to],
                    tem.waste+tp.waste));
                    w[tp.to]=w[tem.number]+tp.waste;
                }
            }

        }
        cout<<dist[end]<<' '<<w[end]<<endl;
    }
}

全部评论

相关推荐

10-28 10:48
已编辑
门头沟学院 Java
孩子我想要offer:发笔试后还没笔试把我挂了,然后邮箱一直让我测评没测,后面不知道干嘛又给我捞起来下轮笔试,做完测评笔试又挂了😅
点赞 评论 收藏
分享
路过的周先森:我就喜欢这种无限复活+筛选快的,比那些投进去就没声还占用投递次数的好多了
投递快手等公司7个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务