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. Cmake 脚本对预处理器的宏定义

    我们有些时候会在#if   #ifndef   等预编译命令里面看到_WIN32等定义的宏.但是有些宏定义,你即使通过Visual Studio右键的go to definitions 和go to ...

  2. vs2008包加载失败

    由于安装vs2008sp1后,新建项目报错,解决未遂,于是重装vs2008,装完后又出现包加载失败问题: Microsoft.Data.Entity.Design.Package.MicrosoftD ...

  3. 段错误bug的调试

    我们在用C/C++语言写程序的时侯,内存管理的绝大部分工作都是需要我们来做的.实际上,内存管理是一个比较繁琐的工作,无论你多高明,经验多丰富,难 免会在此处犯些小错误,而通常这些错误又是那么的浅显而易 ...

  4. OpenCV的矩阵合并方法

    有的时候我们需要将几个矩阵按行或者按列进行合并成一个大矩阵,这在Matlab里面非常的简单,但在OpenCV里面并没有这样的方法,现在我在OpenCV的源码里面发现合并矩阵的方法,分享给大家. A = ...

  5. c#类和结构体的关系

    原文地址:http://www.dnbcw.com/biancheng/c/fvhc81798.html 简介:这是c#类和结构体的关系的详细页面,介绍了和c/c++,有关的知识,谢谢大家的观看!要查 ...

  6. 【开源】Hawk-数据抓取工具:简明教程

    1.软件介绍 HAWK是一种数据采集和清洗工具,依据GPL协议开源,能够灵活,有效地采集来自网页,数据库,文件, 并通过可视化地拖拽, 快速地进行生成,过滤,转换等操作.其功能最适合的领域,是爬虫和数 ...

  7. javascript笔记7之对象数组

    /* var box = new Array(); //声明一个数组,空数组 alert(typeof box); //数组属于object类型 var box = new Array('李炎恢', ...

  8. poj 3186 Treats for the Cows(区间dp)

    Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for gi ...

  9. matlab遗传算法

    function [xv,fv] = myGA(fitness, a, b, NP, NG, Pc, Pm, eps) % 用遗传算法求解一维无约束优化问题 % % 待优化的目标函数 fitness ...

  10. ZOJ 3430 Detect the Virus 【AC自动机+解码】

    解码的那些事儿,不多说. 注意解码后的结果各种情况都有,用整数数组存储,char数组会超char类型的范围(这个事最蛋疼的啊)建立自动机的时候不能用0来判断结束. #include <cstdi ...