HDU 2680 Choose the best route 最短路

Choose the best route
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 144 Accepted Submission(s): 57

Problem Description
One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend’s home as soon as possible . Now give you a map of the city’s traffic route, and the stations which are near Kiki’s home so that she can take. You may suppose Kiki can change the bus at any station. Please find out the least time Kiki needs to spend. To make it easy, if the city have n bus stations ,the stations will been expressed as an integer 1,2,3…n.

Input
There are several test cases.
Each case begins with three integers n, m and s,(n<1000,m<20000,1=

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<vector>
#include<queue>
using namespace std;
struct road
{
    int end;
    int cost;
    friend bool operator < (const road &a,const road &b)
    {
        return a.cost>b.cost;
    }
};
priority_queue<road> que;
vector<road> v[1005];
bool vis[1005];
int main()
{
    int n,m,i,j;
    int a,b,c;
    int sta;
    road p;
    int E;
    while(~scanf("%d%d%d",&n,&m,&E))
    {
        E++;
        memset(vis,0,sizeof(vis));
        for(i=0;i<1005;i++)
            v[i].clear();
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            a++;
            b++;
            p.end=b;
            p.cost=c;
            v[a].push_back(p);
        }
        scanf("%d",&m);
        for(i=0;i<m;i++)
        {
            scanf("%d",&sta);
            sta++;
            p.end=sta;
            p.cost=0;
            v[0].push_back(p);
        }
        p.end=0;
        p.cost=0;
        que.push(p);
        road now,next;
        int falg=0;
        while(!que.empty())
        {
            now=que.top();
            que.pop();
            if(vis[now.end])
                continue;
            vis[now.end]=true;
            if(now.end==E)
            {
                falg=1;
                break;
            }
            int d=v[now.end].size();
            for(i=0;i<d;i++)
            {
                next.end=v[now.end][i].end;
                if(vis[next.end])
                    continue;
                next.cost=v[now.end][i].cost+now.cost;
                que.push(next);
            }
        }
        if(falg)
            cout<<now.cost<<endl;
        else
            cout<<"-1"<<endl;
        while(!que.empty())
        {
            que.pop();
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值