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. pytorch数学运算与统计属性入门(非常易懂)

    pytorch数学运算与统计属性入门1.Broadcasting (维度)自动扩展,具有以下两个重要特征:(1)expand (2)without copying data重点的核心实现功能是:(1) ...

  2. vue中移动端调取本地的复制的文本

      _this.$vux.confirm.show({           title: '复制分享链接',           content: ‘分享的内容’,           onConfi ...

  3. drf大总结

    接口 """ 1.什么是接口:url+请求参数+响应数据 | 接口文档 2.接口规范: url:https,api,资源(名词复数),v1,get|post表示操作资源的 ...

  4. mybatis--实现数据库增删改查

    首先,创建一个数据库my,并在数据库中插入一张表user,然后在user表中插入一行数据,代码如下: create database my; use my; create table user( id ...

  5. centos平台搭建Oracle11g数据库+远程连接

    经过了几天的摸爬滚打,终于成功的能在宿主机上(window10上的Plsql)去成功的连上虚拟机上的centos数据库 下面将自己的经验分享给大家: 具体的centos7.centos6上安装Orac ...

  6. SpringAOP学习之5种通知

    一.Spring的AOP分为以下5种类型通知 ①前置通知(Before):在连接点执行前执行该通知 ②正常返回通知(AfterReturning):在连接点正常执行完后执行该通知,若目标方法执行异常则 ...

  7. Git添加和克隆远程库

    首先我们得有一个GitHub账号,然后把当前电脑的SSH Key添加到GitHub上面 第1步:创建SSH Key.在用户主目录下(可用 “cd ~”进入用户主目录),看看有没有.ssh目录,如果有, ...

  8. python+树莓派实现IoT(物联网)数据上传到服务器

    环境:raspbian-stretch(2018-06-27) 树莓派:3代B型 1.树莓派设备,需要在野外也能拥有独立联网能力,那必不可少的需要使用物联网模块. 这里使用的是微雪的SIM868通讯模 ...

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

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

  10. 【C语言】输入5个整数并按输入顺序逆序输出

    #include <stdio.h> int main() { ],i; printf("请输入5个整数:\n"); ;i<;i++) scanf("% ...