传送门

题目描述

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!

输入

The input consists of a single test case. The first line lists five space-separated integers n, m, f, s, and t, denoting the number of cities n (0 < n ≤ 50 000), the number of roads m (0 ≤ m ≤ 150 000), the number of flights f (0 ≤ f ≤ 1 000), the number s (0 ≤ s < n) of the city in which Peter’s trip starts, and the number t (0 ≤ t < n) of the city Peter is trying to travel to. (Cities are numbered from 0 to n − 1.)
The first line is followed by m lines, each describing one road. A road description contains three space-separated integers i, j, and c (0 ≤ i, j < n, i 6= j and 0 < c ≤ 50 000), indicating there is a road connecting cities i and j that costs c cents to travel. Roads can be used in either direction for the same cost. All road descriptions are unique.
Each of the following f lines contains a description of an available flight, which consists of two space-separated integers u and v (0 ≤ u, v < n, u 6= v) denoting that a flight from city u to city v is available (though not from v to u unless listed elsewhere). All flight descriptions are unique.

输出

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.

样例输入

复制样例数据

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

最多使用一张机票,那就跑f+1遍迪杰斯特拉,找出最小值。这一这里的Dijkstra一定要用优化过的,否则肯定会超时的。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e10;
const ll maxn = 150010;
struct node
{int v;ll c;node(int _v = 0,int _c = 0) : v(_v),c(_c){}bool operator < (const node &r) const{return c > r.c;}
};
struct Edge
{int v;ll cost;Edge(int _v = 0,ll _cost = 0) : v(_v),cost(_cost) {}
};
vector<Edge>E[maxn];
bool vis[maxn];
ll dist[maxn];
void Dijkstra(int n,int start)
{memset(vis,0, sizeof(vis));for(int i = 0;i < n; i++)dist[i] = inf;priority_queue<node>que;while(!que.empty())que.pop();dist[start] = 0;que.emplace(node(start,0));node tmp;while(!que.empty()){tmp = que.top();que.pop();int u = tmp.v;if(vis[u])continue;vis[u] = true;for(int i = 0;i < E[u].size(); i++){int v = E[u][i].v;ll cost = E[u][i].cost;if(!vis[v] && dist[v] > dist[u] + cost){dist[v] = dist[u] + cost;que.emplace(node(v,dist[v]));}}}
}
int main()
{
#ifndef ONLINE_JUDGEfreopen("in","r",stdin);
#endifios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int n,m,f,s,t;cin >> n >> m >> f >> s >> t;for(int i = 0;i < m; i++){int u,v,w;cin >> u >> v >> w;E[u].emplace_back(Edge(v,w));E[v].emplace_back(Edge(u,w));}ll minn = inf;Dijkstra(n,s);minn = min(minn,dist[t]);for(int i = 0;i < f; i++){int x,y;cin >> x >> y;E[x].emplace_back(Edge(y,0));Dijkstra(n,s);minn = min(minn,dist[t]);E[x].erase(E[x].end() - 1);}cout << minn << endl;return 0;
}

第六场问题 B: Bumped!(Dijkstra + 优先队列优化)相关推荐

  1. POJ 1511 Invitation Cards——Dijkstra优先队列优化+反向建图

    [题目描述] In the age of television, not many people attend theater performances. Antique Comedians of M ...

  2. NOI.AC NOIP模拟赛 第六场 游记

    NOI.AC NOIP模拟赛 第六场 游记 queen 题目大意: 在一个\(n\times n(n\le10^5)\)的棋盘上,放有\(m(m\le10^5)\)个皇后,其中每一个皇后都可以向上.下 ...

  3. dijkstra 的优先队列优化

    既然要学习算法,就要学习到它的精髓,才能够使用起来得心应手. 我还是远远不够啊. 早就知道,dijkstra 算法可以用优先队列优化,我却一直不知道该怎样优化.当时,我的思路是这样的:假设有n个顶点, ...

  4. 牛客第六场 H-Hopping Rabbit

    牛客第六场 H-Hopping Rabbit 给出平面上的n个矩形,一只兔子从(x0+0.5,y0+0.5)(x_0+0.5,y_0+0.5)(x0​+0.5,y0​+0.5)出发,每一次可以平行于x ...

  5. 2018南京网络赛L题 Magical Girl Haze(分层图+优先队列优化的dijkstra)

    使用优先队列优化过的dijkstra时间复杂度可以达到O(v*logn),还是很快的. #include <iostream>                //最好是用long long ...

  6. 2020牛客暑期多校训练营(第六场)

    2020牛客暑期多校训练营(第六场) 额,睡了一下午,直接错过了比赛... 文章目录 A African Sort 题意: 题解: 代码: B Binary Vector C Combination ...

  7. 杭电多校第六场个人补题6 7 9 10 12

    杭电多校第六场个人补题6 7 9 10 12 6 题意 给定一棵有n结点的树,点权为1~n,求对所有结点子树的mex操作和的最大值 思路 其实就是从最底部开始网上找,由于0是唯一的一个,所欲最好给在最 ...

  8. 单源最短路径 Dijkstra+优先队列

    用优先队列优化的Dijkstra 1找到最短距离已经确认的顶点,从它出发更新相邻顶点的最短距离 2此后不需要关心1中的"最短距离已经确认的顶点" 堆中元素共有O(V)个,更新和取出 ...

  9. 题解报告(CDUT暑期集训——第六场)

    题解报告(CDUT暑期集训--第六场) A - oval-and-rectangle HDU - 6362 思路:水题 积分一化就出来了 AC代码 #include<stdio.h> #i ...

  10. 10h+技术干货 | 我们整理了过去六场机器人创新生态系列研讨会总结+专家干货,一次性带你看过瘾!

    2020年已经步入下半年,至今为止英特尔智能机器人产学研生态合作系列研讨会已完成了6场研讨会,在这总时长10h+的深度研讨会上,从智能零售为起点,走过人工智能.工业4.0.机器人.数据中台等多个领域, ...

最新文章

  1. java独立承担,Java使用独立文件服务器
  2. Python自动化测试框架之Pytest教程【让你小鸡变老鹰】
  3. 极力推荐python初学者使用wingIDE
  4. yum的方式安装mysql_Linux安装mysql之yum安装方式
  5. 你在看Netflix,Netflix也在看你
  6. maya刷权重时有个叉_抖音账号养号技巧,如何增加抖音账号权重?
  7. 每个程序员都应该知道的基础数论
  8. 使用GDB命令行调试器调试C/C++程序
  9. 计算机控制系统a卷-答案,计算机控制系统2010-2011年试题A答案
  10. 云财经服务器维护,云财经服务器维护
  11. UI搜索栏设计素材模板|设计原则
  12. javaweb——jsp(学习总结,javaweb必备技能)
  13. hex文件分析+Qt5制作Hex文件转Bin文件的工具(含源码+工具下载)
  14. 用 Python 绘制污染物玫瑰图
  15. 如何使用GoldWave中文版进行声道分离?
  16. 《算法笔记》2.3小节——C/C++快速入门-选择结构
  17. UTF-8编码的原理
  18. ERP财务管理系统有哪些特点
  19. android 静态注册获取电量,获取手机电池百分比和电池容量方法
  20. 戴尔电脑最新bios设置图解介绍

热门文章

  1. 【Matlab三维路径规划】蚁群算法三维路径规划【含源码 179期】
  2. Gym 101778G
  3. 君莫笑:小白的堆(bai_dui)
  4. 小李飞刀:醉卧沙场君莫笑,python你还是等等我
  5. 企业微信裂变玩法有哪些?需要使用哪些工具?
  6. 论文笔记-Monocular Depth Estimation as Regression of Classification using Piled Residual Networks
  7. pandas模块DataFrame数据结构行数据的获取
  8. 操作系统 设备基本概念和分类
  9. 计算机默认登录用户名和密码是什么,Windows7默认管理员账户用户名和密码是什么...
  10. android 加载第三方so文件,Uni-app 以Module方式开发Android插件,引入第三方资源包so文件,但无法读取...