P1462 通往奥格瑞玛的道路

提交 29.89k
通过 6.88k
时间限制 1.00s
内存限制 125.00MB
题目提供者gconeice
历史分数100

提交记录

查看算法标签
进入讨论版

相关讨论

 
查看讨论

推荐题目

 
查看推荐
 

展开

题目背景

在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量

有一天他醒来后发现自己居然到了联盟的主城暴风城

在被众多联盟的士兵攻击后,他决定逃回自己的家乡奥格瑞玛

题目描述

在艾泽拉斯,有n个城市。编号为1,2,3,...,n。

城市之间有m条双向的公路,连接着两个城市,从某个城市到另一个城市,会遭到联盟的攻击,进而损失一定的血量。

每次经过一个城市,都会被收取一定的过路费(包括起点和终点)。路上并没有收费站。

假设1为暴风城,n为奥格瑞玛,而他的血量最多为b,出发时他的血量是满的。

歪嘴哦不希望花很多钱,他想知道,在可以到达奥格瑞玛的情况下,他所经过的所有城市中最多的一次收取的费用的最小值是多少。

输入格式

第一行3个正整数,n,m,b。分别表示有n个城市,m条公路,歪嘴哦的血量为b。

接下来有n行,每行1个正整数,fi。表示经过城市i,需要交费fi元。

再接下来有m行,每行3个正整数,ai,bi,ci(1<=ai,bi<=n)。表示城市ai和城市bi之间有一条公路,如果从城市ai到城市bi,或者从城市bi到城市ai,会损失ci的血量。

输出格式

仅一个整数,表示歪嘴哦交费最多的一次的最小值。

如果他无法到达奥格瑞玛,输出AFK。

输入输出样例

输入 #1
  1. 4 4 8
  2. 8
  3. 5
  4. 6
  5. 10
  6. 2 1 2
  7. 2 4 1
  8. 1 3 4
  9. 3 4 3
输出 #1
  1. 10

说明/提示

对于60%的数据,满足n≤200,m≤10000,b≤200

对于100%的数据,满足n≤10000,m≤50000,b≤1000000000

对于100%的数据,满足ci≤1000000000,fi≤1000000000,可能有两条边连接着相同的城市。

题意:找到一条从 1 到 n 的路,且满足路上在某个点交过路费的最大值要最小。

 
思路
  因为血量有限而钱无限,所以显然我们是要找一条能活着走完的路,并且还要满足要求。
  所有类似于“求解所有的最大值中的最小值”的问题,并且满足单调,都可以想到 二分法。
  把扣血量看作边长,在dijkstra的过程中,对于更新dis时,需要再添加一个限制条件 f [i] <= mid 来逐渐放宽我们可以走的点的范围,以此来保证一定可以走出一条血不会被扣完且收费的最大值最小的一条路。
 
CODE
  1. #include <bits/stdc++.h>
  2. #define dbg(x) cout << #x << "=" << x << endl
  3. #define eps 1e-8
  4. #define pi acos(-1.0)
  5.  
  6. using namespace std;
  7. typedef long long LL;
  8.  
  9. template<class T>inline void read(T &res)
  10. {
  11. char c;T flag=;
  12. while((c=getchar())<''||c>'')if(c=='-')flag=-;res=c-'';
  13. while((c=getchar())>=''&&c<='')res=res*+c-'';res*=flag;
  14. }
  15.  
  16. namespace _buff {
  17. const size_t BUFF = << ;
  18. char ibuf[BUFF], *ib = ibuf, *ie = ibuf;
  19. char getc() {
  20. if (ib == ie) {
  21. ib = ibuf;
  22. ie = ibuf + fread(ibuf, , BUFF, stdin);
  23. }
  24. return ib == ie ? - : *ib++;
  25. }
  26. }
  27.  
  28. int qread() {
  29. using namespace _buff;
  30. int ret = ;
  31. bool pos = true;
  32. char c = getc();
  33. for (; (c < '' || c > '') && c != '-'; c = getc()) {
  34. assert(~c);
  35. }
  36. if (c == '-') {
  37. pos = false;
  38. c = getc();
  39. }
  40. for (; c >= '' && c <= ''; c = getc()) {
  41. ret = (ret << ) + (ret << ) + (c ^ );
  42. }
  43. return pos ? ret : -ret;
  44. }
  45.  
  46. const int maxn = 1e5 + ;
  47. const int inf = 0x3f3f3f3f;
  48.  
  49. int head[maxn << ], edge[maxn << ], w[maxn << ], nxt[maxn << ];
  50. int cnt, b;
  51. int f[maxn << ];
  52.  
  53. inline void BuildGraph (int u, int v, int c) {
  54. cnt++;
  55. edge[cnt] = v;
  56. nxt[cnt] = head[u];
  57. w[cnt] = c;
  58. head[u] = cnt;
  59. }
  60.  
  61. struct node {
  62. int u,v;
  63. bool operator <(const node &t) const {
  64. return u > t.u;
  65. }
  66. };
  67.  
  68. int n,m,s,t,maxx;
  69. int dis[maxn];
  70.  
  71. priority_queue < node > q;
  72.  
  73. bool check(int x) {
  74. if(x < f[])
  75. return ;
  76. for (int i = ; i <= n; ++i) {
  77. dis[i] = inf;
  78. }
  79. dis[] = ;
  80. node temp;
  81. temp.u = ;
  82. temp.v = ;
  83. q.push(temp);
  84. while(!q.empty()) {
  85. int u = q.top().v;
  86. int d = q.top().u;
  87. q.pop();
  88. if(d != dis[u]) continue;
  89. for (int i = head[u]; i; i = nxt[i]) {
  90. int v = edge[i];
  91. int c = w[i];
  92. if(dis[v] > dis[u] + c && f[v] <= x) {
  93. dis[v] = dis[u] + c;
  94. node p;
  95. p.u = dis[v], p.v = v;
  96. q.push(p);
  97. }
  98. }
  99. }
  100. if(dis[n] < b) {
  101. return ;
  102. }
  103. return ;
  104. }
  105.  
  106. int main()
  107. {
  108. scanf("%d %d %d",&n,&m,&b);
  109. for (int i = ; i <= n; ++i) {
  110. scanf("%d",&f[i]);
  111. maxx = max(maxx, f[i]);
  112. }
  113. for (int i = ; i <= m; i++) {
  114. int u, v, c;
  115. scanf("%d %d %d",&u,&v,&c);
  116. BuildGraph(u, v, c);
  117. BuildGraph(v, u, c);
  118. }
  119. if(!check(inf)) {
  120. puts("AFK");
  121. return ;
  122. }
  123. int l = , r = inf, mid = (l + r) >> ;
  124. while(l <= r) {
  125. if(check(mid)) {
  126. r = mid - ;
  127. mid = (l + r) >> ;
  128. }
  129. else {
  130. l = mid + ;
  131. mid = (l + r) >> ;
  132. }
  133. }
  134. printf("%d\n",l);
  135.  
  136. return ;
  137. }

P1462 通往奥格瑞玛的道路【二分+Dij】的更多相关文章

  1. P1462 通往奥格瑞玛的道路 (二分+最短路)

    题目 P1462 通往奥格瑞玛的道路 给定\(n\)个点\(m\)条边,每个点上都有点权\(f[i]\),每条边上有边权,找一条道路,使边权和小于给定的数\(b\),并使最大点权最小. 解析 二分一下 ...

  2. [Luogu P1462] 通往奥格瑞玛的道路 (二分答案+最短路径)

    题面 传送门:https://www.luogu.org/problemnew/show/P1462 Solution 这道题如果去除掉经过城市的收费.那么就是裸的最短路 但是题目要求经过城市中最多的 ...

  3. 洛谷 - P1462 - 通往奥格瑞玛的道路 - 二分 - Dijkstra

    https://www.luogu.org/problem/P1462 感觉,要二分最大收费权的城市,把小于等于它的全部插进去,Dijkstra一下求出最小的血量.这样感觉太暴力了. 考虑只有1000 ...

  4. 洛谷P1462 通往奥格瑞玛的道路[二分答案 spfa 离散化]

    题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡奥格瑞玛 题目描述 在艾泽拉斯, ...

  5. 洛谷P1462通往奥格瑞玛的道路——二分答案最短路

    题目:https://www.luogu.org/problemnew/show/P1462 最大值最小问题,二分答案. 代码如下: #include<iostream> #include ...

  6. 洛谷 P1462 通往奥格瑞玛的道路——二分+spfa

    上一波链接 https://www.luogu.org/problem/P1462 这道题我们考虑二分答案 然后每次跑一次spfa判断是否能够到达n点 tips:在不考虑负权边的前提下我们写最短路最好 ...

  7. Luogu P1462 通往奥格瑞玛的道路 二分答案+最短路

    先二分答案,再跑最短路,跑的时候遇到 过路费超过二分的答案的 就不拿他更新最短路 #include<cstdio> #include<iostream> #include< ...

  8. 洛谷 P1462 通往奥格瑞玛的道路 二分 最短路

    #include<cstdio> #include<queue> #include<cstring> #include<algorithm> using ...

  9. 洛谷P1462 通往奥格瑞玛的道路(二分+spfa,二分+Dijkstra)

    洛谷P1462 通往奥格瑞玛的道路 二分费用. 用血量花费建图,用单源最短路判断 \(1\) 到 \(n\) 的最短路花费是否小于 \(b\) .二分时需要不断记录合法的 \(mid\) 值. 这里建 ...

随机推荐

  1. 使用Java实现三个线程交替打印0-74

    使用Java实现三个线程交替打印0-74 题目分析 三个线程交替打印,即3个线程是按顺序执行的.一个线程执行完之后,唤醒下一个线程,然后阻塞,等待被该线程的上一个线程唤醒.执行的顺序是一个环装的队列 ...

  2. Thread Based Parallelism - Thread Synchronization With a Condition

    Thread Based Parallelism - Thread Synchronization With a Condition from threading import Thread, Con ...

  3. malloc返回地址的对齐问题

    http://man7.org/linux/man-pages/man3/malloc.3.html RETURN VALUE         top The malloc() and calloc( ...

  4. [CSS]三大特性之一继承性、层叠性、优先级

    <style> div { color: red; font-size: 30px; {#background: #0066ff;#} } </style> <!-- 1 ...

  5. WebSocket协议分析

    WebSocket协议分析 1.什么是WebSocket协议 WebScoket协议是基于TCP协议建立的全双工通信,所谓的全双工通信就是双向同时通信. 2.WebSocket协议优点 WebSock ...

  6. windows下修改tomcat的startup.bat脚本文件后台运行

    1.修改startup.bat文件 rem Get remaining unshifted command line arguments and save them in the set CMD_LI ...

  7. mitmproxy--Cannot establish TLS with client (sni: e.crashlytics.com): TlsException("(-1, 'Unexpected EOF')",) 解决办法

    按崔哥(https://cuiqingcai.com/5391.html)的安装步骤一步步下来,会报这个错误: Cannot establish TLS with client (sni: e.cra ...

  8. 带输入提示的搜索框ajax请求

    先放图 首先要引用的文件有: base.css  https://www.cnblogs.com/chenyingying0/p/12363689.html jquery.js transition. ...

  9. Android中使用getDrawable时提示:Call requires API level 21(current min is 15)

    场景 在通过getDrawable方法获取照片资源时提示: Call requires API level 21(current min is 15) 注: 博客: https://blog.csdn ...

  10. LeetCode 57. Insert Interval 插入区间 (C++/Java)

    题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...