Bumped!

题目链接(点击)

Peter returned from the recently held ACM ICPC World Finals only to find that his return flight was overbooked and he was bumped from the flight! Well, at least he wasn’t beat up by the airline and he’s received a voucher for one free flight between any two destinations he wishes.

He is already planning next year’s trip. He plans to travel by car where necessary, but he may be using his free flight ticket for one leg of the trip. He asked for your help in his planning.

He can provide you a network of cities connected by roads, the amount it costs to buy gas for traveling between pairs of cities, and a list of available flights between some of those cities. Help Peter by finding the minimum amount of money he needs to spend to get from his hometown to next year’s destination!

Input

The input consists of a single test case. The first line lists five space-separated integers nn, mm, ff, ss, and tt, denoting the number of cities nn (0<n≤500000<n≤50000), the number of roads mm (0≤m≤1500000≤m≤150000), the number of flights ff (0≤f≤10000≤f≤1000), the number ss (0≤s<n0≤s<n) of the city in which Peter’s trip starts, and the number tt (0≤t<n0≤t<n) of the city Peter is trying to travel to. (Cities are numbered from 00 to n−1n−1.)

The first line is followed by mm lines, each describing one road. A road description contains three space-separated integers ii, jj, and cc (0≤i,j<n,i≠j0≤i,j<n,i≠j and 0<c≤500000<c≤50000), indicating there is a road connecting cities ii and jj that costs cccents to travel. Roads can be used in either direction for the same cost. All road descriptions are unique.

Each of the following ff lines contains a description of an available flight, which consists of two space-separated integers uu and vv (0≤u,v<n0≤u,v<n, u≠vu≠v) denoting that a flight from city uu to city vv is available (though not from vv to uu unless listed elsewhere). All flight descriptions are unique.

Output

Output the minimum number of cents Peter needs to spend to get from his home town to the competition, using at most one flight. You may assume that there is a route on which Peter can reach his destination.

Sample Input 1 Sample Output 1
8 11 1 0 5
0 1 10
0 2 10
1 2 10
2 6 40
6 7 10
5 6 10
3 5 15
3 6 40
3 4 20
1 4 20
1 3 20
4 7
45

Sample Input 2

Sample Output 2
8 11 1 0 5
0 1 10
0 2 10
1 2 10
2 6 40
6 7 10
5 6 10
3 5 15
3 6 40
3 4 20
1 4 20
1 3 30
4 7

思路:

开始只学过 普通的迪杰斯特拉+链式前向星 对这个题开始就是想先跑一遍当做最小值 然后把f张机票 每次使用一张再求出最短路 最后果断T了

没办法比赛完去学堆优化 其实和普通的没多大差别 只是使用了优先队列

学会之后就接着做这个题 结果卡在 23/25 了 以为是链式前向星不可以消边 就感觉用迪杰斯特拉消边不对(我也查了资料说vector和邻接矩阵方便消边)可vector 不会 所以就接着学

学完提交还是卡23了 就知道肯定是那个地方个人习惯问题

找了好长时间终于发现 是const int INF时候INF 超 int 了 (下次肯定好好审一审它)

顿时感觉……

不过挺开心的 还学会了vector存图 迪杰斯特拉堆优化 还自己把链式前向星的消边搞了出来

有时间就再写一下vector

AC代码:

1.链式前向星+迪杰斯特拉堆优化

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
typedef long long LL;
const int MAX=1e6;
const LL MAX1=1e10;
LL n;
LL head[MAX+5],flag[MAX+5],ans=0;
LL dis[MAX+5],vis[MAX+5];
struct note{
LL to;
LL len;
LL next;
}edge[MAX+5];
struct note1{
LL x;
LL y;
}edge1[MAX+5];
struct node{
LL to,len;
node(LL a,LL b){
to=b;
len=a;
}
friend bool operator < (node a,node b){ ///是<而不是< node
return a.len>b.len;
}
};
void addnode(LL u,LL v,LL w)
{
edge[ans].to=v;
edge[ans].len=w;
edge[ans].next=head[u];
flag[u]=head[u];
head[u]=ans++;
}
void allbegin()
{
memset(head,-1,sizeof(head));
ans=0;
}
priority_queue<node>q;
void diji(LL s)
{
for(LL i=0;i<=n;i++){ ///不能忘记将diji的数组初始化
dis[i]=MAX1;
vis[i]=0;
}
while(!q.empty()){
q.pop();
}
dis[s]=0;
q.push(node(0,s));
while(!q.empty()){
node num=q.top();
q.pop(); ///用完后把最上层去掉
if(vis[num.to]){
continue;
}
vis[num.to]=1;
for(LL i=head[num.to];~i;i=edge[i].next){
if((dis[edge[i].to]>dis[num.to]+edge[i].len)&&edge[i].len!=MAX1+1){
dis[edge[i].to]=dis[num.to]+edge[i].len; ///copy完改符号
q.push(node(dis[edge[i].to],edge[i].to));///将这个to点对应的len重新放入队列中
}
}
}
}
int main()
{
LL m,f,a1,a2;
scanf("%lld%lld%lld%lld%lld",&n,&m,&f,&a1,&a2);
allbegin();
for(LL i=0;i<m;i++){
LL u,v,w;
scanf("%lld%lld%lld",&u,&v,&w);
addnode(u,v,w);
addnode(v,u,w);
}
for(LL i=0;i<f;i++){
LL u,v;
scanf("%lld%lld",&edge1[i].x,&edge1[i].y);
}
diji(a1);
LL minn=dis[a2];
for(LL i=0;i<f;i++){
diji(edge1[i].x);
LL num=dis[edge1[i].y];
addnode(edge1[i].x,edge1[i].y,0);
diji(a1);
if(minn>dis[a2]){
minn=dis[a2];
}
if(num==MAX1){
edge[ans-1].len=MAX1+1;
}
else{
edge[ans-1].len=num;
}
}
printf("%lld\n",minn);
return 0;
}

2、vector+链式前向星堆优化

#include<stdio.h>
#include<vector>
#include<queue>
using namespace std;
typedef long long LL;
const LL INF=1e10;
const int MAX=150010; int n;
struct node{
int to;
LL len;
node(int _to=0,LL _len=0):to(_to),len(_len){ }
bool operator<(const node &r)const{
return len>r.len;
}
};
struct note{
int to;
LL len;
note(int _to=0,LL _len=0):to(_to),len(_len){}
};
int vis[MAX+5];
LL dis[MAX+5];
vector<note>v[MAX+5];
void diji(int s)
{
for(int i=0;i<MAX+5;i++){
vis[i]=0,dis[i]=INF;
}
priority_queue<node>q;
while(!q.empty()){
q.pop();
}
dis[s]=0;
q.push(node(s,0));
node num;
while(!q.empty()){
num=q.top();
q.pop();
int u=num.to;
if(vis[u]){
continue;
}
vis[u]=1;
for(int i=0;i<v[u].size();i++){
int to=v[u][i].to;
LL len=v[u][i].len;
if(!vis[to]&&dis[to]>dis[u]+len){
dis[to]=dis[u]+len;
q.push(node(to,dis[to]));
}
}
}
}
int main()
{
int m,f,a1,a2;
scanf("%d%d%d%d%d",&n,&m,&f,&a1,&a2);
for(int i=0;i<m;i++){
int u;
node num;
scanf("%d%d%lld",&u,&num.to,&num.len);
v[u].push_back(note(num.to,num.len));
v[num.to].push_back(note(u,num.len));
}
diji(a1);
LL minn=dis[a2];
for(int i=0;i<f;i++){
int u;
node num;
scanf("%d%d",&u,&num.to);
num.len=0;
v[u].push_back(note(num.to,num.len));
diji(a1);
minn = min(minn,dis[a2]);
v[u].erase(v[u].end()-1);
}
printf("%lld\n",minn);
return 0;
}

Bumped!【迪杰斯特拉消边、堆优化】的更多相关文章

  1. 最短路径-迪杰斯特拉(dijkstra)算法及优化详解

    简介: dijkstra算法解决图论中源点到任意一点的最短路径. 算法思想: 算法特点: dijkstra算法解决赋权有向图或者无向图的单源最短路径问题,算法最终得到一个最短路径树.该算法常用于路由算 ...

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

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

  3. 迪杰斯特拉算法(Dijkstra) (基础dij+堆优化) BY:优少

    首先来一段百度百科压压惊... 迪杰斯特拉算法(Dijkstra)是由荷兰计算机科学家狄克斯特拉于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有权图中最 ...

  4. 说说关于洛谷P4779迪杰斯特拉的堆优化

    众所周知,这题必须要用堆优化的迪杰斯特拉的堆优化才能过,否则60分(错失一等奖) 我没有得过一等奖但还是要说: P4779 全过程: struct node//堆中的比较函数 { int dis; i ...

  5. hdu2544 迪杰斯特拉题目优化

    点击打开题目链接 迪杰斯特拉的用法不多讲,详见  点击打开链接 . 下面两个代码: 这个是用邻接矩阵存图的迪杰斯特拉. #include<stdio.h> int main() { int ...

  6. Codeforces Gym 100342H Problem H. Hard Test 构造题,卡迪杰斯特拉

    Problem H. Hard TestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...

  7. 单源最短路径-迪杰斯特拉算法(Dijkstra's algorithm)

    Dijkstra's algorithm 迪杰斯特拉算法是目前已知的解决单源最短路径问题的最快算法. 单源(single source)最短路径,就是从一个源点出发,考察它到任意顶点所经过的边的权重之 ...

  8. 图的最短路径---迪杰斯特拉(Dijkstra)算法浅析

    什么是最短路径 在网图和非网图中,最短路径的含义是不一样的.对于非网图没有边上的权值,所谓的最短路径,其实就是指两顶点之间经过的边数最少的路径. 对于网图,最短路径就是指两顶点之间经过的边上权值之和最 ...

  9. 迪杰斯特拉和spfa

    迪杰斯特拉 Dijkstra算法是典型的算法.Dijkstra算法是很有代表性的算法.Dijkstra一般的表述通常有两种方式,一种用永久和临时标号方式,一种是用OPEN, CLOSE表的方式,这里均 ...

随机推荐

  1. 哈理工新生赛 马拉车+贪心 最大密度子图 AC自动机+DP

    J.Symmys Time Limit: 1000 MS Memory Limit: 262144 K Total Submit: 50 (13 users) Total Accepted: 2 (2 ...

  2. JAVA 基础知识。程序运方法。

    dos     常用命令    dir     查看此文件夹目录下的所有程序    cd..    返回上一层目录    盘符:  直接切换至相应的盘符    cd 目录 切换至指定的目录    cd ...

  3. Asp.net core logging 日志

    1 基本概念 Dotnet core 一个重要的特征是 Dependency injection ,中文一般是依赖注入,可以简单理解为一个集合,在应用程序启动时,定义各种具体的实现类型并将其放到集合中 ...

  4. spring cloud系列教程第六篇-Eureka集群版

    spring cloud系列教程第六篇-Eureka集群版 本文主要内容: 本文来源:本文由凯哥Java(kaigejava)发布在博客园博客的.转载请注明 1:Eureka执行步骤理解 2:集群原理 ...

  5. Python 每日一练(7)

    引言 今天的练习比较轻松,原本是有两题的,但是第一题那个大致看了一下,其实和之前的6个练习差不多,就是把xls中的文件数据读取出来后,进行一下处理,对于那题而言就是一个求和操作,所以就没练了,所以今天 ...

  6. RxJS 中的创建操作符

    RxJs 中创建操作符是创建数据流的起点,这些操作符可以凭空创建一个流或者是根据其它数据形式创建一个流. Observable的构造函数可以直接创建一个数据流,比如: const $source=ne ...

  7. 03 . 前端之JavaScipt

    JavaScript概述 ECMAScript和JavaScript的关系 1996年11月,JavaScript的创造者–Netscape公司,决定将JavaScript提交给国际标准化组织ECMA ...

  8. 这些Java8官方挖过的坑,你踩过几个?

    导读:系统启动异常日志竟然被JDK吞噬无法定位?同样的加密方法,竟然出现部分数据解密失败?往List里面添加数据竟然提示不支持?日期明明间隔1年却输出1天,难不成这是天上人间?1582年神秘消失的10 ...

  9. 前端和Nodejs的关系 简单理解

    前端使用JS脚本语言进行开发. JS脚本语言需要依赖一个平台运行,从而生成可视化的东西. Node.js提供这个平台,同时提供JS运行需要的一些插件.库.包.轮子.组件.功能等等. JavaScrip ...

  10. Jmeter(六) - 从入门到精通 - 建立数据库测试计划(详解教程)

    1.简介 在实际工作中,我们经常会听到数据库的性能和稳定性等等,这些有时候也需要测试工程师去评估和测试,因此这篇文章宏哥主要介绍了jmeter连接和创建数据库测试计划的过程,宏哥在文中通过示例和代码非 ...