Codeforces1307D. Cow and Fields
对于本题,最短路,考虑bfs,那么我们可以跑2次bfs,求出每个点到1与n的最短路,设为x_a, x_b,那我们可以把问题转换成max(min{x_a+y_b,x_b+y_a}+1)(x,y属于1到n),我们假设x_a+y_b<=x_b+y_a,那我们就是求出x_a+y_b的最大值,不等式转换一下,x_a-x_b<=y_a-y_b,那我们可以根据该式sort一下,每次更新一下最大的x_a,然后x_a+y_b+1就是最大值
#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-x))
typedef long long LL;
typedef pair<int,int> pii; const int maxn = 2e5+; vector<int> G[maxn];
int d1[maxn], d2[maxn];
bool ks[maxn]; void bfs(int *dist, int s) {
dist[s] = ;
queue<int> q;
q.push(s);
while(!q.empty()) {
int u = q.front();
q.pop();
for(auto v : G[u]) {
if(dist[v] == -) {
q.push(v);
dist[v] = dist[u] + ;
}
}
}
} void run_case() {
int n, m, k;
cin >> n >> m >> k;
memset(d1, -, sizeof(d1)), memset(d2, -, sizeof(d2));
for(int i = ; i < k; ++i) {
int t; cin >> t;
ks[t] = true;
}
for(int i = ; i < m; ++i) {
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
bfs(d1, );
bfs(d2, n);
vector<pii> v;
for(int i = ; i <= n; ++i) if(ks[i]) v.emplace_back(d1[i], d2[i]);
sort(v.begin(), v.end(), [&](const pii &a, const pii &b) {
return a.first - a.second < b.first - b.second;
});
int ans = , mx = v[].first;
for(int i = ; i < v.size(); ++i) {
ans = max(ans, mx++v[i].second);
mx = max(mx, v[i].first);
}
cout << min(ans, d1[n]);
} int main() {
ios::sync_with_stdio(false), cin.tie();
//cout.setf(ios_base::showpoint);cout.precision(10);
//int t; cin >> t;
//while(t--)
run_case();
cout.flush();
return ;
}
Codeforces1307D. Cow and Fields的更多相关文章
- Codeforces 杂题集 2.0
记录一些没有写在其他随笔中的 Codeforces 杂题, 以 Problemset 题号排序 1326D2 - Prefix-Suffix Palindrome (Hard version) ...
- USACO 2.4 Cow Tours
Cow Tours Farmer John has a number of pastures on his farm. Cow paths connect some pastures with cer ...
- 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 线段树维护dp
题目 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 链接 http://www.lydsy.com/JudgeOnline/proble ...
- 4989: [Usaco2017 Feb]Why Did the Cow Cross the Road
题面:4989: [Usaco2017 Feb]Why Did the Cow Cross the Road 连接 http://www.lydsy.com/JudgeOnline/problem.p ...
- POJ 2018 Best Cow Fences(二分+最大连续子段和)
Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14601 Accepted: 4720 Desc ...
- POJ-2018 Best Cow Fences(二分加DP)
Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10174 Accepted: 3294 Desc ...
- 洛谷 P3659 [USACO17FEB]Why Did the Cow Cross the Road I G
//神题目(题目一开始就理解错了)... 题目描述 Why did the cow cross the road? Well, one reason is that Farmer John's far ...
- Cow Relays 【优先队列优化的BFS】USACO 2001 Open
Cow Relays Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tota ...
- [BZOJ4990][Usaco2017 Feb]Why Did the Cow Cross the Road II dp
4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II Time Limit: 10 Sec Memory Limit: 128 MBSubmi ...
随机推荐
- PHP固定长度字符串
/** * 获取固定长度随机字符串 * @param $n * @return string * @throws Exception */ function gf_rand_str($n) { if ...
- JAVA(2)之关于类的访问权限控制
类的成员的四种访问权限 private 只能在当前类中访问 无修饰 同一个包中的类都可以访问 protected 同一个包中的类可以访问 不同包中的子类可以访问 public 所有类都可以访问 示例代 ...
- 消息队列(五) ---RocketMQ-消息存储3
问题: consumeQueue 如何工作 刷盘机制如何工作 概述 该节我们将学习 consumeQueue 如何工作,先来看一下消息发送的大概过程. 而为什么需要 consumeQueue 的存在呢 ...
- 【C语言】赋值运算中的类型转换
#include<stdio.h> int main() { int a, b; double x = 1.54; char ch; a = x; x = ; b = 'a'; ch = ...
- 微信HTTP公告
- ZOJ 1002 Fire Net(dfs)
嗯... 题目链接:https://zoj.pintia.cn/problem-sets/91827364500/problems/91827364501 这道题是想出来则是一道很简单的dfs: 将一 ...
- vue项目打包后运行报错400如何解决
昨天一个Vue项目打包后,今天测试,发现无论localhost还是服务器上都运行不了,报错如下: Failed to load resource: the server responded with ...
- Git基础及进阶-系统总结
Git基础及进阶-系统总结 by 小强 2019-07-01 考虑到入职后不仅需要熟练掌握git的基本使用,在企业实际操作中还涉及一些进阶指令.作为一个程序员,熟练使用工具是一项基本技能,也是程序员的 ...
- Booth算法: 补码一位乘法公式推导与解析
以下讲解内容出自<计算机组成原理(第三版)>(清华大学出版社) 大二学生一只,我的计组老师比较划水,不讲公式推导,所以最近自己研究了下Booth算法的公式推导,希望能让同样在研究Booth ...
- 「CF891C」Envy
传送门 Luogu 解题思路 考虑最小生成树的几个性质: 所有最小生成树中边权相等的边的条数相等 在任意一颗最小生成树中,边权相等的边所联通的点集一定 那么我们考虑把边权相等的边单独拿出来考虑. 每次 ...