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

/************************************************************************/
/*
hdu Arbitrage
dijkstra算法
题目大意:dijkstra算法,求点与点之间最短距离。因为此题的起始点不定,所以可用
反向图来求得,终点确定,从终点出发,dijkstra算法,求出其他点到终点最小距离。
本题数据量较大,用floyd算法超时。
*/
/************************************************************************/ #include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <algorithm> using namespace std; #define MIN(a,b) a<b?a:b
#define MAX 0xfffffff const int N = ;
int maps[N][N];
int dj[N],vis[N];
int m,n,s,w,num,min_num; void build_map()
{
int p,q,t;
for (int i = ; i <= n; i++)
for (int j = i; j <= n; j++)
maps[i][j] = maps[j][i] = ((i==j)?:MAX); for (int i = ; i <= m; i++)
{
scanf("%d%d%d",&p,&q,&t);
if(maps[q][p] > t) maps[q][p] = t;//这里注意构建反向图
} for (int i = ; i <= n; i++)
dj[i] = MAX;
} /*
//此floyd算法超时
void floyud()
{
for (int k = 1; k <= n; k++)
{
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
maps[i][j] = MIN(maps[i][j],maps[i][k] + maps[k][j]);
}
}
*/
void DJ()
{
int cur = s;
int next,min;
dj[cur] = ;
while()
{
min = MAX;
vis[cur] = ;
for (int i = ; i <= n; i++)
{
if (vis[i] == )continue;
if (dj[i] > dj[cur] + maps[cur][i])
dj[i] = dj[cur] + maps[cur][i];
if (dj[i] < min)
{
min = dj[i];
next = i;
}
}
if ( min == MAX)break;
cur = next;
}
} int main()
{
while(scanf("%d%d%d",&n,&m,&s)!= EOF )
{
build_map();
memset(vis,,sizeof(vis));
DJ();
min_num = MAX;
scanf("%d",&w);
while(w--)
{
scanf("%d",&num);
if ( dj[num] < min_num)
min_num = dj[num];
}
if (min_num == MAX)
printf("-1\n");
else printf("%d\n",min_num);
}
return ;
}

hdu 2680 Choose the best route (dijkstra算法)的更多相关文章

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

  2. hdu 2680 Choose the best route

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

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

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

  4. HDU 2680 Choose the best route(SPFA)

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

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

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

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

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

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

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

  8. 【hdu 2544最短路】【Dijkstra算法模板题】

    Dijkstra算法 分析 Dijkstra算法适用于边权为正的情况.它可用于计算正权图上的单源最短路( Single-Source Shortest Paths, SSSP) , 即从单个源点出发, ...

  9. HDU 2068 Choose the best route

    http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one ...

随机推荐

  1. python之模块cmath

    # -*- coding: utf-8 -*-#python 27#xiaodeng#python之模块cmath #复数的数学函数,如log.tan.sin等函数用法,针对我目前的情况用途较少,暂不 ...

  2. 转 安装Nginx 1.2.0+PHP 5.4.3(FastCGI)+MySQL 5.5.24

    硬件环境:Dell PowerEdge 1950,4核Intel(R) Xeon(R) CPU E5410  @ 2.33GHz4G内存 系统环境:CentOS release 5.2,内核版本: 2 ...

  3. Jenkins执行批处理文件失败

    今天搭建Jenkins持续集成环境,编译环境是.net,在.net下没有比较好的代码覆盖率测试插件,所以用了开源的OpenCover,计算代码覆盖率,然后用ReportGenerator导出hmtl格 ...

  4. Ubuntu下的init.d管理update-rc.d

    计算机在启动的时候会自动执行一些脚本,用于启动一些应用程序服务,update-rc.d 是管理这些脚本的常用命令之一. 首先这是 LinuxQuestions 中对 update-rc.d 的定义: ...

  5. 严重: The web application [] registered the JDBC driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDB

    idea项目启动报如下错误, 网上的方法都试了都没用, 一直没解决, 干掉项目, 重新从svn检出就好了...坑 啊 Root WebApplicationContext: initializatio ...

  6. LUA返回的是引用

    ,} function t1.Show() print("t1 show") end function GetT() return t1 end local t2 = GetT() ...

  7. C语言open()函数:打开文件函数(转)

    相关函数:read, write, fcntl, close, link, stat, umask, unlink, fopen 头文件:#include <sys/types.h>   ...

  8. fedora下安装运行keil uVision 4 (MDK v4.7)

    先准备好mdk4.73.exe和和谐文件. 1.安装 wine 1.7 添加ppa sudo add-apt-repository ppa:ubuntu-wine/ppa      安装wine 1. ...

  9. MFC的DLL中实现定时器功能

    方法一:创建一个线程, 反复读系统时间不就可以了? 如果定时要求不严,用Sleep就可以了.DWORD WINAPI TimerThread(LPVOID pamaram) { UINT oldTic ...

  10. MachineLearning之Logistic回归

    一.概述 假设现在有一些数据点,我们用一条直线对这些点进行拟合(该线称为最佳拟合直线),这个拟合过程就称为回归: 利用Logistic回归进行分类的主要思想是: 根据现有数据对分类边界线建立回归公式, ...