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. java多线程与线程并发三:线程同步通信

    本文章内容整理自:张孝祥_Java多线程与并发库高级应用视频教程. 有些时候,线程间需要传递消息,比如下面这道面试题: 子线程循环10次,然后主线程循环100次,然后又回到子线程循环50次,然后再回到 ...

  2. linux服务器MySQL数据从磁盘拷贝以及恢复

    偶有感触:遇到这个问题,经过一个下午的排查, 终于解决. 故事情节:我的阿里云服务器突然被黑客攻击了,整个系统down了. 找客服,他们排查说usr目录的文件全部丢失.让我重新初始化系统盘.初始化之前 ...

  3. PHP字符逃逸导致的对象注入

    1.漏洞产生原因: 序列化的字符串在经过过滤函数不正确的处理而导致对象注入,目前看到都是因为过滤函数放在了serialize函数之后,要是放在序列化之前应该就不会产生这个问题 ?php functio ...

  4. Reverse proxy

    Nginx 反向代理配置: upstream dynamic { zone upstream_dynamic 64k; least_conn; ##适用于long connect,即请求处理时间长 # ...

  5. echarts绘制彩虹色背景

    大致成品如图所示 关键的步骤: var dom = document.getElementById("myChart"); var myChart = echarts.init(d ...

  6. ASP.NET Aries 高级开发教程:如何写WebAPI接口

    前提: 最近,有不少同学又问到,Aries里如何提供WebAPI接口? 针对这个问题,今天给顺路写个教程,其实呢,很简单的. 方式一:直接用WebService提供接口. 用这种方式,直接添加接口就可 ...

  7. source for "Android 28 platform" not found

    source for "Android 28 platform" not found 解决办法:点击右上角的Download,但是下载完点击Refresh之后没有反应,这时候需要重 ...

  8. 异步任务AsyncTask使用解析

    在Android中实现异步任务机制有两种方式,Handler和AsyncTask. Handler模式需要为每一个任务创建一个新的线程,任务完成后通过Handler实例向UI线程发送消息,完成界面的更 ...

  9. IDEA连接Redis

    1.创建一个Maven项目 2.在src下的pom.xml文件里,添加相关包引用 <?xml version="1.0" encoding="UTF-8" ...

  10. golang开发环境配置

    下载安装 从 https://golang.org/dl/ 下载最新的安装包. windows直接执行exe,按指示进行安装(默认安装目录是:C:\Go) linxu解压后复制到你喜欢目录就行(一般放 ...