题: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(思维)的更多相关文章

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

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

  3. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  4. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

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

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

  7. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

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

  9. Educational Codeforces Round 48 (Rated for Div. 2) CD题解

    Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...

  10. Educational Codeforces Round 60 (Rated for Div. 2) 题解

    Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...

随机推荐

  1. 三十七、SAP中文本资源的存放

    一.我们看看之前的代码内容 二.菜单转到->文本元素 三.在文本符号中写入需要替换的内容 四.修改一下代码,可以用text-001来等效替换 五.效果如下

  2. HZNU-ACM寒假集训Day12小结 数论入门 题解

    算不出的等式 BJOI2012 看到这题 真没什么办法 无奈看题解 1.注意到p/q 联想到斜率 2.注意到 [ ] 联想到整点 注意到k在变化,构造一次函数 f(x)=p/q*x ,g(x)=q/p ...

  3. Python MySQL Update

    章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...

  4. sql ,类型转换,日期截取格式

    字符型 转换成整型 CONVERT(int ,字段) 只取年月日格式 CONVERT(varchar(10), ZB.drive_time, 120 ) SELECT CONVERT(VARCHAR, ...

  5. UVA - 10003 Cutting Sticks(切木棍)(dp)

    题意:有一根长度为L(L<1000)的棍子,还有n(n < 50)个切割点的位置(按照从小到大排列).你的任务是在这些切割点的位置处把棍子切成n+1部分,使得总切割费用最小.每次切割的费用 ...

  6. JAVAEE 和项目开发(第三课:HTTP的请求头和请求方式)

    HTTP 协议之请求格式   请求格式的结构:请求行:请求方式.请求的地址和 HTTP 协议版本 请求头:消息报头,一般用来说明客户端要使用的一些附加信息 空行: 位于请求行和请求数据之间,空行是必须 ...

  7. Linux command line and shell scripting buble

    Chapter 4 More bash shell Commands 1. ps ps -ef 2. top 3. kill 3940 kill -s HUP 3940 killall http* 4 ...

  8. PAT Advanced 1013 Battle Over Cities (25) [图的遍历,统计连通分量的个数,DFS,BFS,并查集]

    题目 It is vitally important to have all the cities connected by highways in a war. If a city is occup ...

  9. if case for while

    #!/bin/basha=$1if [ $a ] #判断$1是否为空then #非空echo "the input is No:$a"exit 0else #空read -p &q ...

  10. MYSQL连接不上100061错误

    有界面的情况下启动MYSQL 无界面 https://blog.csdn.net/qq_22233621/article/details/72673176 参考