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. jQuery DataTables的简单实现

    DataTables是一个jQuery的表格插件.这是一个高度灵活的工具,依据的基础逐步增强,这将增加先进的互动控制,支持任何HTML表格. 主要特点: 1.自动分页处理 2.即时表格数据过滤 3.数 ...

  2. C#代码规范 程序员必备的秘笈

    1.引言 本文是一套面向C# programmer和C# developer进行开发所应遵循的开发规范 按照此规范来开发C#程序可带来以下益处: 代码的编写保持一致性,提高代码的可读性和可维护性,在团 ...

  3. 摘:用ADO操作数据库的方法步骤

    用ADO操作数据库的方法步骤 ADO接口简介 ADO库包含三个基本接口:_ConnectionPtr接口._CommandPtr接口和_RecordsetPtr接口. _ConnectionPtr接口 ...

  4. Linux时间子系统(六) POSIX timer

    一.前言 在用户空间接口函数文档中,我们描述了和POSIX timer相关的操作,主要包括创建一个timer.设定timer.获取timer的状态.获取timer overrun的信息.删除timer ...

  5. hdu 1853 Cyclic Tour 最大权值匹配 全部点连成环的最小边权和

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) ...

  6. WM_MOUSELEAVE 和 WM_MOUSEHOVER 使用

    原文链接: http://www.cnblogs.com/weiqubo/archive/2011/04/14/2016323.html 默认情况下,窗口是不响应 WM_MOUSELEAVE 和 WM ...

  7. [转]VTH changes in DC from Hspice

    Hello, everyone. I’d like to know the threshold of the MOS transistor. And I found the “.print vth() ...

  8. mod_fastcgi和mod_fcgid的区别

    mod_fcgid是一个跟mod_fastcgi二进制兼容的Apache module. 原 来的mod_fastcgi因为实现方式的限制,所以可能会创建了很多不必要的进程,而实际上只需要更少的进程就 ...

  9. OpenGl学习 glenable()函数理解

    glEnable用于启用各种功能.功能由参数决定.与glDisable相对应.glDisable是用来关闭的.两个函数参数取值是一至的. 参数说明:void glEnable(GLenum cap)G ...

  10. macbook基本配置

    1.安装iterm2, 2.安装搜狗输入法, 3.安装迅雷, 4.安装homebrew 5.安装新版的gcc,bash等等,及升级配置文件.