[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;
} 

 

posted @ 2013-04-15 00:38  kbyd  阅读(198)  评论(0编辑  收藏  举报