问题 B: Bumped!

时间限制: 1 Sec  内存限制: 128 MB
提交: 351  解决: 44
[提交] [状态] [命题人:admin]

题目描述

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

题意:n个点,m条边,f条飞行路线,s起始点,t终点

其中f条飞行路线可以使一条边的距离为0;

这题很坑,相当坑,有如下几点

1.  不管f有多少条 ,只能选一条 ,所以我们枚举每一条f边为0,去求最短路;

2. 如果f=0,别直接不跑了

3. 数据范围是long long ,如果初始化为INF 就会WA!!  太坑了! 改成9999999999或更多(在此WA了几百发,留下惨痛教训)

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define rep(i,a,n) for(int i=a;i<n;++i)
#define readc(x) scanf("%c",&x)
#define read(x) scanf("%d",&x)
#define sca(x) scanf("%d",&x)
#define read2(x,y) scanf("%d%d",&x,&y)
#define read3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define print(x) printf("%d\n",x)
#define mst(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&-x
#define lson(x) x<<1
#define rson(x) x<<1|1
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<ll,ll> P;
const int INF =0x3f3f3f3f;
const int inf =0x3f3f3f3f;
const int mod = 1e9+7;
const int MAXN = 105;
const int maxn =1000010;
using namespace std;
struct node{ll to;ll cost;bool operator < (node b)const{return cost > b.cost;}
};
struct edge{ll to;ll cost;
};
vector<edge> E[maxn];
int vis[maxn];
ll dis[maxn];
priority_queue<node>q;
ll x,y,z;
ll n,m,f,st,ed;void Dijkstra(ll s){memset(vis,0,sizeof vis);for(int i = 0; i < n; i++)dis[i] = 9999999999;while(!q.empty())q.pop();dis[s] = 0;q.push(node{s,0});node tmp;while(!q.empty()){tmp = q.top();q.pop();ll u = tmp.to;if(vis[u])continue;vis[u] = 1;for(int i = 0; i < E[u].size(); i++){ll v = E[u][i].to;ll w = E[u][i].cost;if(!vis[v] && dis[v] > dis[u] + w){dis[v] = dis[u] + w;q.push(node{v,dis[v]});}}}
}int main(){scanf("%lld%lld%lld%lld%lld",&n,&m,&f,&st,&ed);while(m--){scanf("%lld%lld%lld",&x,&y,&z);E[x].pb(edge{y,z});E[y].pb(edge{x,z});}ll ans = 9999999999;Dijkstra(st);ans = dis[ed];while(f--){scanf("%lld%lld",&x,&y);E[x].pb(edge{y,0});Dijkstra(st);ans = min(ans,dis[ed]) ;E[x].erase(E[x].end() - 1);}printf("%lld\n",ans);return 0;
}
/*
6 7 2 0 5
0 1 10
0 3 5
1 4 10
2 3 5
0 5 100
4 5 5
2 5 5
0 2
1 3
*/

Bumped!【最短路】(神坑相关推荐

  1. [C] [最短路] 只有5行的算法:Floyd-Warshall

    终于学到求最短路了,终于来到我最喜欢的算法--Floyd-Warshall了!今天还有点小激动呢! 我喜欢它,当然是因为它逻辑十分简单咯!真的只有5行诶! Floyd-Warshall算法 题目描述 ...

  2. BZOJ4152 AMPPZ2014 The Captain(最短路)

    事实上每次走到横坐标或纵坐标最接近的点一定可以取得最优方案.于是这样连边跑最短路就可以了. #include<iostream> #include<cstdio> #inclu ...

  3. Codeforces.1051F.The Shortest Statement(最短路Dijkstra)

    题目链接 先随便建一棵树. 如果两个点(u,v)不经过非树边,它们的dis可以直接算. 如果两个点经过非树边呢?即它们一定要经过该边的两个端点,可以直接用这两个点到 u,v 的最短路更新答案. 所以枚 ...

  4. BZOJ1491: [NOI2007]社交网络(Floyd 最短路计数)

    Time Limit: 10 Sec  Memory Limit: 64 MB Submit: 2343  Solved: 1266 [Submit][Status][Discuss] Descrip ...

  5. HDU1811 Rank of Tetris 拓扑排序+并查集 OR 差分约束最短路+并查集

    题目链接 题意:就是给你一堆关系,看能不能排出个确定的顺序 做法: 1. 拓扑排序+并查集 应该很容易想到的一种思路,大于小于建立单向边.对于相等的呢,就把他们缩成一个点.就用并查集缩成一个点就行了 ...

  6. E:By Elevator or Stairs? CF595 DP最短路

    题目链接 比赛的时候一看,这不是最短路吗,然后敲了一个最短路. 然后比赛完发现大家基本都写的dp,我真是个憨憨,dp3行 最短路就建个简单的图,dp就是从上一维转化过来就是了 优秀的dp: //#pr ...

  7. The Shortest Statement CodeForces - 1051F LCA+最短路

    太弱了... 一开始看到题感觉是跑一个最小生成树在上边进行LCA就行了,但是发现过不了样例,然后就是就想到了之前做过类似做法的题目,就是非生成树上的边最多只有21条,然后就那些边记录下来,通过每一条边 ...

  8. JZOJ #4722 跳楼机 (最短路模型的完美转化)

    题目描述: 给出$h,x,y,z$,求在$h$以内,$x,y,z$可以凑出多少个不同的数.$(1\leq{h}\leq{10^{18}},1\leq{x,y,z}\leq{10^5})$ 解题思路: ...

  9. matlab单机无限大系统_基于MATLAB的单机无穷大系统短路故障分析_吕鹏

    _______________________________电子技术__丝I 基于MA丁LAB的单机无穷大系统短路故障分析 山东科技大学吕鹏钟家成纪妮妮李漫漫 [摘要]本z l),NIATLAB7. ...

  10. 【二分答案】【最短路】bzoj1614 [Usaco2007 Jan]Telephone Lines架设电话线

    对于二分出的答案x而言,验证答案等价于将所有边权>x的边赋成1,否则赋成0,然后判断从1到n的最短路是否<=K. #include<cstdio> #include<cs ...

最新文章

  1. python3 Parallel Python
  2. java 禁止使用多线程_Java多线程(四)-线程状态的转换 - Java 技术驿站-Java 技术驿站...
  3. C++多小球非对心弹性碰撞(HGE引擎)
  4. Vagrant 手册之多个虚拟机 multi-machine
  5. 在C++的类中封装多线程
  6. [XSY] 简单的博弈题(博弈+dp+组合数+容斥)
  7. android 数据库模糊查询语句_单表千万行数据库:LIKE 搜索优化手记
  8. vscode 新版eslint自动修复_程序员请收好:10 个实用的 VS Code 插件
  9. 我的Linux系统的一些截图 之二
  10. Android Studio下导出jar包和aar包
  11. SQL Server数据库应用技术
  12. Echarts柱状图,如何基于数据进行百分比显示
  13. 百度自动php推送蜘蛛怎么不来访问,使用代码向百度蜘蛛主动推送链接
  14. 利用公网ip盒子搭建自己的kod云资源管理器
  15. Spring中的依赖注入(10级学员 韩晓爽课堂总结)
  16. 睡个“好”觉,还真是有益身心胖瘦的
  17. python抢票开发——设备预约助手实现
  18. FastRule: Efficient Flow Entry Updates for TCAM-based OpenFlow Switches(一)
  19. (二)zynq芯片是什么
  20. 如何将Nginx的版本号隐藏

热门文章

  1. 加权随机采样 (Weighted Random Sampling)
  2. Android中 手机震动功能的实现
  3. 电脑无法进入bios
  4. python爬虫爬取豆瓣电影评分排行榜前n名的前n页影评
  5. 《从前慢》 ----- 作者:木心
  6. Windows10系统EFI分区和OEM分区的删除方法
  7. 提取win10 锁屏壁纸
  8. Qt中使用TCP和MC协议与三菱Q系列PLC通信
  9. 《庄子·杂篇·天下第三十三》
  10. hive的join,left join,right join,full outer join,left semi join,cross join