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
 
dijkstra代码:
 #include<stdio.h>
#define INF 0x3f3f3f3f
#define N 1010
int vis[N], dis[N], cost[N][N];
int n, m, s, w, p, q, t;
int min(int x, int y)
{ return x < y ? x : y;
}
void dijkstra(int beg)
{
int u, v;
for(u = ; u <= n; u++)
{
vis[u] = ;
dis[u] = INF;
}
dis[beg] = ;
while(true)
{
v = -;
for(u = ; u <= n; u++)
if(!vis[u] && (v == - || dis[u] < dis[v]))
v = u;
if(v == -)
break;
vis[v] = ;
for(u = ; u <= n; u++)
dis[u] = min(dis[u], dis[v] + cost[v][u]);
}
}
int main()
{
int i , j;
while(~scanf("%d%d%d", &n, &m, &s))
{
for(i = ; i <= n; i++)
for(j = i; j <= n; j++)
cost[i][j] = cost[j][i] = INF;
while(m--)
{
scanf("%d%d%d", &p, &q, &t);
if(cost[q][p] > t)
cost[q][p] = t;
}
scanf("%d", &w);
int sum = INF, b;
dijkstra(s);
for(i = ; i <= w; i++)
{
scanf("%d", &b);
if(sum > dis[b])
sum = dis[b];
} if(sum == INF)
printf("-1\n");
else
printf("%d\n", sum);
}
return ;
}

spfa代码:

 #include <stdio.h>
#include <string.h>
#include <queue>
#define N 10000000
#define M 1010
#define INF 0x3f3f3f3f
using namespace std;
int n, m, s, cnt;
int vis[M], head[M], time[M];
queue<int>q;
struct node
{
int from, to, cost, next;
}road[N];
void add(int x, int y, int z)
{
node e = {x, y, z, head[x]};
road[cnt] = e;
head[x] = cnt++;
}
void spfa()
{
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = ;
for(int i = head[u]; i != -; i = road[i].next)
{
int v = road[i].to;
if(time[v] > time[u] + road[i].cost)
{
time[v] = time[u] + road[i].cost;
if(!vis[v])
{
vis[v] = ;
q.push(v);
}
}
}
}
}
int main()
{
while(~scanf("%d%d%d", &n, &m, &s))
{ memset(head, -, sizeof(head));
memset(vis, , sizeof(vis));
memset(time, INF, sizeof(time));
while(m--)
{
int p, q, t;
scanf("%d%d%d", &p, &q, &t);
add(p, q, t);
//add(q, p, t);
}
int w;
scanf("%d", &w);
while(w--)
{
int posi;
scanf("%d", &posi);
q.push(posi);
time[posi] = ;
vis[posi] = ;
}
spfa();
if(time[s] == INF)
printf("-1\n");
else
printf("%d\n", time[s]);
}
return ;
}

hdoj 2680 choose the best route的更多相关文章

  1. hdu 2680 Choose the best route

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

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

  3. HDU 2680 Choose the best route(SPFA)

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

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

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

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

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

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

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

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

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

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

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

  9. HDU2680 Choose the best route 最短路 分类: ACM 2015-03-18 23:30 37人阅读 评论(0) 收藏

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

随机推荐

  1. Android API Guides 学习笔记---Application Fundamentals(一)

    今天开始学习google官网上的API guides ,主要读了Application Fundamentals这一章节,此章节介绍了一个App的基本组成,共包括四大部分内容. 1.      App ...

  2. PNG与iOS优化选项

    从App Store下载到的每一枚App最初都是一只IPA文件(其实是zip格式,内含特定规则的文件夹组织方式).但当作zip解开之后会发现里面很多的PNG文件看不了,这是因为在这些PNG图像都已被i ...

  3. restfull api

    REST 表示状态传输.这是一个体系结构样式,可用于设计网络服务,可以被各种客户端消耗.核心思想是,不使用如CORBA,RPC或SOAP复杂的机制在机器之间进行连接,简单的 HTTP 用于使它们之间调 ...

  4. JAVA设计模式之不变模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述不变(Immutable)模式的: 一个对象的状态在对象被创建之后就不再变化,这就是所谓的不变模式. 不变模式的结构 不变模式可增强对象的 ...

  5. [Git].gitignore失效的原因

    使用git管理源代码已经成为现在开源社区的一大选择. 开发的人都知道,在源代码管理中,我们需要监控和备份的是代码,而不是开发过程中生成的exe和dll文件.//即使在某些时候,我们需要某些dll,我们 ...

  6. 服务器间打通ssh无密钥

    1 打通无密钥 配置HDFS,首先就得把机器之间的无密钥配置上.我们这里为了方便,把机器之间的双向无密钥都配置上. (1)产生RSA密钥信息 ssh-keygen -t rsa 一路回车,直到产生一个 ...

  7. MFC使用TRACKMOUSEEVENT触发mouseHover和mouseLeave

    为对话框添加WM_MOUSEHOVER或WM_MOUSELEAVE消息并不会响应.MFC需要特殊处理,其中一法就是使用TRACKMOUSEEVENT void CmfcDlgDlg::OnMouseM ...

  8. DataAdapter与DataSet的使用

    1.创建数据库连接: 2.创建数据适配器(Adapter); 3.创建容器数据集(DataSet); 4.从数据集中取出指定表: 5.遍历表数据并输出: using System; using Sys ...

  9. Error : Must specify a primary resource (JAR or python or R file)

    spark-submit 报错:must specify resource 取消关注 | 1 ... 我的submit.sh内容: /bin/spark-submit \ --class abc.pa ...

  10. swift 代码添加image

    let image_ElectricianBtn = UIImage(named: "ElectricianBtn") let vimage_ElectricianBtn = UI ...