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. 关于域名如何指向WordPress homepage问题的解决

    http://genuinelx.org/oldversion.php/archives/19为解决这个问题真的费了我半天的时间= = ,不写出来真的难以抒发苦闷. 下午VPS开通了,虽然有个ip被墙 ...

  2. Google C++单元测试框架

    一.概述 Google C++单元测试框架(简称Gtest),可在多个平台上使用(包括Linux, Mac OS X, Windows, Cygwin和Symbian),它提供了丰富的断言.致命和非致 ...

  3. Google Map 学习过程中的代码

    <!DOCTYPE html><html> <head> <title>Simple click event</title> <met ...

  4. 帆软报表和jeecg的进一步整合--ajax给后台传递map类型的参数

    下面是页面代码: <%@ page language="java" contentType="text/html; charset=UTF-8" page ...

  5. scrapy 的一个例子

    1.目标: scrapy 是一个爬虫构架,现用一个简单的例子来讲解,scrapy 的使用步骤 2.创建一个scrapy的项目: 创建一个叫firstSpider的项目,命令如下: scrapy sta ...

  6. JavaScript与DOM(上)

    本来像自己写一篇的...结果看到了Tom uncle的这篇..总结的确实很赞,其他文章也非常好推荐 转载自:http://www.cnblogs.com/TomXu/archive/2011/12/1 ...

  7. 微信开发,对象转换为xml时候引用XStream这个类报错处理方案

    报错的信息为:The type org.xmlpull.v1.XmlPullParser cannot be resolved. /**  * 扩展XStream 支持CDATA  */ privat ...

  8. Linode中的Network Helper

    Linode主机vps有一个很好的网络配置工具:Network Helper,他可以在系统启动的时候,根据你的操作系统,以及检测到的网络配置等信息,自动配置好网络,非常有用. 官方文档: Networ ...

  9. Sampling and Estimation

    Sampling and Estimation Sampling Error Sampling error is the difference between a sample statistic(t ...

  10. [na]PKI公钥处理思路

    前提申明: 在使用任何基于RSA服务之前,一个实体要真实可靠的获取其他实体的公钥. 1,一个可以确认公钥身份的方案:[离线确认] 主:B做同样的事情得到A的公钥. 但是这种方法扩展性差,不可行. 2, ...