题目连接

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

Choose the best route

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

最短路。。

 #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using std::min;
using std::cin;
using std::cout;
using std::endl;
using std::find;
using std::sort;
using std::pair;
using std::vector;
using std::multimap;
using std::priority_queue;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr,val) memset(arr,val,sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = ;
const int INF = 0x3f3f3f3f;
typedef unsigned long long ull;
struct P {
int w, v;
P(int i = , int j = ) :w(i), v(j) {}
inline bool operator<(const P &a) const {
return w > a.w;
}
};
struct Node { int to, w, next; };
struct Dijkstra {
Node G[N];
int tot, dist[N], head[N];
inline void init() {
tot = , cls(dist, 0x3f), cls(head, -);
}
inline void add_edge(int u, int v, int w) {
G[tot] = { v, w, head[u] }; head[u] = tot++;
}
inline void built(int m) {
int u, v, w;
while (m--) {
scanf("%d %d %d", &u, &v, &w);
add_edge(v, u, w);
}
}
inline void dijkstra(int s) {
priority_queue<P> q;
q.push(P(, s));
dist[s] = ;
while (!q.empty()) {
P t = q.top(); q.pop();
int u = t.v;
if (dist[u] < t.w) continue;
for (int i = head[u]; ~i; i = G[i].next) {
int &w = dist[G[i].to];
if (w > dist[u] + G[i].w) {
w = dist[u] + G[i].w;
q.push(P(w, G[i].to));
}
}
}
}
inline void work(int s) {
dijkstra(s);
int k, v, ans = INF;
scanf("%d", &k);
while (k--) {
scanf("%d", &v);
ans = min(ans, dist[v]);
}
printf("%d\n", ans == INF ? - : ans);
}
}go;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n, m, s;
while (~scanf("%d %d %d", &n, &m, &s)) {
go.init();
go.built(m);
go.work(s);
}
return ;
}

hdu 2680 Choose the best route的更多相关文章

  1. 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 ( ...

  2. hdu 2680 Choose the best route (dijkstra算法)

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

  3. hdu 2680 Choose the best route 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目意思:实质就是给定一个多源点到单一终点的最短路. 卑鄙题---有向图.初始化map时 千万不 ...

  4. HDU 2680 Choose the best route(SPFA)

    Problem DescriptionOne day , Kiki wants to visit one of her friends. As she is liable to carsickness ...

  5. HDU 2680 Choose the best route 最短路问题

    题目描述:Kiki想去他的一个朋友家,他的朋友家包括所有的公交站点一共有n 个,一共有m条线路,线路都是单向的,然后Kiki可以在他附近的几个公交站乘车,求最短的路径长度是多少. 解题报告:这道题的特 ...

  6. HDU 2680 Choose the best route(多起点单终点最短路问题)题解

    题意:小A要乘车到s车站,他有w个起始车站可选,问最短时间. 思路:用Floyd超时,Dijkstra遍历,但是也超时.仔细看看你会发现这道题目好像是多源点单终点问题,终点已经确定,那么我们可以直接转 ...

  7. HDU 2068 Choose the best route

    http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one ...

  8. hdoj 2680 choose the best route

    Problem Description One day , Kiki wants to visit one of her friends. As she is liable to carsicknes ...

  9. Choose the best route(最短路)dijk

    http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Time Limit: 2000/1000 MS (Java/ ...

随机推荐

  1. sqool导出oracle数据

    set colsep '|'               --设置|为列分隔符 set echo off --在用start命令执行一个sql脚本时,是否显示脚本中正在执行的SQL语句 set fee ...

  2. 洛谷P2751 [USACO4.2]工序安排Job Processing

    P2751 [USACO4.2]工序安排Job Processing 18通过 78提交 题目提供者该用户不存在 标签 难度普及+/提高 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 一家工 ...

  3. javascript设计模式-享元模式

    享元模式采用一个共享来有效的支持大量细小粒度的对象,避免大量有相同内容的类的开销(如内存耗费),共享一个元类. 应用场景:页面存在大量的资源密集型对象:他们具备一些共性,可以分离出公共操作的数据. 一 ...

  4. 【linux】 静态库编译

    文件如下: root@ubuntu:/home/test# ll total drwxr-xr-x root root Sep : ./ drwxr-xr-x root root Sep : ../ ...

  5. dig out deledted chat messages

    One of my friends asked me to do a favor for her. She said her friend deleted some important chat me ...

  6. Rich控件一

    Calendar控件 Calendar控件用来在Web页面中显示日历中的可选日期,并显示与特定日期关联的数据. 控件声明代码如下: <asp: Calendar id=" Calend ...

  7. 语音分享应用ios源码

    该源码是语音分享应用源码,本demo使用了科大讯飞语音识别作为分享内容的输入方式,同时也支持手动键盘输入分享内容,限制分享内容文字不能超过180个字符,分享内容输入完成后可以直接分享,分享SDK使用的 ...

  8. centos下网络的基本配置方法讲解

    上一篇中我们已经成功安装了我们的centos系统,但是我们可能发现我们安装的centos上不了网,所以这一章我们来说说如何配置centos来连接外网和局域网. 我们首先来认识一下linux下部分网络配 ...

  9. YUM软件管理

    YUM是一个RPM的前端程序,主要目的是设计用来解决RPM的依赖关系的问题,而不用手动安装所依赖的所有软件.它使用仓库保存管理RPM软件包,仓库的配置文件保存在/etc/yum.repos.d/目录下 ...

  10. 使用plupload绕过服务器,批量上传图片到又拍云

    本文最初发布于我的个人博客:Jerry的乐园 综述 论坛或者贴吧经常会需要分享很多图片,上传图片比较差的做法是上传到中央服务器上,中央服务器再转发给静态图片服务器.而这篇文章讲介绍如何使用pluplo ...