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

这道题我用spfa在枚举删除边的时候求最短路超时,改用dijkstra就过了。

 #include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#define maxn 1001
using namespace std;
const int inf=<<; int g[maxn][maxn];
int n,m;
int s,e,c;
int dis[maxn];
bool vis[maxn];
int pre[maxn],pre1[maxn]; /*void spfa()
{
queue<int>q;
memset(vis,false,sizeof(vis));
for(int i=1; i<=n; i++) dis[i]=inf;
dis[1]=0;
vis[1]=true;
q.push(1);
pre[1]=-1;
while(!q.empty())
{
int u=q.front(); q.pop();
vis[u]=false;
for(int i=1; i<=n; i++)
{
if(dis[i]>dis[u]+g[u][i]&&g[u][i]!=inf)
{
dis[i]=dis[u]+g[u][i];
pre[i]=u;
if(!vis[i])
{
q.push(i);
vis[i]=true;
}
}
}
}
}*/ void dijstra()
{
memset(vis,false,sizeof(vis));
for(int i=; i<=n; i++) dis[i]=(i==?:inf);
for(int i=; i<=n; i++)
{
int x,mm=inf;
for(int y=; y<=n; y++) if(!vis[y]&&dis[y]<mm) mm=dis[x=y];
vis[x]=true;
for(int y=; y<=n; y++)
{
if(dis[y]>dis[x]+g[x][y]&&!vis[y])
{
pre[y]=x;
dis[y]=dis[x]+g[x][y];
}
}
}
}
void solve()
{
int v1=n;
pre1[]=-;
while(pre[v1]!=-)
{
pre1[v1]=pre[v1];
v1=pre[v1];
}
int v=n;
int max1=-;
while(pre1[v]!=-)
{
int mm=g[pre1[v]][v];
g[pre1[v]][v]=g[v][pre1[v]]=inf;
dijstra();
if(dis[n]!=inf)
max1=max(dis[n],max1);
g[pre1[v]][v]=g[v][pre1[v]]=mm;
v=pre1[v];
}
printf("%d\n",max1);
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(pre,-,sizeof(pre));
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
if(i==j) g[i][j]=;
else g[i][j]=inf;
}
}
for(int i=; i<m; i++)
{
scanf("%d%d%d",&s,&e,&c);
g[s][e]=g[e][s]=min(g[s][e],c);
}
dijstra();
solve();
}
return ;
}

hdu 1595 find the longest of the shortest的更多相关文章

  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(dijstra + 枚举)

    http://acm.hdu.edu.cn/showproblem.php?pid=1595 大致题意: 给一个图.让输出从中删除随意一条边后所得最短路径中最长的. . 思路: 直接枚举每条边想必是不 ...

  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的工作记录B

    这次很强了.就差最后一步,判断一下SVN的版本是否是真的库里的. #!/usr/bin/env python # -*- coding: utf-8 -*- import os,sys,command ...

  2. Android开发多线程断点续传下载器

    使用多线程断点续传下载器在下载的时候多个线程并发可以占用服务器端更多资源,从而加快下载速度,在下载过程中记录每个线程已拷贝数据的数量,如果下载中断,比如无信号断线.电量不足等情况下,这就需要使用到断点 ...

  3. 【转】Ubuntu 修改hosts

    原文网址:http://l.14551.org/2009/12/2166 Ubuntu系统的Hosts只需修改/etc/hosts文件,在目录中还有一个hosts.conf文件,刚开始还以为只需要修改 ...

  4. poj2752 Seek the Name, Seek the Fame

    Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...

  5. cf494A Treasure

    A. Treasure time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  6. [Matlab] Attempt to execute SCRIPT *** as a function

    Attempt to execute SCRIPT *** as a function 问题: 在运行MATLAB程序的时候,出现如题的报错. 原因: 在系统中,现有的.m文件有的与***函数重名,所 ...

  7. Cocos2d-x3.0游戏实例之《别救我》第二篇——创建物理世界

    这篇我要给大家介绍两个知识点: 1. 创建游戏物理世界 2. 没了(小若:我噗) 害怕了?不用操心.这太简单了~! 笨木头花心贡献.啥?花心?不呢.是用心~ 转载请注明,原文地址:http://www ...

  8. oracle SQL语句练习MERGE、模糊查询、排序、

    Oracle支持的SQL指令可分为数据操作语言语句.数据定义语言语句.事务控制语句.会话控制语句等几种类型:1.数据操作语言语句数据操作语言语句(Data manipulation language, ...

  9. seajs初尝 加载jquery返回null解决学习日志含示例下载

    原文地址:http://www.tuicool.com/articles/bmuaEb 如需demo示例,请点击下方链接下载: http://yunpan.cn/cVEybKs8nV7CF  提取码 ...

  10. WebApi2官网学习记录---Media Formatters

    Web API内建支持XML.JSON.BSON.和form-urlencoded的MiME type. 创建的自定义MIME类型要继承一下类中的一个: MediaTypeFormatter 这个类使 ...