hdu 2680(最短路)
Choose the best route
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12442 Accepted Submission(s): 4046
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.
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 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”.
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
-1
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <queue>
using namespace std;
const int N = ;
const int INF = ;
int graph[N][N];
int n,m,s;
int low[N];
bool vis[N];
bool can[N];
int dijkstra(int s){
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++){
low[i] = graph[s][i];
}
vis[s] = true;
for(int i=;i<n;i++){
int Min = INF;
for(int j=;j<=n;j++){
if(!vis[j]&&low[j]<Min){
Min = low[j];
s= j;
}
}
vis[s] = true;
for(int j=;j<=n;j++){
if(!vis[j]&&low[j]>low[s]+graph[s][j]){
low[j]=low[s]+graph[s][j];
}
}
}
int result = INF;
for(int i=;i<=n;i++){
if(can[i])
result = min(result,low[i]);
}
return result;
}
int main()
{
while(scanf("%d%d%d",&n,&m,&s)!=EOF){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(i==j) graph[i][j] = ;
else graph[i][j] = INF;
}
}
for(int i=;i<=m;i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c); ///directed ways!!反向添边,因为我们是从终点开始找
if(graph[b][a]<c) continue; ///记得判重,不然AC不了
graph[b][a] = c;
}
int t;
memset(can,false,sizeof(can));
scanf("%d",&t);
while(t--){
int k;
scanf("%d",&k);
can[k]=true;
}
int c = dijkstra(s);
if(c>=INF) printf("-1\n");
else printf("%d\n",c);
}
return ;
}
hdu 2680(最短路)的更多相关文章
- HDU - 2680 最短路 spfa 模板
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目大意,就是一个人可以从多个起点开始出发,看到终点的最短路是多少..只有可以运用和hdu2066 ...
- HDU 2680 最短路 迪杰斯特拉算法 添加超级源点
Choose the best route Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- ACM: HDU 2544 最短路-Dijkstra算法
HDU 2544最短路 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
- UESTC 30 &&HDU 2544最短路【Floyd求解裸题】
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 5521 最短路
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- HDU - 2544最短路 (dijkstra算法)
HDU - 2544最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以 ...
- HDU 2680(最短路)(多个起始点)
这道题也是死命TLE.. http://acm.hdu.edu.cn/showproblem.php?pid=2680 /* 使用pair代替结构 */ #include <iostream&g ...
- hdu 2680 Choose the best route
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Description One day , Kiki ...
- 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 ( ...
随机推荐
- linux批量替换
sed -i "s/李三/李四/g" -r result/* 将result文件夹下的所有文件中的李三替换成李四 sed命令下批量替换文件内容 格式: sed -i ...
- relu函数为分段线性函数,为什么会增加非线性元素
relu函数为分段线性函数,为什么会增加非线性元素 我们知道激活函数的作用就是为了为神经网络增加非线性因素,使其可以拟合任意的函数.那么relu在大于的时候就是线性函数,如果我们的输出值一直是在大于0 ...
- kubectl alias auto complete
平时kubectl命令管理kubernetes,敲久了就觉得比较麻烦,想着使用alias k来代替kubectl,可是当输入k时没有了自动补全的功能 这里在 ~/.bashrc 添加如下配置后,可以自 ...
- A1082 Read Number in Chinese (25)(25 分)
A1082 Read Number in Chinese (25)(25 分) Given an integer with no more than 9 digits, you are suppose ...
- requests中文页面乱码解决方案【转】
requests中文页面乱码解决方案! 请给作者点赞 --> 原文链接 Python中文乱码,是一个很大的坑,自己不知道在这里遇到多少问题了.还好通过自己不断的总结,现在遇到乱码的情况越来越 ...
- MySQL基础9-主键约束、外键约束、等值连接查询、一对一和多对多关系
1.主键约束和外键约束 外键约束 * 外键必须是另一表的主键的值(外键要引用主键!) * 外键可以重复 * 外键可以为空 * 一张表中可以有多个外键! 概念模型在数据库中成为表 数据库表中的多对一关系 ...
- TCP的运输连接管理
TCP的运输连接管理 TCP是面向连接的协议,有三个阶段:连接建立.数据传送 和 连接释放.运输连接的管理就是使运输连接的简历和释放都能正常地进行. 在TCP连接建立过程中要解决一下三个问题: 1. ...
- loj2073 「JSOI2016」扭动的回文串
ref 主要是要理解"撑到"最长这个概念 (为啥我的代码这么长QAQ #include <iostream> #include <cstdio> using ...
- day02_02.能被3整除的个位数为6的数
第2题 能被3整除的个位数为6的数 难度增加一点点,再接再厉 注意: 把一些限制条件,用PHP编程的语言来执行 题目:输出100以内(不含100)能被3整除且个位数为6的所有整数 <?php f ...
- sklearn快速入门
原创博文,转载请注明出处. (为了节约空间,打印结果常用"..."表示省略) 一.加载数据集 1. 加载sklearn自带的数据集 scikit-learn有一些自带的标准数据集, ...