首页 技术 正文
技术 2022年11月21日
0 收藏 634 点赞 4,015 浏览 2757 个字

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

Problem DescriptionMarica is very angry with Mirko because he found a new
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. InputEach case there are two numbers in the first row, N and
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. OutputIn the first line of the output file write the maximum
time in minutes, it could take Marica to come to Mirko. Sample Input5 61 2 41 3 32 3 12 4 42 5 74 5 1 6 71 2 12 3 43 4 44 6 41 5 52 5 25 6 5  5 71 2 81 4 102 3 92 4 102 5 13 4 73 5 10 Sample Output111327 Authorailyanlu SourceHDU
2007-Spring Programming Contest – Warm Up (1)
 Recommend8600   |   We have carefully selected several similar
problems for you:  1596 1598 1142 1162 1301  枚举所有的路被删除的情况,用迪杰斯特拉求最短路。 题意:一个无向图,n个点,m条边,输入m条边的起点,终点,距离,假设减去其中任意一条边,问各种情况下的最短路中最长的是哪条 附上代码: 

 #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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,954
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,479
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,291
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,108
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,740
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,774