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.

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 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

题解:本题是让求去掉最短路上的其中一条后的最短路径;我们可以先处理处最短路径(Dijkstra)并记录所经过的节点,然后依次去掉最短路径上的每一条线段,再Dijkstra,找到最小值即可;

AC代码为:

#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
int N,M,U,V,W,dis[],fa[],Map[][],vis[];
int Dijkstra(int temp)
{
memset(vis,,sizeof vis);
memset(dis,INF,sizeof dis);
dis[]=;
for(int i=;i<=N;i++)
{
int min_dis=INF,u=-;
for(int j=;j<=N;j++)
{
if(!vis[j] && dis[j]<min_dis)
{
min_dis=dis[j]; u=j;
}
}
vis[u]=;
for(int j=;j<=N;j++)
{
if(!vis[j]&&dis[j]>Map[u][j]+dis[u])
{
dis[j]=Map[u][j]+dis[u];
if(temp) fa[j]=u;
}
}
}
return dis[N];
}
int main()
{
ios::sync_with_stdio(false); cin.tie();
while(cin>>N>>M)
{
for(int i=;i<=N;i++)
{
for(int j=;j<=N;j++)
i==j? Map[i][j]=:Map[i][j]=INF;
}
for(int i=;i<=M;i++)
{
cin>>U>>V>>W;
Map[U][V]=Map[V][U]=W;
}
int Max=Dijkstra(),x=N;
while(x!=)
{
int flag=Map[x][fa[x]];
Map[x][fa[x]]=Map[fa[x]][x]=INF;
Max=max(Max,Dijkstra());
Map[x][fa[x]]=Map[fa[x]][x]=flag; x=fa[x];
}
cout<<Max<<endl;
}
return ;
}

最新文章

  1. visual studio code更新
  2. sqlserver中将某数据库下的所有表字段名称为小写的改为大写
  3. hdu1798(几何面积计算)
  4. abstract 类 构造函数
  5. jQuery取值相加
  6. android一些基础知识
  7. show slave status中的log_file / log_pos
  8. php session_id()函数介绍及代码实例
  9. Spring之Spring MVC
  10. Linux NTP校时
  11. 面试题 HashMap 原理
  12. js下拉菜单默认值为当前年份,下拉菜单的选项为当前年份往前推5年
  13. 21个js 技巧收藏
  14. javascript执行原理
  15. mysql left join
  16. ServletFileUpload 图片上传
  17. SQLI DUMB SERIES-14
  18. 在windows中把一个文件夹打成war包
  19. 驱动笔记 - platform中断程序
  20. Eclipse \ MyEclipse \Scala IDEA for Eclipse里如何将控制台console输出的过程记录全程保存到指定的文本文件(图文详解)

热门文章

  1. K8S入门系列之集群二进制部署--&gt;node篇(三)
  2. JSON《===》JavaBean的相互转换
  3. 深入理解计算机系统 第三章 程序的机器级表示 part1
  4. T-SQL Part VIII: CROSS APPLY, OUTER APPLY
  5. Arduino 将 String 转化为 int
  6. go中的关键字-defer
  7. 剑指Offer-20.包含min函数的栈(C++/Java)
  8. gcc在x64体系中如何传递参数,linux,mac,iOS适用
  9. Java 数据持久化系列之JDBC
  10. Java基础语法(二)