题目链接:###

点我

题意:###

给定一个\(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的更多相关文章

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

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

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

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

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

  6. hdu 1595 find the longest of the shortest

    http://acm.hdu.edu.cn/showproblem.php?pid=1595 这道题我用spfa在枚举删除边的时候求最短路超时,改用dijkstra就过了. #include < ...

  7. hdu 1595 find the longest of the shortest(dijstra + 枚举)

    http://acm.hdu.edu.cn/showproblem.php?pid=1595 大致题意: 给一个图.让输出从中删除随意一条边后所得最短路径中最长的. . 思路: 直接枚举每条边想必是不 ...

  8. HDU 1595 find the longest of the shortest【次短路】

    转载请注明出处:http://blog.csdn.net/a1dark 分析:经典的次短路问题.dijkstra或者SPFA都能做.先找出最短路.然后依次删掉没条边.为何正确就不证明了.了解思想直接A ...

  9. hdu1595find the longest of the shortest 最短路

    //给一个无向图,问删除一条边,使得从1到n的最短路最长 //问这个最长路 //这个删除的边必定在最短路上,假设不在.那么走这条最短路肯定比其它短 //枚举删除这条最短路的边,找其最长的即为答案 #i ...

随机推荐

  1. Good Bye 2015 B. New Year and Old Property —— dfs 数学

    题目链接:http://codeforces.com/problemset/problem/611/B B. New Year and Old Property time limit per test ...

  2. 关于Zookeeper

    Zookeeper是分布式协调工具 应用场景 命名服务(注册中心) Dubbo注册中心 分布式配置中心(SpringCloud config)动态管理配置文件信息 消息中间件 事件通知(类似发布订阅) ...

  3. 如何把wecenter的推荐的问题模块单独调取出来?

    查阅文档: http://wenda.wecenter.com/question/1893 http://www.zhidiu.com/article/1012 http://wenda.wecent ...

  4. centos6.5 mysql 5.6修改root密码,以及创建用户并授权

    mkdir -p mysql_home/{data,temp,undologs,logs} chown -R mysql:mysql /dbfiles/mysql_home mysql_install ...

  5. Android图片加载神器之Fresco, 基于各种使用场景的讲解

    Fresco是Facebook开源Android平台上一个强大的图片加载库,也是迄今为止Android平台上最强大的图片加载库. 优点:相对于其他开源的第三方图片加载库,Fresco拥有更好的内存管理 ...

  6. Python:递归

    递归两个基本要素: (1) 边界条件:确定递归到何时终止,也称为递归出口. (n = 1)(2) 递归模式:大问题是如何分解为小问题的,也称为递归体.(n*(n-1)!   n>1) 例:累加 ...

  7. shell---rpm

    [root@master src]# rpm -qpl epel-release-latest-6.noarch.rpm         ##查询该rpm包安装了什么warning: epel-rel ...

  8. 不能访问tomcat中的项目

    tomcat在eclipse里面能正常启动,而在浏览器中访问http://localhost:8080/不能访问,且报404错误.同时其他项目页面也不能访问. 关闭eclipse里面的tomcat,在 ...

  9. Ubuntu 16.04 安装wine

    1.安装源       sudo add-apt-repository ppa:wine/wine-builds       sudo apt-get update 2.安装wine      sud ...

  10. Azure Key Vault (1) 入门

    <Windows Azure Platform 系列文章目录> 为什么要使用Azure Key Vault? 我们假设在微软云Azure上有1个场景,在Windows VM里面有1个.NE ...