HDU 2680 Choose the best route 最短路

来源:互联网 发布:淘宝人工客服在哪里 编辑:程序博客网 时间:2024/06/05 16:12

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