HDU-1595Find the longest of shortest(最短路径的最长路Dijkstra+记录路径)
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+记录路径)的更多相关文章
- 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 ...
- HDU 6201 transaction transaction transaction(拆点最长路)
transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 132768/1 ...
- 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 ...
- HDU 4109 Instrction Arrangement(DAG上的最长路)
把点编号改成1-N,加一点0,从0点到之前任意入度为0的点之间连一条边权为0的边,求0点到所有点的最长路. SPFA模板留底用 #include <cstdio> #include < ...
- 【最长上升子序列记录路径(n^2)】HDU 1160 FatMouse's Speed
https://vjudge.net/contest/68966#problem/J [Accepted] #include<iostream> #include<cstdio> ...
- HDU - 6201 transaction transaction transaction(spfa求最长路)
题意:有n个点,n-1条边的无向图,已知每个点书的售价,以及在边上行走的路费,问任选两个点作为起点和终点,能获得的最大利益是多少. 分析: 1.从某个结点出发,首先需要在该结点a花费price[a]买 ...
- HDU - 1503 最长公共子序列记录路径
题意:先给两个水果的名字然后得出一个最短的序列包含这两个词. 思路:我一开始的思路是先求出最长公共子序列,然后做一些处理将其他的部分输出来:两种水果的字符串和最长公共子序列的字符串这三个字符串做对比, ...
- HDU 3416 Marriage Match IV (最短路径,网络流,最大流)
HDU 3416 Marriage Match IV (最短路径,网络流,最大流) Description Do not sincere non-interference. Like that sho ...
- 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 ...
随机推荐
- 使用CXF发布webservice服务及注意要点
一.概念 1.什么是webservice Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML标准来描述.发布.发现.协调和配置这些应用程序,用 ...
- SVN积极拒绝解决办法
出现以上情况多数为Linux里面的svn自启动没有设置好,一般是自启文件被废弃了,就算在里面添加自启代码也无效,想要兼容旧版本使用这个文件,只需在root管理员模式下输入代码chmod +x /etc ...
- Ubuntu字符界面与图形界面的切换
1.按ALT+CTRL+F1切换到字符界面 2.按ALT+CTRL+F7切换到图形界面
- thefuck的安装和使用
先上一张图片 当输错linux命令,fuck一下….. 安装步骤: sudo apt-get install python3-dev python3-pip sudo -H pip3 install ...
- 系统信息命令(uname、dmesg、df、hostname、free)
uname 显示计算机及操作系统相关的信息,uname -a显示全部信息,uname -r内核的发行号,各种信息可以有单独的选项分别指出 [lixn@Fedora24 ~]$ uname -a Lin ...
- Centos7下oracle12c的安装与配置(详细)
一.硬件配置 CentOS7@VMware® Workstation 15 Pro,分配资源:CPU:2颗,内存:4GB,硬盘空间:30GB 二.软件准备 linux.x64_11gR2_datab ...
- 搭建wordPress遇到无法连接数据库的问题
在确认了数据库用户,密码,地址都没有错的情况下,仍然出现无法连接数据库的问题,以至无法安装wordpress 我的wordpress:4.8.1-zh_CN 解决办法: 1.更改php的版本(我的改为 ...
- 扛把子组20191121-3 Final阶段贡献分配规则
此作业的要求参见http://edu.cnblogs.com/campus/nenu/2019fall/homework/10063 队名:扛把子 组长:孙晓宇 组员:宋晓丽 梁梦瑶 韩昊 刘信鹏 F ...
- 微信二维码添加logo
生成带参数的二维码 为了满足用户渠道推广分析和用户帐号绑定等场景的需要,公众平台提供了生成带参数二维码的接口.使用该接口可以获得多个带不同场景值的二维码,用户扫描后,公众号可以接收到事件推送. 目前有 ...
- Acquistion Location Confidence for accurate object detection
Acquistion Location Confidence for accurate object detection 本论文主要是解决一下两个问题: 1.分类得分高的预测框与IOU不匹配,(我猜应 ...