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. protoc 命令参数

    protoc 命令的获得 源码在 https://github.com/google/protobuf , 如果不想自己编译获得最新版本,则可以下载官方编译好的各个平台的,下载地址:https://g ...

  2. Good Bye 2013 C

    C. New Year Ratings Change time limit per test 1 second memory limit per test 256 megabytes input st ...

  3. EL简介

    一.EL简介 1.语法结构    ${expression}2.[]与.运算符    EL 提供.和[]两种运算符来存取数据.    当要存取的属性名称中包含一些特殊字符,如.或?等并非字母或数字的符 ...

  4. JSP三大指令

    JSP三大指令1.page-->最复杂,<%@page language = "java" info = "xxx" ..%>  *pageE ...

  5. MessageFormat不支持{

    转自 :http://zqc-0101.iteye.com/blog/1140140 MessageFormat用来格式化一个消息,通常是一个字符串,比如: String str = "I' ...

  6. Android开发--布局二

    1.Andrid:控件布局(表格布局)TableLayout 有多少个TableRow对象就有多少行, 列数等于最多子控件的TableRow的列数 直接在TableLayout加控件,控件会占据一行 ...

  7. A required class was missing while executing org.apache.maven.plugins:maven-war-plugin:2.1.1:war

    完美解决方案: http://stackoverflow.com/questions/18442753/a-required-class-was-missing-while-executing-org ...

  8. HeartBeat的一些介绍和功能上的一些总结

    HeartBeat的作用: 通过HeartBeat,可以将资源(IP以及程序服务等资源)从一台已经故障的计算机快速转移到另一台正常运转的机器上继续提供服务,一般称之为高可用的服务.在实际的生产应用场景 ...

  9. app.js ejs 转换为html

    var express = require('express');var path = require('path');var favicon = require('serve-favicon');v ...

  10. INNO SETUP 5.5.0以上版本中文语言包

    ; *** Inno Setup version 5.5.0+ Chinese messages ***;; To download user-contributed translations of ...