HDU2066 一个人的旅行 (priority_queue)

题目:hdu2066
就是找最短路径问题。
本来是多源起点的,但是把小草的加当作是0的话,就变成单源起点了。
这条直接用优先队列就可以了,再存路径的时候,推荐使用 vector ,更方便一点。而且存路径应该是双向的。

#include <bits/stdc++.h>

using namespace std;
const int inf=1<<30;
struct edge
{
    int city,t;
    bool operator <(const edge &D)const    
    {
        return t>=D.t;
    }
};

vector<edge> A[1005];     //存路径
priority_queue<edge> Q;
int T,S,D,a,b,t;
int en[1005],d[1005];     // en 存终点,d[i] 表示 0 到 i 的最短时间

bool init(int n)
{
    for(int i=1; i<=D; i++)
        if(n==en[i])
            return true;
    return false;
}

void bfs()
{
    while(!Q.empty())
        Q.pop();

    for(int i=0; i<=1000; i++)
        d[i]=inf;
    edge temp,tem;
    temp.city=0;
    temp.t=0;
    Q.push(temp);

    while(!Q.empty())
    {
        temp=Q.top();
        Q.pop();
        int c=temp.city;
        if(init(c))
        {
            cout<<temp.t<<endl;
            return;
        }
        int si=A[c].size();
        for(int i=0; i<si; i++)
        {
            tem.city=A[c][i].city;
            tem.t=A[c][i].t+temp.t;
            if(tem.t>=d[tem.city])
                continue;
            Q.push(tem);
            d[tem.city]=tem.t;
        }
    }
}

int main()
{
    while(cin>>T>>S>>D)
    {
        for(int i=0; i<=1000; i++)
            A[i].clear();
        edge temp;
        for(int i=1; i<=T; i++)
        {
            cin>>a>>b>>t;
            temp.city=b;
            temp.t=t;
            A[a].push_back(temp);
            temp.city=a;
            A[b].push_back(temp);           //路径是双向的
        }
        temp.t=0;
        for(int i=1; i<=S; i++)
        {
            cin>>a;
            temp.city=a;
            A[0].push_back(temp);         //A[0] 存起点
        }
        for(int i=1; i<=D; i++)
            cin>>en[i];
        bfs();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值