[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 ...
随机推荐
- bzoj4670: 佛罗里达
这题直接随机化+贪心就可以爆踩过去,我加了个退火增加容错率而已....其实你随机的次数够多根本不需要... 然后来自肉丝哥哥的正经做法: 先钦定D(A)>D(B),那么可以枚举D(A),然后再去 ...
- java-线程(一)
1.进程与线程的区别 多个进程的内部数据和状态都是完全独立的,而多个线程是共享一块内存空间和一组系统资源,有可能互相影响.多线程程序比多进程程序需要更少的管理费用.进程是重量级的任务,需要分配他们的单 ...
- session机制大揭秘(结合cookie)
session运行机制 当一个session开始时,servlet容器将创建一个httpSession对象,在HttpSession对象中可以存放客户状态信息. servlet容器为HttpSessi ...
- 脚踏实地学C#1-基元类型
基元类型:编译器直接支持的数据类型 基元类型直接映射到FCL类库上,如int 和Int32是等价的,只不过是int是c#提供的,Int32是FCL类库提供的. int只是Int32的别名 using ...
- 谈谈java中静态变量与静态方法在有继承关系的两个类中调用
谈谈java中静态变量与静态方法在有继承关系的两个类中调用 学习的中如果遇到不明白或者不清楚的的时候,就是自己做些测试,自己去试试,这次我就做一个关于静态变量和静态方法在有继承关系的两个类中的问题测试 ...
- git 错误 Reinitialized existing Git repository in /**/***/ 和refusing to merge unrelated histories
报错一: 这句话的意思是 在路径 /Users/jackma/Downloads/lotteryTicket 2/.git/ 现有的Git存储库初始化 ➜ lotteryTicket 2 git:(m ...
- 页面刷新 方法总结 JSP刷新[转]
1) <meta http-equiv="refresh"content="10;url=跳转的页面"> 10表示间隔10秒刷新一次 2) < ...
- react之redux增加删除数字
比如在页面中添加和删除‘222’ action.js export const ADD= 'ADD'; export const RED='RED'; export const add=(str)=& ...
- the generation has been cancelled because errors have been found by the check model
生成物理模型出现这个错误的话,那就打开“生成物理模型”(快捷键 ctrl+shift+p) 然后找到 Detail → Option 接着讲Check mode复选框去掉.
- Linux中进程控制块PCB-------task_struct结构体结构
Linux中task_struct用来控制管理进程,结构如下: struct task_struct { //说明了该进程是否可以执行,还是可中断等信息 volatile long state; // ...