hdu 2680 Choose the best route(dijkstra 优先队列+堆 附模版)

题目列表

http://acm.hdu.edu.cn/showproblem.php?pid=2680

Choose the best route

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


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 5 1 2 2 1 5 3 1 3 4 2 4 7 2 5 6 2 3 5 3 5 1 4 5 1 2 2 3 4 3 4 1 2 3 1 3 4 2 3 2 1 1
 

Sample Output
  
  
1 -1
 

题目大意就是一个人坐公交车晕车,要找到时间最少的路线。

测试数据意思:
第一行三个数:n(车站的个数,n<1000)  |  m(代表车站之间所有线路的总个数)  | s(代表离朋友家最近的车站)
下面有m行:   p q t   意思是:一条从p到q的线路,花费t时间
m行之后有个数字:w (代表可以在开始时搭乘的车站)
下面w个数W1,W2....Ww就是车站的编号

思路:

逆向的找路径就好了,把终点当做起点,比较每个车站到终点的值,取最小的即可。我用的是Dijkstra的优先队列 优化做的。

先贴一下这个的模板。

/*
Dijkstra的算法思想:
在所有没有访问过的结点中选出dis(s,x)值最小的x
对从x出发的所有边(x,y),更新
dis(s,y)=min(dis(s,y),dis(s,x)+dis(x,y))
*/
#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
const int Ni = 10000;
const int INF = 1<<27;
struct node{
    int x,d;
    node(){}
    node(int a,int b){x=a;d=b;}
    bool operator < (const node & a) const
    {
        if(d==a.d) return x<a.x;
        else return d > a.d;
    }
};
vector<node> eg[Ni];
int dis[Ni],n;
void Dijkstra(int s)
{
    int i;
    for(i=0;i<=n;i++) dis[i]=INF;
    dis[s]=0;
    //用优先队列优化
    priority_queue<node> q;
    q.push(node(s,dis[s]));
    while(!q.empty())
    {
        node x=q.top();q.pop();
        for(i=0;i<eg[x.x].size();i++)
        {
            node y=eg[x.x][i];
            if(dis[y.x]>x.d+y.d)
            {
                dis[y.x]=x.d+y.d;
                q.push(node(y.x,dis[y.x]));
            }
        }
    }
}
int main()
{
    int a,b,d,m;
    while(scanf("%d%d",&n,&m),n+m)
    {
        for(int i=0;i<=n;i++) eg[i].clear();
        while(m--)
        {
            scanf("%d%d%d",&a,&b,&d);
            eg[a].push_back(node(b,d));
            eg[b].push_back(node(a,d));
        }
        Dijkstra(1);
        printf("%d\n",dis[n]);
    }
    return 0;
}
/*
6 6
1 2 2
3 2 4
1 4 5
2 5 2
3 6 3
5 6 3
*/

然后是这道题的AC代码:

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
const int Ni = 20010;
const int INF = 1<<27;
struct node{
    int x,d;
    node(){}
    node(int a,int b){x=a;d=b;}
    bool operator < (const node & a) const
    {
        if(d==a.d) return x<a.x;
        else return d > a.d;
    }
};
vector<node> eg[Ni];
int dis[Ni],n;
void Dijkstra(int s)
{
    int i;
    for(i=0;i<=n;i++) dis[i]=INF;
    dis[s]=0;
    //用优先队列优化
    priority_queue<node> q;
    q.push(node(s,dis[s]));
    while(!q.empty())
    {
        node x=q.top();
        q.pop();
        for(i=0;i<eg[x.x].size();i++)
        {
            node y=eg[x.x][i];
            if(dis[y.x]>x.d+y.d)
            {
                dis[y.x]=x.d+y.d;
                q.push(node(y.x,dis[y.x]));
            }
        }
    }
}
int main()
{
    int a,b,d,m,s,t,minx,de;
    int w[1010];
    while(~scanf("%d%d%d",&n,&m,&s))
    {
        for(int i=0;i<=n;i++) eg[i].clear();
        while(m--)
        {
            scanf("%d%d%d",&a,&b,&d);
            eg[b].push_back(node(a,d));
        }
        scanf("%d",&t);
        minx=INF;
        Dijkstra(s);
        for(int i=0;i<t;i++)
        {
            scanf("%d",&w[i]);
            //printf("%d\n",dis[w[i]]);
            if(dis[w[i]]<minx)
                minx=dis[w[i]];
        }
        if(minx==INF) printf("-1\n");
        else printf("%d\n",minx);
    }
    return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值