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

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个点,m条单向边,然后给出一个终点,然后给出t个车站,求这些车站到终点耗费的时间最短是多少,如果不可达,输出-1.
题解:如果直接从车站去找是会超时的,所以我们把所有的边反向之后再从终点开始找,这样的话找一次就ok,还有就是这题要判重.
#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(最短路)的更多相关文章

  1. HDU - 2680 最短路 spfa 模板

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目大意,就是一个人可以从多个起点开始出发,看到终点的最短路是多少..只有可以运用和hdu2066 ...

  2. HDU 2680 最短路 迪杰斯特拉算法 添加超级源点

    Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  3. ACM: HDU 2544 最短路-Dijkstra算法

    HDU 2544最短路 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descrip ...

  4. UESTC 30 &&HDU 2544最短路【Floyd求解裸题】

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  5. hdu 5521 最短路

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  6. HDU - 2544最短路 (dijkstra算法)

    HDU - 2544最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以 ...

  7. HDU 2680(最短路)(多个起始点)

    这道题也是死命TLE.. http://acm.hdu.edu.cn/showproblem.php?pid=2680 /* 使用pair代替结构 */ #include <iostream&g ...

  8. hdu 2680 Choose the best route

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Description One day , Kiki ...

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

随机推荐

  1. CentOS6.7下的软件安装

    一.JDK安装及其环境变量的配置 **创建一个专门安装软件的文件夹:mkdir /root/apps **解压安装包:tar -zxvf jdk-7u45-linux-x64.tar.gz -C /r ...

  2. Gym - 101908G Gasoline 二分+最大流

    G - Gasoline Gym - 101908G 题意:给出R个提供点,P个接收点,每个接收点都要接收满,还有一个运输的时间,问最小时间能够完成所有的运输 题解:首先每次都必须要满流,所以我们只要 ...

  3. TCP/IP网络编程之套接字与标准I/O

    标准I/O函数 标准标准I/O函数有两个优点: 标准I/O函数具有良好的移植性 标准I/O函数可以利用缓冲提高性能 关于移植性无需过多解释,不仅是I/O函数,所有标准函数都具有良好的移植性.因为,为了 ...

  4. Linux服务器管理员必备Linux命令TOP5

    Linux桌面环境的界面友好度.图形性能及附件工具已经大幅进化,然而Linux服务器却还没有能达到这一步. 作为系统管理员必须熟练掌握Linux命令.Linux命令的内容很多,其中的一些TOP命令对于 ...

  5. Asp.net自定义控件开发任我行(1)-笑傲江湖

    1.引言 参加工作5个月了,来到一家小公司,有几只老鸟带我,但不是我公司的,几个礼拜才来一次.来到公司做的第一个项目是web项目,里面有很多的重复代码,页面代码都是千篇一律,你这人也太水了吧,垃圾代码 ...

  6. IOS开发---菜鸟学习之路--(十八)-利用代理实现向上一级页面传递数据

    其实我一开始是想实现微信的修改个人信息那样的效果 就是点击昵称,然后跳转到另外一个页面输入信息 但是细想发现微信的话应该是修改完一个信息后就保存了 而我做的项目可能需要输入多个数据之后再点击提交的. ...

  7. phpmyadmin漏洞利用general_log和general_log_file拿权限

    我们如果知道phpmyadmin和网站的绝对路径后,可以利用into outfile写马拿到权限,但是吧有新版本的mysql和一些系统设置的问题这个函数受到了secure-file-priv这个的影响 ...

  8. 【LoadRunner】利用lr_db_connect函数对Oracle数据库压测的完整流程

    项目中常常会有直接对数据库进行压测的需求,以前都是通过Jmeter实现的,但是Jmeter本身图表及结果收集方面没有Loadrunner那么强大,所以利用loadrunner工具自己的函数整理了一个脚 ...

  9. git基础之常用操作

    一.版本提交: (1)git add 文件名 (2)git commit -m "版本提交信息" 注:git分两个区:工作区+版本库 在电脑中看到的文件夹就是工作区 有一个隐藏的. ...

  10. Linux下MySQL c++ connector示例

    最近在学习数据库的内容,起先是在windows下用mysql c++ connector进行编程,之所以选用c++而不是c的api,主要是考虑到c++ connector是按照JDBC的api进行实现 ...