题目链接:###

点我

题意:###

给定一个\(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. Codeforces Round #105 (Div. 2) E. Porcelain —— DP(背包问题)

    题目链接:http://codeforces.com/problemset/problem/148/E E. Porcelain time limit per test 1 second memory ...

  2. Xshell和secureCRT

    作为一名测试人员,xshell和secureCRT用它们来查看日志.排查定位问题,用的时间长了总感觉只是摸着点皮毛,连这两个工具的名字以及它的工作原理都不清楚,就查了点资料来多了解下,虽然可能在日常工 ...

  3. C结构体、C++结构体、C++类的区别

    先来说说C和C++中结构体的不同 a) C语言中的结构体不能为空,否则会报错 1>d:\myproject\visual studio 2013\projects\myc++\main.c(71 ...

  4. bzoj 4827 [Hnoi2017] 礼物 —— FFT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4827 首先,旋转对应,可以把 b 序列扩展成2倍,则 a 序列对应到的还是一段区间: 再把 ...

  5. Azure Key Vault (2) 使用Azure Portal创建和查看Azure Key Vault

    <Windows Azure Platform 系列文章目录> 请注意: 文本仅简单介绍如何在Azure Portal创建和创建Key Vault,如果需要结合Application做二次 ...

  6. 从MySQL获取数据

    安装 PM> install-package newtonsoft.json PM> install-package mysql.data string connectionString ...

  7. 24.如何结束返回值是void的方法

    如何结束返回值是void的方法? return;只能够出现在方法类型是void 的方法中,用来结束方法. return后面还可以跟数据,后面的数据可以是整数.字符串.false.ture.小数.主要看 ...

  8. 回味经典——uboot1.1.6 之 第二阶段 第三阶段

    转自:http://blog.csdn.net/lizuobin2/article/details/52061530 上篇文章说到,再清 BSS 段之后,CPU 跳转到 sdram 里的 start_ ...

  9. css样式 -- 表格不会因为字体过长导致字体溢出的问题

    常常碰到因为表格大小就麽大了,字体过长会爆炸溢出的问题,我们后端就用这个可以了,溢出的可以省略号 ... 代替好了. /* 在表格css样式加上这三个就可以了 效果就会变成 “abc...” */ { ...

  10. css sprite讲解与使用实例

    转自:http://www.manongjc.com/article/886.html 一.什么是css sprites css sprites直译过来就是CSS精灵.通常被解释为“CSS图像拼合”或 ...