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 ... 
随机推荐
- 我的18vps~
			自从买了18vps的香港虚拟主机后,就面临一个问题,浏览器无法访问它的apache服务,后来发现,需要同时开启nginx服务: /usr/local/nginx/sbin/nginx -c /usr/ ... 
- Python函数基础进阶
			函数参数的另一种使用方式 def print_info(name,age): print("Name: %s" %name) print("age: %d" % ... 
- Go类型断言
			package main import ( "fmt" ) type Point struct { x int y int } func main() { var a interf ... 
- 「JSOI2015」salesman
			「JSOI2015」salesman 传送门 显然我们为了使收益最大化就直接从子树中选大的就好了. 到达次数的限制就是限制了可以选的子树的数量,因为每次回溯上来都会减一次到达次数. 多种方案的判断就是 ... 
- Top 9  colleges in the world from 2010 to 2020, AI and interdisciplinary areas.
			http://csrankings.org/ 
- 安装go1.11.2
			1. 设置go环境变量 vim $HOME/.bashrc export GOROOT=$HOME/go export PATH=$PATH:$GOROOT/bin export GOPATH=$HO ... 
- Druid数据源SQL数据库与Spring监控
			Druid监控概要说明 为什么要监控? Druid是什么?德鲁伊 URL监控配置说明 配置步骤 步骤 配置 第一步 web.xml 配置 WebStatFilter 第二步 WebStatFilter ... 
- python opencv:图像的一些属性与操作
			img = cv.imread(xxx) # 常用的有以下属性 type(img) # img的数据类型 img.shape # img的结构 img.size # img的大小 img.dtype ... 
- Python os模块、os.path模块常用方法
			os模块:os模块在python中包含普遍的操作系统功能,下面列出了一些在os模块中比较有用的部分. os.sep 可以取代操作系统特定的路径分隔符.windows下为 "\" o ... 
- 【快学Docker】快速创建容器,容器常用命令
			前言 容器是Docker的三大核心概念之一.简单地说,容器是独立运行的一个或一组应用,以及它们的运行态环境.对应的,虚拟机可以理解为模拟运行的一整套操作系统(提供了运行态环境和其他系统环境)和跑在上面 ... 
