hdu-2068-Choose the best route

来源:互联网 发布:写漫画的软件 编辑:程序博客网 时间:2024/06/05 08:12

Choose the best route

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7046    Accepted Submission(s): 2294


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=<s<=n) n stands for the number of bus stations in this city and m stands for the number of directed ways between bus stations .(Maybe there are several ways between two bus stations .) s stands for the bus station that near Kiki’s friend’s home.
Then follow m lines ,each line contains three integers p , q , t (0<t<=1000). means from station p to station q there is a way and it will costs t minutes .
Then a line with an integer w(0<w<n), means the number of stations Kiki can take at the beginning. Then follows w integers stands for these stations.
 

Output
The output contains one line for each data set : the least time Kiki needs to spend ,if it’s impossible to find such a route ,just output “-1”.
 

Sample Input
5 8 51 2 21 5 31 3 42 4 72 5 62 3 53 5 14 5 122 34 3 41 2 31 3 42 3 211
 

Sample Output
1-1
 

Author
dandelion
 

Source
2009浙江大学计算机研考复试(机试部分)——全真模拟
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  2112 1874 1217 2066 1142 

题目大意:Kiki 要到朋友家里去,然后她希望在最短的时间内到她朋友家,告诉你她朋友家所在的车站位置,和一开始时你可以搭乘的车站位置,问怎么搭公交车可以最快到她朋友家,公交路线是单向的边。

思路:反过来想其实就是求她朋友家到她家周围车站的最短路就行,所以只要一遍dij算法就可以求出答案,不过一开始处理边的时候要把a到b改成b到a因为是反过来走的。并且用邻接矩阵做应该会超时。没去尝试过。

代码 :
#include <algorithm>#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <queue>#include <cstdlib>using namespace std;const int maxn = 22222222;int n,m,s,head[1100],vis[1100],d[1100],cnt;struct Edge{    int u,v,w,next;    Edge(int a =0, int b=0, int c = 0)    {        u = a;        v = b;        w = c;    }}e[41000];struct node{    int vis,val;    friend bool operator <(node x, node y)    {        return x.val>y.val;    }    node (int a=0, int b=0)    {        vis = a;        val = b;    }};void add(Edge q){    e[cnt].u = q.u;    e[cnt].v = q.v;    e[cnt].w = q.w;    e[cnt].next = head[q.u];    head[q.u] = cnt++;}void dij(int st){    int i;    memset(vis,0,sizeof(vis));    for(i = 1; i<=n;i++)        d[i] = maxn;    d[st] = 0;    priority_queue <node> q;    q.push(node(st,d[st]));    while (!q.empty())    {        node w = q.top();        q.pop();        if(vis[w.vis] == 1)        {            continue;        }        vis[w.vis] = 1;        int begin = w.vis;        for(i = head[begin];i!=-1;i=e[i].next)        {            int end = e[i].v;            if(d[end]>d[begin] + e[i].w )            {                d[end]=d[begin] + e[i].w;                q.push(node (end,d[end]));            }        }    }}int main(){    while (scanf("%d %d %d",&n,&m,&s)!=EOF)    {        int i,j;        int a,b,c;        memset(head,-1,sizeof(head));        cnt = 0;        for(i = 0; i < m; i++)        {            scanf("%d %d %d",&a,&b,&c);            add(Edge(b,a,c));        }        int z;        cin>>z;        int t[1100];        for(i = 0; i< z;i++)        {            scanf("%d",&t[i]);        }        dij(s);        int ans = maxn;        for(i = 0;i<z;i++)        {            if(ans>d[t[i]])            {                ans = d[t[i]];            }        }        if(ans == maxn)            cout<<"-1";        else            cout<<ans;        cout<<endl;    }    return 0;}

0 0