zoukankan      html  css  js  c++  java
  • [HDU] 3790 最短路径问题一条边有两种权值

    题目链接:

    http://acm.hdu.edu.cn/showproblem.php?pid=3790

    方法:用建立有向图的方式建立无向图,调用dijkstra的时候,在优先队列里面的比较以及对边的松弛,都要依次根绝两种权值进行比较。

    感想:简单题。

    代码:

    View Code
    #include<iostream>
    #include<queue>
    //#include<algorithm>
    using namespace std;
    int const MAX =0x3f3f3f3f;
    int cityCount,roadsCount;
    struct Arc
    {
        int vetex;
        int dist;
        int cost;
        Arc* nextArc;
    };
    struct Node
    {
        int x;
        int current_dist;
        int current_cost;
        Arc* firstArc;
        bool visisted;
    };
    
    Node* nodes[1001];
    struct cmp  
    { 
        bool operator() (Node* x, Node* y)
        {
            if(x->current_dist>y->current_dist)
                return true;
            if(x->current_dist==y->current_dist && x->current_cost < y->current_cost)
                return true;
            return false;
        }
    };
    void createArc(int x,int y,int dist,int cost)
    {
        Arc* arc = (Arc*)malloc(sizeof(Arc));
        if(nodes[x]->firstArc==NULL)
            arc->nextArc = NULL;
        else
            arc->nextArc = nodes[x]->firstArc;
        arc->vetex = y;
        arc->dist=dist;
        arc->cost=cost;
        nodes[x]->firstArc = arc;
    }
    void loadArc(int x,int y,int dist,int cost)
    {
        createArc(x,y,dist,cost);
        createArc(y,x,dist,cost);
    }
    
    void getTarget(int x,int y)
    {
        priority_queue<Node*,vector<Node*>,cmp> q;
        q.push(nodes[x]);
        while(!q.empty())
        {
            Node* t_node = q.top();
            q.pop();
            if(t_node->visisted == false)
            {
                t_node->visisted=true;
                Arc* t_arc = t_node->firstArc;
                int t_cost,t_dist;
                while(t_arc!=NULL)
                {
                    if(nodes[t_arc->vetex]->visisted == false)
                    {
                        t_dist = t_arc->dist + t_node->current_dist;
                        t_cost = t_arc->cost + t_node->current_cost;
                        if(t_dist<nodes[t_arc->vetex]->current_dist)
                        {
                            nodes[t_arc->vetex]->current_dist= t_dist;
                            nodes[t_arc->vetex]->current_cost= t_cost;
                        }
                        else if(t_dist==nodes[t_arc->vetex]->current_dist && nodes[t_arc->vetex]->current_cost> t_cost)
                            nodes[t_arc->vetex]->current_cost= t_cost;
                        q.push(nodes[t_arc->vetex]);
                         
                    }
                    t_arc=t_arc->nextArc;
                }
            }
        }
    }
    int main()
    {     
        while(scanf("%d %d",&cityCount,&roadsCount) && !(cityCount==0 && roadsCount==0))
        {
            for(int i=1;i<=cityCount;i++)
            {
                nodes[i] =(Node*)malloc(sizeof(Node));
                nodes[i]->firstArc=NULL;
                nodes[i]->x=i;
                nodes[i]->visisted=false;
                nodes[i]->current_cost = nodes[i]->current_dist = MAX;
            }
            int _st,_ed,_dist,_cost;
            for(int i=0;i<roadsCount;i++)
            {
                scanf("%d %d %d %d",&_st,&_ed,&_dist,&_cost);
                if(_st!=_ed)
                    loadArc(_st,_ed,_dist,_cost);
            }
            scanf("%d %d",&_st,&_ed);
            nodes[_st]->current_cost = nodes[_st]->current_dist = 0;
            getTarget(_st,_ed);
            cout<<nodes[_ed]->current_dist<<" "<<nodes[_ed]->current_cost<<endl;
        }
        return 0;
    } 
  • 相关阅读:
    畅销书排行榜
    阿里云大数据产品体系
    天然气收费管理系统的研究与实现随笔
    Web端实现RTC视频特效的解决方案
    从0搭建在线聊天室,只需4步!
    技术干货 | JavaScript 之事件循环(Event Loop)
    C++20 四大特性之一:Module 特性详解
    Android Flutter 多实例实践
    网易云信线上万人连麦技术大揭秘
    Python + Pytest 自动化框架的用例依赖实操
  • 原文地址:https://www.cnblogs.com/kbyd/p/3021327.html
Copyright © 2011-2022 走看看