find the longest of the shortest

Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2524    Accepted Submission(s):
888

Problem Description
Marica 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.
 
Input
Each 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.
 
Output
In the first line of the output file write the maximum
time in minutes, it could take Marica to come to Mirko.
 
Sample Input
5 6
1 2 4
1 3 3
2 3 1
2 4 4
2 5 7
4 5 1
6 7
1 2 1
2 3 4
3 4 4
4 6 4
1 5 5
2 5 2
5 6
5

5 7

1 2 8
1 4 10
2 3 9
2 4 10
2 5 1
3 4 7
3 5 10
 
Sample Output
11
13
27
 
题意:最短路枚举删边求删除每条边后的最短路,并从这些最短路中找出最长的那条,
题解:我们只需要找出所有路都通畅时的那条最短路,并且依次删去这条路径上的每条边即可,因为如果我们删除的不是这条最短路上的边,我们每次找最短路时依旧会找到这一条
 
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAX 1010
#define INF 0x3f3f3f
using namespace std;
int low[MAX];
int map[MAX][MAX];
int vis[MAX],p[MAX];
int n,m;
void init()
{
int i,j;
for(i=0;i<MAX;i++)
for(j=0;j<MAX;j++)
map[i][j]=i==j?0:INF;
}
int dj(int v)
{
int i,j,min,next;
memset(vis,0,sizeof(vis));
memset(low,INF,sizeof(low));
low[1]=0;
for(i=1;i<=n;i++)
{
min=INF;
for(j=1;j<=n;j++)
{
if(!vis[j]&&min>low[j])
{
next=j;
min=low[j];
}
}
vis[next]=1;
for(j=1;j<=n;j++)
{
if(!vis[j]&&low[j]>low[next]+map[next][j])
{
low[j]=low[next]+map[next][j];
if(v)
p[j]=next;//记录寻找最短路时i的上一个点的位置
}
}
}
return low[n];
}
int main()
{
int a,b,c,i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
if(map[a][b]>c)
map[a][b]=map[b][a]=c;
}
memset(p,0,sizeof(p));
int maxx=dj(1);
for(i=n;i!=1;i=p[i])//倒着遍历最短路
{
int x=map[i][p[i]];
map[i][p[i]]=map[p[i]][i]=INF;
maxx=max(maxx,dj(0));
map[i][p[i]]=map[p[i]][i]=x;
}
printf("%d\n",maxx);
}
return 0;
}

  

最新文章

  1. 本地新建项目提交到github
  2. uitableview性能优化(转)
  3. C#中判断字符是否大写
  4. 逻辑卷管理LVM (Logical Volume Manager)
  5. Android Studio新建Jni工程
  6. Git 提交大文件提示 fatal: The remote end hung up unexpectedly
  7. ThreadPool for Delphi
  8. 解决IE下z-index的bug
  9. input的多条数据以数组形势上传
  10. Spring_Springmvc_mybatis一般配置
  11. WINFORM跟随WPF窗体移动
  12. 第三章:真正弄清楚一个Mod的组织结构
  13. Python读取Yaml文件
  14. zoj 1107 FatMouse and Cheese(记忆化搜索)
  15. Apache JMeter--网站自动测试与性能测评
  16. Linux centos7系统下svn的安装与配置
  17. Java与算法之(2) - 快速排序
  18. Java基础语法&lt;五&gt; 大数值BigInteger BigDecimal
  19. 关于&lt;超文本&gt;定义
  20. bzoj 4128 矩阵求逆

热门文章

  1. Java在Windows的环境配置
  2. mycat读写分离
  3. SRM 585 DIV1 L2
  4. Move to Another Changelist
  5. Apache Wamp WampServer 配置多端口 多站点 虚拟目录
  6. How to remove spaces of a string with regular expression
  7. 关于SqlParameter中IN子句查询的问题
  8. IDE模式下安装Windows 7强行改回ACHI后不断重启的解决方法
  9. 如何在CentOS 5.x 中安装Windows Azure Linux Agent (WALA)
  10. Apache2.2+Tomcat7.0整合配置详解