Codeforces Round #621 (Div. 1 + Div. 2)D dij(思维)
题:https://codeforces.com/contest/1307/problem/D
题意:给定无向图,n为点,m为边。在给个k,为特殊点的数目,题目要求在这些特殊点上连一条边,让新图最短路尽可能大,问新图最短路(1到n)是多少?
分析:因为题目保证连通且原本的图一定可以从1到n,我们假设原本的图最短路为ans;
易得虽然要求我们最大化最短路,但是加一条边只可能让答案不变或变小(因为一定要加这条边),不会使最短路变大;
假设我们在特殊点x和y之间加了边,要是新图的最短路有走这条边,那么新图的最短路长度一定是p[x].t1+p[y].tn+1,p[].t1表示原本图以1为起点的最短路数组,p[].tn则是以n为起点;
这里有个问题:那么是不是找到最大的t1和最大的tn相加就能得到最短路的?这个结论的错误的,有个简单的反例,假设图为一条链(1为链头,n为链尾),那么最大的t1和tn肯定就是n,长度为2(n-1)+1,但是点1和点n连起来后最短路就变成了1,而不是2(n-1)+1,这样的情况在更大的图中更有体现
所以我们这里采用min(p[x].t1+p[y].tn+1,p[y].t1+p[x].tn+1),因为最短路,所以是要取其中较小的,化简一下就是p[x].t1-p[x].t2<p[y].t1-p[y].t2,取值小的x就作为1号点,y作为2号点,在代码中就体现为取max前面的t1,和当前的t2相加,取max到答案上;
处理方法就是将k个点按照这个优先级来排序,那么当前的p[i].tn和i之前的t1组成的长度就是我们要的最短路,然后就贪心地把t1取成 i 之前最大的ti就行
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int MAXN=;
int t1[MAXN],t2[MAXN];
struct qnode{
int v;
int c;
qnode(int _v=,int _c=):v(_v),c(_c){}
bool operator <(const qnode &r)const{
return c>r.c;
}
};
struct Edge{
int v,cost;
Edge(int _v=,int _cost=):v(_v),cost(_cost){}
};
vector<Edge>E[MAXN];
bool vis[MAXN];
int dist[MAXN],vis1[MAXN],vis2[MAXN],pre[MAXN];
void Dij(int n,int start){
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++)
dist[i]=INF;
priority_queue<qnode>que;
while(!que.empty())que.pop();
dist[start]=;
que.push(qnode(start,));
qnode tmp;
while(!que.empty()){
tmp=que.top();
que.pop();
int u=tmp.v;
if(vis[u])continue;
vis[u]=true;
for(int i=;i<E[u].size();i++){
int v=E[tmp.v][i].v;
int cost=E[u][i].cost;
if(!vis[v]&&dist[v]>dist[u]+cost){
dist[v]=dist[u]+cost;
que.push(qnode(v,dist[v])); }
}
}
}
void addedge(int u,int v,int w){
E[u].push_back(Edge(v,w));
}
struct node{
int t1,tn;
}p[MAXN];
bool cmp(int x,int y){
return p[x].t1+p[y].tn<p[x].tn+p[y].t1;
}
int a[MAXN];
int main(){
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=k;i++){
scanf("%d",&a[i]);
}
while(m--){
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v,);
addedge(v,u,);
}
Dij(n,);
int ans=;
for(int i=;i<=n;i++)
p[i].t1=dist[i];///从1出发的最短路 Dij(n,n);
for(int i=;i<=n;i++)
p[i].tn=dist[i];///从n出发的最短路
sort(a+,a++k,cmp);
int maxx=p[a[]].t1;
for(int i=;i<=k;i++){
ans=max(ans,maxx+p[a[i]].tn+);
maxx=max(maxx,p[a[i]].t1);
}
printf("%d\n",min(ans,p[n].t1));
return ;
}
Codeforces Round #621 (Div. 1 + Div. 2)D dij(思维)的更多相关文章
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- Educational Codeforces Round 39 (Rated for Div. 2) G
Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...
- Educational Codeforces Round 48 (Rated for Div. 2) CD题解
Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...
- Educational Codeforces Round 60 (Rated for Div. 2) 题解
Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...
随机推荐
- 第十二篇视图层之视图函数(views)-三件套
视图层之视图函数(views) 阅读目录(Content) 视图层之视图函数(views) 一个简单的视图 HttpRequest HttpResponse redirect 函数 对比render与 ...
- weex框架
weex优势: (1)支持ES6规范 (2)性能优异,开发简介标准,提及小巧. (3)跨平台 weex调试工具:weexplayground weex环境搭建: (1)安装 node.js.npm ( ...
- 禁止ViewPager的左右滑动
参考 思路:重写android.support.v4.view.ViewPager中的ViewPager 写一个NoScrollViewPager继承ViewPager 然后用NoScrollVi ...
- 2.10 Jetpack LiveData部分
LiveData是一个可观察的数据持有者类,但和其他的可观察对象不同,它与生命周期相关联,比如Activity的生命周期.LiveData能确保仅在Activity处于活动状态下才会更新.也就是说当观 ...
- web.xml中filter加载顺序出现的问题
刚刚遇到了一个问题,项目中需要用到characterEncodingFilter和HiddenHttpMethodFilter,但是post请求还是会中文乱码,找了半天原因,后来发现,filter加载 ...
- 《Thinking in Java》位运算
按位操作符: 首先先记住一件事,方便理解:是否对应正负对应10. 1.与(&):11得1,10得0,00得0. 2.或(|):11得1,10得1,00得0. 3.异或(^):11得0,10得1 ...
- bestphp's revenge
0x00 知识点 1利用PHP原生类来构造POP链 本题没有可以利用的类,没有可以利用的类就找不到POP链所以只能考虑PHP原生类 我们先来解释一下什么是POP链 POP:面向属性编程 在二进制利用时 ...
- Tensorflow学习教程------Fetch and Feed
#coding:utf-8 import tensorflow as tf #Fetch input1 = tf.constant(3.0) input2 = tf.constant(1.0) inp ...
- redis官网下载自动安装脚本
注释:使用方法为 # ./redis.sh version ----version为官网版本号 #!/bin/bashversion=$1serverurl='download. ...
- Java8集合框架——LinkedList源码分析
java.util.LinkedList 本文的主要目录结构: 一.LinkedList的特点及与ArrayList的比较 二.LinkedList的内部实现 三.LinkedList添加元素 四.L ...