[HDU1595] find the longest of the shortest
题目链接:###
题意:###
给定一个\(n\)个点,\(m\)条边的带权无向图,起点为\(1\),终点为\(n\),现在可以删去其中的一条边,求一种删边方案使得剩下图的最短路值最大,输出这个最短路的长度。
题目保证删去任意一条边都能使得\(1\)与\(n\)连通,且边权均为正值。
题目分析:###
首先用\(Dijkstra\)跑出原图的最短路,由于在最短路之外删边对图的最短路没有影响,所以考虑在最短路上删边,直接暴力跑\(Dijkstra\)。
需要注意的是,这里不会超时的原因是一个有\(n\)个点的正权无向图的最短路总共包含的边数不会超过\(n-1\)条。最坏的情况是把每个点都经过一遍,这样就会有\(n-1\)条边在最短路上,而如果最短路的边数\(>n-1\),就说明有节点被经过了两遍,(既然要从这里第二次经过,为什么还要到外面绕一圈而不是直接走呢?)这和最短路矛盾,而在实际情况中,最短路的边数甚至远远达不到这个上界。所以在最短路上删边暴力跑\(Dij\)的最坏时间复杂度是\(O(n*log(n)+n^2*log(n))\)。
代码:###
#include<bits/stdc++.h>
#define N (1000+5)
#define M (1000000+5)
using namespace std;
priority_queue< pair<int,int> > q;
inline int read(){
int cnt=0,f=1;char c;
c=getchar();
while(!isdigit(c)){
if(c=='-') f=-f;
c=getchar();
}
while(isdigit(c)){
cnt=cnt*10+c-'0';
c=getchar();
}
return cnt*f;
}
int n,m,first[N],nxt[M],to[M],w[M],d[N],tot,a,b,v,ans=-1;
bool vis[N];
int path[N];
void add(int x,int y,int z) {
nxt[++tot]=first[x];
first[x]=tot;
to[tot]=y;
w[tot]=z;
}
void dijkstra(){
memset(d,0x3f,sizeof(d));
memset(vis,0,sizeof(vis));
d[1]=0;
q.push(make_pair(0,1));
while(q.size()) {
int u=q.top().second; q.pop();
if(vis[u])continue;
vis[u]=1;
for(register int i=first[u];i;i=nxt[i]) {
int v=to[i];int z=w[i];
if(d[v]>d[u]+z) {
d[v]=d[u]+z;
path[v]=u;
q.push(make_pair(-d[v],v));
}
}
}
}
void dijkstra2(int x,int y) {
memset(d,0x3f,sizeof(d));
memset(vis,0,sizeof(vis));
d[1]=0;
q.push(make_pair(0,1));
while(q.size()) {
int u=q.top().second; q.pop();
if(vis[u])continue;
vis[u]=1;
for(register int i=first[u];i;i=nxt[i]) {
int v=to[i];int z=w[i];
if((u==x&&v==y)||(u==y&&v==x))continue;
if(d[v]>d[u]+z) {
d[v]=d[u]+z;
q.push(make_pair(-d[v],v));
}
}
}
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF) {
tot=0;
for(register int i=1;i<=n;i++) path[i]=-1;
for(register int i=1;i<=n;i++) first[i]=0;
ans=-1;
for(register int i=1;i<=m;i++) {
a=read();b=read();v=read();
add(a,b,v);add(b,a,v);
}
dijkstra();
for(register int i=n;i!=-1;i=path[i]) {
// cout<<i<<" "<<path[i]<<endl;
dijkstra2(i,path[i]);
ans=max(ans,d[n]);
}
printf("%d\n",ans);
}
return 0;
}
[HDU1595] find the longest of the shortest的更多相关文章
- hdu1595 find the longest of the shortest(Dijkstra)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1595 find the longest of the shortest Time Limit: 100 ...
- hdu 1595 find the longest of the shortest【最短路枚举删边求删除每条边后的最短路,并从这些最短路中找出最长的那条】
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- find the longest of the shortest (hdu 1595 SPFA+枚举)
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- hdu 1595 find the longest of the shortest(迪杰斯特拉,减去一条边,求最大最短路)
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- hdu 1595 find the longest of the shortest(dijkstra)
Problem Description Marica is very angry with Mirko because he found a new girlfriend and she seeks ...
- hdu 1595 find the longest of the shortest
http://acm.hdu.edu.cn/showproblem.php?pid=1595 这道题我用spfa在枚举删除边的时候求最短路超时,改用dijkstra就过了. #include < ...
- hdu 1595 find the longest of the shortest(dijstra + 枚举)
http://acm.hdu.edu.cn/showproblem.php?pid=1595 大致题意: 给一个图.让输出从中删除随意一条边后所得最短路径中最长的. . 思路: 直接枚举每条边想必是不 ...
- HDU 1595 find the longest of the shortest【次短路】
转载请注明出处:http://blog.csdn.net/a1dark 分析:经典的次短路问题.dijkstra或者SPFA都能做.先找出最短路.然后依次删掉没条边.为何正确就不证明了.了解思想直接A ...
- hdu1595find the longest of the shortest 最短路
//给一个无向图,问删除一条边,使得从1到n的最短路最长 //问这个最长路 //这个删除的边必定在最短路上,假设不在.那么走这条最短路肯定比其它短 //枚举删除这条最短路的边,找其最长的即为答案 #i ...
随机推荐
- Alsa中PCM参数设置⭐⭐
1) PCM设备的句柄.2) 指定同时可供回放或截获的PCM流的方向3) 提供一些关于我们想要使用的设置选项的信息,比如缓冲区大小,采样率,PCM数据格式等4) 检查硬件是否支持设置选项. 4.1 ...
- 人生苦短之Python多线程
#encoding=utf-8 import threading import time ''' python多线程并不是真正意义上的多线程,通常我们所说的多线程是多个线程同时执行某功能,而在pyth ...
- codevs 3324 新斯诺克
3324 新斯诺克 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 白银 Silver 题目描述 Description 斯诺克又称英式台球,是一种流行的台球运动.在球桌上, ...
- 20.java的7种控制语句
1.java控制语句可以划分为七种 1)控制选择结构语句: if ,if else switch 2)控制循环结构语句: for ,while,do while 3)改变控制语句的顺序 break, ...
- vue render函数使用jsx语法 可以使用v-model语法 vuex实现数据持久化
render函数使用jsx语法: 安装插件 transform-vue-jsx 可以使用v-model语法安装插件 jsx-v-model .babelrc文件配置: vuex实现数据持久化 安装插 ...
- 纯css 图片自适应居中
html 结构 <div class="container"> <div class="content"></div> &l ...
- 2、webpack基础配置
我们需要安装webpack 还需要安装webpack cli 这两个都是我们的开发依赖 这里我们一般会加一个-D表示上线的时候不需要他们两个包 安装我们的webpack 先初始化一下,记住我们的安装依 ...
- Laravel中的模板引擎Blade
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Lombok减少代码冗余量
Eclipse需要安装,具体用法见: https://projectlombok.org/ 用maven project的朋友,一定要记得安装到IED里面才能使用,不然无法直接使用哦 从此以后和get ...
- 51nod 1102 【单调栈】
思路: 对于这个高度往左能延伸最远x,往右能延伸最远y,(x+1+y)*w; 利用单调栈就行了: #include <cstdio> #include <stack> #inc ...