http://acm.hdu.edu.cn/showproblem.php?pid=1595

大致题意:

给一个图。让输出从中删除随意一条边后所得最短路径中最长的。



思路:

直接枚举每条边想必是不行的。事实上有些边是不须要枚举的,由于删除它们并不影响起点到终点的最短路。起作用的边都是未删边前的最短路径上的边,删除它们最短距离肯定增大,仅仅需在这些最短距离中求最大的就可以。

记录最短路径上的边,仅仅需一个pre数组记录松弛时每一个点的前驱节点。

#include <stdio.h>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <string.h>
#define LL long long
#define _LL __int64
using namespace std; const int maxn = 1010;
const int INF = 0x3f3f3f3f; int n,m;
int mapp[1010][1010];
int dis[maxn],vis[maxn];
int pre[maxn],pre1[maxn];
int ans; void init()
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(i == j)
mapp[i][j] = 0;
else
mapp[i][j] = INF;
}
}
memset(pre,-1,sizeof(pre));
} int dijstra(int s)
{
memset(dis,INF,sizeof(dis));
memset(vis,0,sizeof(vis)); for(int i = 1; i <= n; i++)
{
dis[i] = mapp[s][i];
pre[i] = s;
}
vis[s] = 1;
pre[s] = -1; for(int i = 1; i < n; i++)
{
int min = INF,pos;
for(int j = 1; j <= n; j++)
{
if(min > dis[j] && !vis[j])
{
min = dis[j];
pos = j;
}
} vis[pos] = 1;
for(int j = 1; j <= n; j++)
{
if(!vis[j] && dis[j] > dis[pos] + mapp[pos][j])
{
dis[j] = dis[pos] + mapp[pos][j];
pre[j] = pos;
}
}
}
return dis[n]; } void solve()
{
memcpy(pre1,pre,sizeof(pre));
ans = -1; int u = n; while(pre1[u] != -1)
{
int tmp = mapp[pre1[u]][u]; mapp[pre1[u]][u] = mapp[u][pre1[u]] = INF; //删除该边 int res = dijstra(1);
if(res != INF)
ans = max(ans,res); mapp[ pre1[u] ][u] = mapp[u][ pre1[u] ] = tmp; u = pre1[u];
}
printf("%d\n",ans);
} int main()
{
int u,v,w;
while(~scanf("%d %d",&n,&m))
{
init(); for(int i = 0; i < m; i++)
{
scanf("%d %d %d",&u,&v,&w);
mapp[u][v] = mapp[v][u] = min(w,mapp[u][v]);
} dijstra(1);
solve();
}
return 0;
}

hdu 1595 find the longest of the shortest(dijstra + 枚举)的更多相关文章

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

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

  4. hdu 1595 find the longest of the shortest

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

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

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

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

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

  8. hdu 1595(最短路变形好题)

    find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  9. HDU 5373(2015多校7)-The shortest problem(模拟%11)

    题目地址:pid=5373">HDU 5373 题意:给你一个数n和操作次数t,每次操作将n的各位数之和求出来放在n的末尾形成新的n,问t次操作后得到的n能否够被11整除. 思路:就是 ...

随机推荐

  1. Python的 numpy中 numpy.ravel() 和numpy.flatten()的区别和使用

    两者所要实现的功能是一致的(将多维数组降为一维), 两者的区别在于返回拷贝(copy)还是返回视图(view),numpy.flatten() 返回一份拷贝,对拷贝所做的修改不会影响(reflects ...

  2. 【jquery】hover方法

    方法名称:hover(over, out) 概述:当鼠标移动到一个匹配的元素上面时,会触发指定的第一个函数.当鼠标移出这个元素时,会触发指定的第二个函数. 参数: 1) overFunction 鼠标 ...

  3. CURL实现HTTP的GET POST方法

    Curl是Linux下一个非常强大的http命令行工具,其功能十分强大. 一.CURL对HTTP的常规訪问 1. 訪问站点 $ curl http://www.linuxidc.com 回车之后.ww ...

  4. Python 列表 remove() 方法

    描述 Python 列表 remove() 方法通过指定元素的值来移除列表中某个元素的第一个匹配项,如果这个元素不在列表中会报一个异常. 语法 remove() 方法语法: L.remove(obj) ...

  5. 导入sklearn 报错,找不到相关模块

    1.问题原因::安装包的来源问题,也可以理解为包版本兼容问题,有的包使用官方出版,有的包使用whl文件安装解决方案:将所有包都统一来源,要么全部使用官方出版的包,要么全部使用whl里面的包,问题就解决 ...

  6. mysql 查询昨天,今天、七天、30天的数据

    主要是时间戳转"1993-01-01 00:00:00"的时间格式,然后和当前时间比对CURDATE() 如果字段本身符合正常时间格式,则直接使用即可 今天的数据 SELECT * ...

  7. C# ASE加密解密

    项目中比较常用的加密手段 /// <summary> /// ASE_128_ECB_无填充_64Base_加密函数 /// </summary> /// <param ...

  8. 程序员Git代码托管平台

    程序员Git代码托管平台 说到Git代码托管平台,首先推荐的是GitHub,好多好的开源项目都来自GitHub,但是GitHub只能新建公开的Git仓库,私有 仓库要收费,如果你做的是一个开源项目,可 ...

  9. 群智能优化算法-测试函数matlab源码

    群智能优化算法测试函数matlab源代码 global M; creatematrix(2); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %画ackley图. %%%% ...

  10. ecshop中ajax的调用

    1.首先ecshop是如何定义ajax对象的. ecshop中的ajax对象是在js/transport.js文件中定义的.里面是ajax对象文件.声明了一个var Ajax = Transport; ...