hdu 2680 Choose the best route
题目连接
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的更多相关文章
- 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 ( ...
- hdu 2680 Choose the best route (dijkstra算法)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2680 /************************************************* ...
- hdu 2680 Choose the best route 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目意思:实质就是给定一个多源点到单一终点的最短路. 卑鄙题---有向图.初始化map时 千万不 ...
- HDU 2680 Choose the best route(SPFA)
Problem DescriptionOne day , Kiki wants to visit one of her friends. As she is liable to carsickness ...
- HDU 2680 Choose the best route 最短路问题
题目描述:Kiki想去他的一个朋友家,他的朋友家包括所有的公交站点一共有n 个,一共有m条线路,线路都是单向的,然后Kiki可以在他附近的几个公交站乘车,求最短的路径长度是多少. 解题报告:这道题的特 ...
- HDU 2680 Choose the best route(多起点单终点最短路问题)题解
题意:小A要乘车到s车站,他有w个起始车站可选,问最短时间. 思路:用Floyd超时,Dijkstra遍历,但是也超时.仔细看看你会发现这道题目好像是多源点单终点问题,终点已经确定,那么我们可以直接转 ...
- HDU 2068 Choose the best route
http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one ...
- hdoj 2680 choose the best route
Problem Description One day , Kiki wants to visit one of her friends. As she is liable to carsicknes ...
- Choose the best route(最短路)dijk
http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Time Limit: 2000/1000 MS (Java/ ...
随机推荐
- 【教程】【FLEX】#003 自定义事件、模块间通讯
本篇笔记,主要阐明 事件是如何创建 和 如何使用自定义事件达到模块之间通讯 的效果. 句子解释: 什么叫做模块之间的通讯呢?? 简单点说,就是两个模块之间可以互相传数据. A模块 可以接收到 B模块的 ...
- Java导出数据为EXCEL的两种方式JXL和POI
JXL和POI导出数据方式的比较 POI支持excel2003和2007,而jxl只支持excel2003. 下面为测试代码: public class TestCondition { /** * 生 ...
- Devexpress 中如何写ASPxGridView新增修改时的数据验证
//验证 protected void grid_Deptlist_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidatio ...
- Track files and folders manipulation in Windows
The scenario is about Business Secret and our client do worry about data leakage. They want to know ...
- hdu1506
today, my cc can't make '__in64' it's so bad!!! i don't know why #include <stdio.h> //long lon ...
- Linux下对于inode的理解
0x01 什么是inode 文件存储在硬盘上,硬盘的最小存储单位叫做“扇区”(Sector),每个扇区储存512字节: 操作系统读取硬盘时,不会一个个扇区地读取,这样效率低,而是一次性连续读取多个扇区 ...
- 隐藏DLL
先来推广一下QQ群:61618925.欢迎各位爱好编程的加入. 在外挂或者病毒中,经常需要隐藏掉自己注入的DLL,以免被发现.下面就是一个隐藏DLL的通用模块,用的时候只需要加入到相关模块中即可. 详 ...
- static local variable
Putting the keyword static in front of a local variable declaration creates a special type of variab ...
- Objective-C基本数据类型、表达式和语句
一.基本数据类型 1.一般的计算机语言在定义变量的时候,需要提供给两个内容:类型和名字. 比如:int myClassID; 2.在Xcode中,无论你使用的是GCC编译器还是LLVM编译器,如果我们 ...
- Webservice SOAP传输序列化总结 以及webservice之序列化以及反序列化实例
一.所有Webservice中传递的对象都必须能够序列化,这个是作为在网络之间传输的必要条件.XML WebService和SOAP标准支持的数据类型如下: 1.基本数据类型. 标准类型,如:int ...