对于本题,最短路,考虑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的更多相关文章

  1. Codeforces 杂题集 2.0

      记录一些没有写在其他随笔中的 Codeforces 杂题, 以 Problemset 题号排序   1326D2 - Prefix-Suffix Palindrome (Hard version) ...

  2. USACO 2.4 Cow Tours

    Cow Tours Farmer John has a number of pastures on his farm. Cow paths connect some pastures with cer ...

  3. 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 ...

  4. 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 ...

  5. POJ 2018 Best Cow Fences(二分+最大连续子段和)

    Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14601 Accepted: 4720 Desc ...

  6. POJ-2018 Best Cow Fences(二分加DP)

    Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10174 Accepted: 3294 Desc ...

  7. 洛谷 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 ...

  8. Cow Relays 【优先队列优化的BFS】USACO 2001 Open

    Cow Relays Time Limit: 1000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Tota ...

  9. [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 ...

随机推荐

  1. 你了解真正的 restful API 吗?

    本文原创地址,博客:https://jsbintask.cn/2019/03/20/api/restful-api-best-practices/(食用效果最佳),转载请注明出处! 前言 在以前,一个 ...

  2. java中怎么表现一对多

    链接:https://www.cnblogs.com/w-xibao/p/8183680.html 链接2:https://blog.csdn.net/C_time/article/details/8 ...

  3. 微软停止支持Windows 7 数百万台电脑将面临病毒等风险

    导读 微软给出的公告称,从2020年开始停止支持Windows 7操作系统,这意味着该公司不会再向数百万台电脑发布任何软件更新,包括可以防止网络攻击的软件补丁. 微软给出的公告称,从2020年开始停止 ...

  4. 【CSS属性#2】

    " 目录 一.盒子模型 二.外边距 margin 三.内填充 padding 四.浮动 float 五.清除浮动 clear 六.溢出 overflow 七.定位 position 1. 无 ...

  5. idea设置单行注释格式(包括配置文件)

    idea中文件单行注释默认在行首位置 修改方法:进入File-->Settings-->Editor-->Code Style-->Java,修改Code Generation ...

  6. ubuntu mysql允许root用户远程登录

    有两种方法: 一. 1.mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPT ...

  7. 解决chrome记住账号默认样式覆盖

    当谷歌游览器记住账号后,输入框的颜色会变为黄色,最直接的方法:加入以下代码 input:-webkit-autofill , textarea:-webkit-autofill, select:-we ...

  8. Nexus-vPC和STP BPDU

    1.为了交互vPC拓扑,STP机制被修改适应到vPC peer环境.2.对于vPC ports,只有主角色运行STP,换句话说,vPC下的STP由主角色设备控制.3.只有主角色设备在DP(指定端口)上 ...

  9. 疫情对国内5G发展的影响

    导读 2020年春节期间,“新型冠状病毒”引发了自SARS之后又一次全国性疫情,而世卫组织也将之列为国际关注的突发公共卫生事件,随后多国发布了针对中国的旅行警告和入境限制,从当前情况来看,疫情对中国各 ...

  10. Android读取权限

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <use ...