Choose the best route

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5273    Accepted Submission(s): 1680

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
 

虽然又是迪杰斯特拉算法,但是还是搬到博客上来写,是因为解题思想挺牛的。

节点为10^3,本来用迪杰斯特拉,就算没优化,也是没什么问题的,可是题目指定了终点,又给定了一系列点作为起点,我估摸着,即使代码中间优化剪枝,如果数据够强力,还是有TLE危险。(后来即使用n^2算法也跑了400+ms,所以如果按这个思路几乎是TLE的命)。

后来大神博客介绍了两种思路。。瞬间就把时间复杂度降低了。。简直碉堡了

思路1、添加超级源点。这也是我之后选择的方法

即定义一个0号点,d[0]=0,并且v[0][j]=0,j为题目指定的一系列初始点坐标,使得将那一系列起始点都统一成了一个超级源点,但是因为把v[0][j]都设置为0,所以不会对结果产生任何影响,这样,只要照着朴素的迪杰斯特拉算法敲一遍,遍历n+1个点,即可得到结果。

思路2.逆向图

因为题目不是指定了最终点只是一个点吗?何不逆向从终点出发,这样,只是把循环反转了一下,照样是朴素的迪杰斯特拉算法。。最后比较一下,出发的那一系列点的数值即可。

我用的是思路一,思路二我大概想了一下,不会太难写,除了逆向的时候注意一下细节。。对了,必须提醒一下!!!该题目的图为单向图!!!多向图就会完蛋!!!!,HDU的discuss里面说是因为英语不好,但是我仔细再读了一下,也没发现哪里说了是单向的。。而且尼玛,我严重怀疑出题者是不是自己忘了弄双向,根据生活经验,尼玛哪个城市的公交是只去不回的!!!!。。。还有必须吐槽一下今天做的其他几个最短路的题目,还都是HDU的,都尼玛是坑货题意,明明两点存在多条路径,需要在读入的时候,判断一下,。。但题目愣是一点都没说。。幸好有discuss里面早就被坑死的人提示了这里,否则我还不知道要WA多少次。。。

好了  不废话了、、、

#include <iostream>
#include <cstdio>
#include <cstring>
#define inf 100000000
using namespace std;
int d[];
int vis[];
int v[][];
int n,m,s;
int stapoint[];
int w;
int dijst()
{
int i,j,k;
memset(vis,,sizeof vis);
d[]=;//设置超级源点
for (i=; i<=w; i++)
{
v[][stapoint[i]]=; //将超级源点与已知一系列初始点的路程设置为0;
}
for (j=; j<=n; j++)
{
int mini=inf;
int loc=;
for (k=; k<=n; k++)
if (mini>d[k]&&!vis[k])
mini=d[k],loc=k;
if (mini==inf) break;
vis[loc]=;
for (k=; k<=n; k++)
{
if (d[k]>d[loc]+v[loc][k]) d[k]=d[loc]+v[loc][k];
//cout<<k<<" "<<d[k]<<endl;
}
}
if (d[s] < inf) return d[s];
else
return -;
}
int main()
{
while (scanf("%d %d %d",&n,&m,&s)!=EOF)
{
int i,j,k;
for (i=; i<=n; i++)
{
d[i]=inf;
for (j=; j<=n; j++)
v[i][j]=inf;
}
for (i=; i<=m; i++)
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
if (v[a][b]>c)
v[a][b]=c;
//cout<<v[a][b]<<endl;
}
scanf("%d",&w);
for (i=; i<=w; i++)
scanf("%d",&stapoint[i]);
printf("%d\n",dijst());
}
return ;
}

HDU 2680 最短路 迪杰斯特拉算法 添加超级源点的更多相关文章

  1. HDU6166-Senior Pan-Dijkstra迪杰斯特拉算法(添加超源点,超汇点)+二进制划分集合-2017多校Team09

    学长好久之前讲的,本来好久好久之前就要写题解的,一直都没写,懒死_(:з」∠)_ Senior Pan Time Limit: 12000/6000 MS (Java/Others)    Memor ...

  2. 最短路——迪杰斯特拉算法 HDU_3790

    初识最短路,今天只弄了一个迪杰斯特拉算法,而且还没弄成熟,只会最基本的O(n^2),想弄个优先队列都发现尼玛被坑爆了,那个不应该用迪杰斯特拉算法写 表示还是不会优化版的迪杰斯特拉算法,(使用优先队列) ...

  3. HUD 2544 最短路 迪杰斯特拉算法

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

  4. 图(最短路径算法————迪杰斯特拉算法和弗洛伊德算法).RP

    文转:http://blog.csdn.net/zxq2574043697/article/details/9451887 一: 最短路径算法 1. 迪杰斯特拉算法 2. 弗洛伊德算法 二: 1. 迪 ...

  5. HDU 2544最短路 (迪杰斯特拉算法)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Time Limit: 5000/1000 MS (Java/Others)    Me ...

  6. HDU 3790(两种权值的迪杰斯特拉算法)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3790 最短路径问题 Time Limit: 2000/1000 MS (Java/Others)    ...

  7. HDU 1874畅通工程续(迪杰斯特拉算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others)     ...

  8. 【算法杂谈】LJX的迪杰斯特拉算法报告

    迪杰斯特拉(di jie qi)算法 这里有一张图: 假设要求从1号节点到5号节点的最短路.那么根据迪杰斯特拉算法的思想,我们先看: 节点1,从节点1出发的一共有3条路,分别是1-6.1-3.1-2. ...

  9. 最短路径之迪杰斯特拉算法的Java实现

    Dijkstra算法是最短路径算法中为人熟知的一种,是单起点全路径算法.该算法被称为是“贪心算法”的成功典范.本文接下来将尝试以最通俗的语言来介绍这个伟大的算法,并赋予java实现代码. 一.知识准备 ...

随机推荐

  1. stl队列

    队列(Queue)也是一种运算受限的线性表,它的运算限制与栈不同,是两头都有限制,插入只能在表的一端进行(只进不出),而删除只能在表的另一端进行(只出不进),允许删除的一端称为队尾(rear),允许插 ...

  2. 吴裕雄 python 机器学习——伯努利贝叶斯BernoulliNB模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets,naive_bayes from skl ...

  3. Catalyst9K设备介绍

    Catalyst9K系列的里面包含了多款交换机,以及无线控制器,甚至包含了无线AP,如下将简单的介绍这几款产品的情况: 首先,这是一种总体的对应关系: 1.Catalyst9200 Series 主要 ...

  4. Spring - 周边设施 - H2 数据库启动时写入数据

    1. 概述 之前讲到了 H2 的引入 这下我想说说 H2 启动时的 数据导入 2. 场景 需求 启动项目后, H2 启动起来 环境数据会自动注入 H2 数据库 可以验证是否成功 3. 环境 os wi ...

  5. Python的几种主动结束程序方式

    1. sys.exit() 执行该语句会直接退出程序,这也是经常使用的方法,也不需要考虑平台等因素的影响,一般是退出Python程序的首选方法. 该方法中包含一个参数status,默认为0,表示正常退 ...

  6. 你知道for(;;) vs. while(true)那个更快吗?

    来来来, for(;;) vs. while(true) 有什么区别?从java的语义上来说,他们是一模一样的.为何怎么说? 开始我们先测试for(;;) package com.tony.test; ...

  7. drf基础知识01

    drf框架 """ 接口: 接口规范: drf生命周期: 序列化组件: 三大认证组件: 过滤.筛选.排序.分页组件: 请求.响应.解析.异常模块: jwt: " ...

  8. 激活4500-X RTU license

    1.查看设备license Switch#sho version Cisco IOS Software, IOS-XE Software, Catalyst 4500 L3 Switch Softwa ...

  9. 异常的jvm(java虚拟机)与异常处理try catch与throwable

  10. 学习笔记(22)- plato-训练端到端的模型

    原始文档 Train an end-to-end model To get started we can train a very simple model using Ludwig (feel fr ...