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

HDU-1595Find the longest of shortest(最短路径的最长路Dijkstra+记录路径)的更多相关文章

  1. Codeforces-A. Shortest path of the king(简单bfs记录路径)

    A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input s ...

  2. HDU 6201 transaction transaction transaction(拆点最长路)

    transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/1 ...

  3. HDU 1224 Free DIY Tour(spfa求最长路+路径输出)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1224 Free DIY Tour Time Limit: 2000/1000 MS (Java/Oth ...

  4. HDU 4109 Instrction Arrangement(DAG上的最长路)

    把点编号改成1-N,加一点0,从0点到之前任意入度为0的点之间连一条边权为0的边,求0点到所有点的最长路. SPFA模板留底用 #include <cstdio> #include < ...

  5. 【最长上升子序列记录路径(n^2)】HDU 1160 FatMouse's Speed

    https://vjudge.net/contest/68966#problem/J [Accepted] #include<iostream> #include<cstdio> ...

  6. HDU - 6201 transaction transaction transaction(spfa求最长路)

    题意:有n个点,n-1条边的无向图,已知每个点书的售价,以及在边上行走的路费,问任选两个点作为起点和终点,能获得的最大利益是多少. 分析: 1.从某个结点出发,首先需要在该结点a花费price[a]买 ...

  7. HDU - 1503 最长公共子序列记录路径

    题意:先给两个水果的名字然后得出一个最短的序列包含这两个词. 思路:我一开始的思路是先求出最长公共子序列,然后做一些处理将其他的部分输出来:两种水果的字符串和最长公共子序列的字符串这三个字符串做对比, ...

  8. HDU 3416 Marriage Match IV (最短路径,网络流,最大流)

    HDU 3416 Marriage Match IV (最短路径,网络流,最大流) Description Do not sincere non-interference. Like that sho ...

  9. HDU1595-find the longest of the shortest-dijkstra+记录路径

    Marica is very angry with Mirko because he found a new girlfriend and she seeks revenge.Since she do ...

随机推荐

  1. netty源码解析(4.0)-29 Future模式的实现

    Future模式是一个重要的异步并发模式,在JDK有实现.但JDK实现的Future模式功能比较简单,使用起来比较复杂.Netty在JDK Future基础上,加强了Future的能力,具体体现在: ...

  2. jquery 判断数组是否为空

    jquery 判断数组是否为空 if (data.length === 0) { console.log("数组为空"); }

  3. nyoj 204-Coin Test (python count)

    204-Coin Test 内存限制:64MB 时间限制:3000ms 特判: No 通过数:2 提交数:2 难度:1 题目描述: As is known to all,if you throw a ...

  4. python:沙盒(virtualenv)

    当电脑需要使用多个版本的python时,可以使用沙盒:或者使用docker: virtualenv是Python自带的,通过pip安装的 [root@centos7 public]# cd jinji ...

  5. JVM的内存回收机制

    垃圾回收机制,简称gc.对堆与方法区的对象进行回收,因为java不像c需要编程人员手动clear,虚拟机通过垃圾回收算法,对堆与方法区的对象进行自动回收处理. 1.引用计数法(jvm没有采用,因为当两 ...

  6. 面试官:关于Java性能优化,你有什么技巧

    通过使用一些辅助性工具来找到程序中的瓶颈,然后就可以对瓶颈部分的代码进行优化. 一般有两种方案:即优化代码或更改设计方法.我们一般会选择后者,因为不去调用以下代码要比调用一些优化的代码更能提高程序的性 ...

  7. 提高PHP性能效率的几个技巧!

    如何提高效率问题,往往同样的功能,不一样的代码,出来的效率往往大不一样. ● 用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有ec ...

  8. 【集合系列】- 深入浅出的分析 Hashtable

    一.摘要 在集合系列的第一章,咱们了解到,Map 的实现类有 HashMap.LinkedHashMap.TreeMap.IdentityHashMap.WeakHashMap.Hashtable.P ...

  9. JavaScript笔记二

    1.表格 - 在网页中可以通过表格来表示一些格式化的数据 - 表格相关的标签 - <table> 用来创建一个表格 - <tr> 表示表格中的一行 - <th> 表 ...

  10. 题解 P1047 【校门外的树】

    可以直接模拟,用珂朵莉树是不有点小题大做. 你怎么做珂朵莉都会骂你:"这么简单的模拟都要用***" 附赠珂朵莉照片一张 另外讲几点: 可以用int,你要不怕MLE #include ...