HDU2680 Choose the best route

题目大意:一个笨蛋要坐车去朋友家,但坐车呕吐,所以想在最短时间内到达。

测试数据意思:

第一行三个数:n(车站的个数,n<1000)  |  m(代表车站之间所有线路的总个数)  | s(代表离朋友家最近的车站)

下面有m行:   p q t   意思是:一条从p到q的线路,花费t时间

m行之后有个数字:w (代表可以在开始时搭乘的车站)

下面w个数W1,W2....Ww就是车站的编号

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
#define inf 1e8
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[1005];
int n,m,k,w,dis[1005],a,b,t;
void dijkstra(int x)
{
    for(int i=1;i<=n;i++)
        dis[i]=inf;
    dis[x]=0;
    priority_queue<node>q;
    q.push(node(x,0));
    while(!q.empty())
    {
        node x=q.top();
        q.pop();
        for(int 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()
{
    //freopen("in.txt","r",stdin);
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        for(int i=1;i<=n;i++)
            eg[i].clear();
        while(m--)
        {
            scanf("%d%d%d",&a,&b,&t);
            eg[b].push_back(node(a,t));
        }
        dijkstra(k);
        scanf("%d",&w);
        int ans=inf;
        while(w--)
        {
            scanf("%d",&a);
            if(dis[a]<ans) ans=dis[a];
        }
        if(ans<inf) printf("%d\n",ans);
        else printf("-1\n");
    }
    return 0;
}
时间: 2024-10-02 22:34:06

HDU2680 Choose the best route的相关文章

HDU2680 Choose the best route 【Dijkstra】

Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7061    Accepted Submission(s): 2300 Problem Description One day , Kiki wants to visit one of her friends. As she is liable

HDU-2680 Choose the best route 单向边+反向dijkstra

https://vjudge.net/problem/HDU-2680 题意:给出m条边及其起始点终点与长度,已知可以从w个起点出发,求到某个终点s的最短路径.注意是单向边.m<1e5,w<n<1000. 题解:若每个起点都kijkstra一遍时间复杂度为O((E+VlogV)*V),会TLE,想了一下,终点当初起点,反向建边就可以了 坑点:做图论习题一度因为没看到directed,directional,wa到怀疑dijkstra错了 ac代码,用的邻接表存图及优先队列dijkstr

hdu2680 Choose the best route 最短路(多源转单源)

此题中起点有1000个,别有20000条.用链式前向星建图,再枚举起点用SPFA的话,超时了.(按理说,两千万的复杂度应该没超吧.不过一般说计算机计算速度 1~10 千万次/秒.也许拿最烂的计算机来卡时间) 有一个技巧,加一个超级源点.也就是加一个点,使得该点连通所有的起点,并且边的权值为0.这个技巧应用蛮多的.网络流.最小树形图都有题目这样做. #include<iostream> #include<cstdio> #include<cstring> #include

hdu 2680 Choose the best route 大年三十的首A 赤裸裸的Dijkstra 做这题需要一个小技巧

Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8063    Accepted Submission(s): 2655 Problem Description One day , Kiki wants to visit one of her friends. As she is liable

HDU 2680 Choose the best route(dijkstra+优先队列优化)

Choose the best route 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 stati

Choose the best route(最短路)dijk

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): 9602    Accepted Submission(s): 3111 Problem Description One day , Kiki wants

HDU - 2680 - Choose the best route (经典最短路问题dijkstra算法!!)

Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7671    Accepted Submission(s): 2524 Problem Description One day , Kiki wants to visit one of her friends. As she is liable

Choose the best route(迪杰斯特拉)

通过做这题,发现了自己的问题很大,做题不是贴代码,而是要了解思想:这题考的是有一个起点的集合,求起点集合到一个终点的最短距离, 本来想用Floy的但一看map[1000][1000]超时,有向图,逆序建邻接矩阵,这样就成了一个终点到所有点的最短路了. 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #define N 1000001 5 int n,m,s,w; 6 int map[1

Choose the best route

Choose the best route Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 12   Accepted Submission(s) : 6 Problem Description One day , Kiki wants to visit one of her friends. As she is liable to car