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 K (Java/Others)
Total Submission(s): 2702 Accepted Submission(s):
974
girlfriend and she seeks revenge.Since she doesn't live in the same city, she
started preparing for the long journey.We know for every road how many minutes
it takes to come from one city to another.
Mirko overheard in the car that
one of the roads is under repairs, and that it is blocked, but didn't konw
exactly which road. It is possible to come from Marica's city to Mirko's no
matter which road is closed.
Marica will travel only by non-blocked roads,
and she will travel by shortest route. Mirko wants to know how long will it take
for her to get to his city in the worst case, so that he could make sure that
his girlfriend is out of town for long enough.Write a program that helps Mirko
in finding out what is the longest time in minutes it could take for Marica to
come by shortest route by non-blocked roads to his city.
M, separated by a single space, the number of towns,and the number of roads
between the towns. 1 ≤ N ≤ 1000, 1 ≤ M ≤ N*(N-1)/2. The cities are markedwith
numbers from 1 to N, Mirko is located in city 1, and Marica in city N.
In the
next M lines are three numbers A, B and V, separated by commas. 1 ≤ A,B ≤ N, 1 ≤
V ≤ 1000.Those numbers mean that there is a two-way road between cities A and B,
and that it is crossable in V minutes.
time in minutes, it could take Marica to come to Mirko.
#include <iostream>
#include <cstdio>
#include <cstring>
#define M 1005
#define inf 0x3f3f3f3f
using namespace std;
int map[M][M],vis[M],dis[M],link[M];
int n,m;
int max(int a,int b)
{
return a>b?a:b;
} void djstl(int s,int flag) ///最短路遍历,s代表起点,flag作为标记,初始第一次为1,以后为0
{
int i,j,minn,t;
memset(vis,,sizeof(vis));
for(i=; i<=n; i++)
dis[i]=inf;
dis[s]=;
for(i=; i<=n; i++)
{
minn=inf;
for(j=; j<=n; j++)
if(!vis[j]&&dis[j]<minn)
{
minn=dis[j];
t=j;
}
vis[t]=;
for(j=; j<=n; j++)
{
if(!vis[j]&&map[t][j]<inf)
{
if(dis[j]>dis[t]+map[t][j])
{
dis[j]=dis[t]+map[t][j];
if(flag) ///这个点一定为一个缩短路程的转折点,去除后才会导致路程变长
{
link[j]=t;
} }
}
}
}
return;
} int main()
{
int i,j;
while(~scanf("%d%d",&n,&m))
{
memset(link,,sizeof(link));
for(i=; i<=n; i++)
for(j=; j<=n; j++)
if(i==j) map[i][j]=;
else map[i][j]=inf;
int a,b,c;
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
if(map[a][b]>c)
map[a][b]=map[b][a]=c;
}
djstl(,); ///第一次初始化图,求出每个点的最短路
int ans=dis[n];
for(i=n; i!=; i=link[i])
{
int tem=map[i][link[i]];
map[i][link[i]]=map[link[i]][i]=inf; ///遍历去掉每条路的情况
djstl(,);
ans=max(ans,dis[n]);
map[i][link[i]]=map[link[i]][i]=tem;
}
printf("%d\n",ans);
}
return ;
}
hdu 1595 find the longest of the shortest(迪杰斯特拉,减去一条边,求最大最短路)的更多相关文章
- HDU 3790(两种权值的迪杰斯特拉算法)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3790 最短路径问题 Time Limit: 2000/1000 MS (Java/Others) ...
- 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 ...
- 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 ...
- hdu 1595 find the longest of the shortest
http://acm.hdu.edu.cn/showproblem.php?pid=1595 这道题我用spfa在枚举删除边的时候求最短路超时,改用dijkstra就过了. #include < ...
- hdu 1595 find the longest of the shortest(dijstra + 枚举)
http://acm.hdu.edu.cn/showproblem.php?pid=1595 大致题意: 给一个图.让输出从中删除随意一条边后所得最短路径中最长的. . 思路: 直接枚举每条边想必是不 ...
- HDU 1595 find the longest of the shortest【次短路】
转载请注明出处:http://blog.csdn.net/a1dark 分析:经典的次短路问题.dijkstra或者SPFA都能做.先找出最短路.然后依次删掉没条边.为何正确就不证明了.了解思想直接A ...
- HDU 3339 In Action(迪杰斯特拉+01背包)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3339 In Action Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 2544最短路 (迪杰斯特拉算法)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Time Limit: 5000/1000 MS (Java/Others) Me ...
- HDU 1874畅通工程续(迪杰斯特拉算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others) ...
随机推荐
- GYM 101350 F. Monkeying Around
F. Monkeying Around time limit per test 2.0 s memory limit per test 256 MB input standard input outp ...
- 卡特兰数(catalan)总结
卡特兰数的公式 递推公式1:$f(n)=\sum \limits_{i=0}^{n-1}f(i)*f(n-i-1)$ 递推公式2:$f(n)=\frac{f(n-1)*(4*n-2)}{n+1}$ 组 ...
- python学习笔记08-- socket编程
本节内容: 一.网络基础知识 二.socket概念及相关语法 2.1socket概念 2.2socket解释 2.3socket模块功能介绍 2.4socket粘包问题 2.5Socket多并发 一. ...
- Java.控制层.响应工具类.
Java.控制层.响应工具类. package cn.com.spdbccc.cds.index.web.base; public class ApiResponse { private int co ...
- VisualVM介绍使用
1 打开VisualVM(这个工具放在JDK安装目录的bin目录下,双击jvisualvm.exe即可打开),如下图所示 以VisualVM自身为例,VisualVM本身也是一个java程序,当 ...
- Python 经典正则表达式语法实例
- 【软件安装】Linux下安装OpenSSL
安装环境: 操作系统:Ubuntu14.04 OpenSLL Version:openssl-1.0.2n.tar.gz(1.0.2版本为稳定版本,1.1.0为开发版本) OpenSLL下载地址为:h ...
- DELL(linux 系统里系统掉盘)(阵列Foreign)命令行里重做阵列
故障现象 (阵列Foreign 系统下重做阵列) 例:四合一机器(DELL_XENCOMPA09) 有四个硬盘 如图 少认到一个 df -h fdisk -l 2>/dev/null | gr ...
- web框架起源
web框架 python三大主流web框架 django 大而全,自带的组件和功能极多, 缺点:写小项目时候会比较笨重(杀鸡用牛刀),大并发不行,3000撑死 flask 小而精 自带的组件和功能极少 ...
- 连接池c3p0
连接池c3p0 C3P0:hibernate和spring使用,有自动回收空闲连接的功能. 使用步骤: 1.导入jar包(c3p0-0.9.1.2.jar) 2.使用api a.硬编码(不推荐) ne ...