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){ ///是<而不是< nodereturn 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. 迪杰斯特拉算法及其堆优化

    迪杰斯特拉算法及其堆优化 迪杰斯特拉算法是一种求解图的单点最短路径的算法. 一句话来说就是找最近点,更新相邻距离,再找最近点,更新相邻距离 迪杰斯特拉算法的原理是 1.首先在没有中间节点的情况下,也就 ...

  2. 堆优化版迪杰斯特拉(Dijkstra)算法简单分析

    堆优化版迪杰斯特拉算法: 优化原理: 上面的朴素版迪杰斯特拉算法主要缺陷是,每当找到一个最短路径,如果需要找下一个最短路径,就需要在完成松弛操作之后,遍历dist数组,寻找其中的最小值.遍历dist数 ...

  3. 【讲解 + 模板】Dijkstra迪杰斯特拉+堆优化

    Dijkstra迪杰斯特拉+堆优化 众所周知,朴素的迪杰斯特拉的时间复杂度为O(n^2),这在某些题目当中是会超时的.但如果在迪杰斯特拉中枚举每个最短边时加入堆优化,则迪杰斯特拉的效率则会大大提高. ...

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

    算法实现步骤: a.初始时,只包括源点,即S = {v},v的距离为0.U包含除v以外的其他顶点,即:U ={其余顶点},若v与U中顶点u有边,则(u,v)为正常权值,若u不是v的出边邻接点,则(u, ...

  5. dij算法堆优化_迪杰斯特拉算法(Dijkstra) (基础dij+堆优化) BY:优少(示例代码)...

    算法实现步骤: a.初始时,只包括源点,即S = {v},v的距离为0.U包含除v以外的其他顶点,即:U ={其余顶点},若v与U中顶点u有边,则(u,v)为正常权值,若u不是v的出边邻接点,则(u, ...

  6. 7-38 社交网络图中结点的“重要性”计算 (30分) 最短路 迪杰斯特拉堆优化

    在社交网络中,个人或单位(结点)之间通过某些关系(边)联系起来.他们受到这些关系的影响,这种影响可以理解为网络中相互连接的结点之间蔓延的一种相互作用,可以增强也可以减弱.而结点根据其所处的位置不同,其 ...

  7. 堆优化版迪杰斯特拉算法

    先是朴素版的迪杰斯特拉算法 存储结构为邻接矩阵 int dijstra(int n) {for (int i = 1; i <= n; i++){dist[1] = 0;int index = ...

  8. HDU 4725 层级最短路 思维建图 邻接表 堆优化迪杰斯特拉 真的难想 区域网络赛真题

    题目 题解思路 知道是最短路,怎么建图呢? 一开始想到每层来一个超级源点,但是方向不知道怎么确定,用双向边果然WA了(如果是双向边,那一层的都会变成0费连通了 ). 翻了翻 大佬的博客 大佬定义了一种 ...

  9. 迪杰斯特拉最全详解(朴素版,堆优化+邻接表存图/链式前向星存图)

    迪杰斯特拉 迪杰斯特拉算法分析 迪杰斯特拉(朴素版) 迪杰斯特拉堆优化(邻接表存图) 迪杰斯特拉堆优化(链式前向星存图) 最短路--spfa(链式前向星存图) 迪杰斯特拉算法分析 一般用三种数据结构存 ...

  10. usaco Sweet Butter(迪杰斯特拉(优先队列优化),bellman_ford算法模板)

    这题开始用没有优化的迪杰斯特拉喜闻乐见的超时了,然后我用bellmanford算法按理说时间复杂度更大但是书上说往往只要很短的时间就可以求出最短路. 所以我用了这个算法但是我对这个算法还是不熟套了模板 ...

最新文章

  1. 【基础不牢地动山摇】一遍记住 Java 面试中常用的八种排序算法与代码实现!...
  2. php直接修改excel,php如何修改excel
  3. 关于事件相关电位P300应用于视频游戏的研究
  4. AI:机器学习、深度学习在实际应用(工业应用)中的步骤流程框架、实际场景(案例)之详细攻略
  5. 8年运维大神总结:坚持4-3-2备份策略,删库也不怕
  6. mybatis generator生成example_[Springboot系列] SpringBoot与Mybatis结合
  7. B8.软件工程与设计模式
  8. P2920 [USACO08NOV]时间管理Time Management
  9. thinkphp框架知识点
  10. C语言算法-求两直线夹角计算公式
  11. 河南自考本科英语可用计算机代替,河南:自学考试改革方案出炉 专业课可代替英语课程...
  12. 硬件工程师的真实前途我说出来可能你们不信
  13. 【整理】爬取网页数据的方法汇总
  14. 运筹帷幄之中,决胜千里之外——运筹学1-3章
  15. 我们到底能从《别逗了,费曼先生》中学到什么?
  16. 社招两年半10个公司28轮面试面经
  17. 【光线追踪】 流程分析与实现的路径跟踪渲染器
  18. su必备插件_建模必备逆天Sketchup插件I
  19. MFC对话框中使用GDI画二维码
  20. VMware虚拟机中linux CentOS7上网联网,简单粗暴亲测有效

热门文章

  1. APP注册名称的一些问题
  2. 爱加密加密Android apk 使用步骤
  3. 【公众号】JAVA微信公众号技术大佬文章精选
  4. 【安全牛苑房弘】Kali Linux 环境熟悉
  5. Vue-电子签名(E-Signature)
  6. mermaid 饼图使用指南
  7. 没有桌面体验功能就不能进行图片打印报错解决
  8. bitbucket 预览html,BitBucket基本使用操作
  9. 正阅读微信小说分销系统-视频教程-1.渠道商-公众号配置-基础信息
  10. 删除磁盘分区 删除OEM分区