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

给定时间是5s,不会超时

题意:

给出的所有路中存在一条路正在修建

要从1走到n,找出最短路径的最大长度

思路:

先一遍dijkstra找到最短路径长度,并且记录路径

再逐个删掉通向最短路径的每一条边换成别的能够到达终点的路

计算其最大长度

难点:在记录路径上

 #include<iostream>
#include<string.h>
#include<cmath>
#include<iomanip>
#define inf 0x3f3f3f3f
#define ios() std::ios::sync_with_stdio(false)
#define cin() cin.tie(0)
#define cout() cout.tie(0)
#define mem(a) memset(a,0,sizeof(a))
using namespace std; int vertex[];//记录首先求得的最短路径所经过的顶点
int n,m;//n顶点总个数,m是边数
int e[][];
int dis[],dis1[],dis2[];
int book[];
int flag; void init()
{
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
if(i==j)
e[i][j]=;
else
e[i][j]=inf;
}
}
} int dijkstra(int *dis)
{
mem(book);mem(dis1);mem(dis2);
for(int i=;i<=n;i++)
dis[i]=e[][i];
book[]=;
int u;
for(int i=;i<=n;i++)
{
int minn=inf;
for(int j=;j<=n;j++)
{
if(book[j]==&&dis[j]<minn)
{
u=j;
minn=dis[j];
}
}
book[u]=;
for(int k=;k<=n;k++)
{
if(e[u][k]<inf&&dis[u]+e[u][k]<dis[k])
{
dis[k]=dis[u]+e[u][k];
if(flag)//如果flag=1,则开始记录最短路的路径
{
vertex[k]=u;//记录顶点
}
}
}
}
return dis[n];
} int main()
{
ios();cin();cout();
int x,y,z;
while(cin>>n>>m)
{
mem(dis);mem(vertex);mem(e);
init();
for(int i=; i<m; i++)
{
cin>>x>>y>>z;
e[x][y]=z;
e[y][x]=z;
} flag=;mem(dis1);
fill(vertex,vertex+,);
int shortest=dijkstra(dis1);
// cout<<shortest<<"**"<<endl; // for(int i=n;i>=2;i--)
// cout<<vertex[i]<<" "; flag=;//通过控制flag决定是否记录路径
int w=n;//一步步倒回去替换掉每一段的距离且变成无穷大,寻找其他边
int ans=shortest;
while(w!=)// 通过while去控制是否已经倒退回去遍历到所有的边
{
// cout<<"***"<<endl;
int frontt=vertex[w];
int ori=e[frontt][w];
e[frontt][w]=e[w][frontt]=inf;//第一次进去先变最后一条边
mem(dis2);
int ww=dijkstra(dis2);
// if(ww!=inf)
ans=max(ww,ans);
e[frontt][w]=e[w][frontt]=ori;
w=frontt;
// 变成无穷大之后再进行下一次找的时候可能会需要到这条边,所以需要恢复
}
cout<<ans<<endl;
}
return ;
}

HDU1595-find the longest of the shortest-dijkstra+记录路径的更多相关文章

  1. hdu1595 find the longest of the shortest(Dijkstra)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1595 find the longest of the shortest Time Limit: 100 ...

  2. 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板)

    layout: post title: 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板) author: "luowentaoaa" catalo ...

  3. hdu 1595 find the longest of the shortest(dijkstra)

    Problem Description Marica is very angry with Mirko because he found a new girlfriend and she seeks ...

  4. 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 do ...

  5. [HDU1595] find the longest of the shortest

    题目链接: 点我 题意: 给定一个\(n\)个点,\(m\)条边的带权无向图,起点为\(1\),终点为\(n\),现在可以删去其中的一条边,求一种删边方案使得剩下图的最短路值最大,输出这个最短路的长度 ...

  6. 【PAT甲级】1087 All Roads Lead to Rome (30 分)(dijkstra+dfs或dijkstra+记录路径)

    题意: 输入两个正整数N和K(2<=N<=200),代表城市的数量和道路的数量.接着输入起点城市的名称(所有城市的名字均用三个大写字母表示),接着输入N-1行每行包括一个城市的名字和到达该 ...

  7. hdu 1595 find the longest of the shortest【最短路枚举删边求删除每条边后的最短路,并从这些最短路中找出最长的那条】

    find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  8. find the longest of the shortest (hdu 1595 SPFA+枚举)

    find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  9. hdu 1595 find the longest of the shortest(迪杰斯特拉,减去一条边,求最大最短路)

    find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  10. HDU 1595 find the longest of the shortest【次短路】

    转载请注明出处:http://blog.csdn.net/a1dark 分析:经典的次短路问题.dijkstra或者SPFA都能做.先找出最短路.然后依次删掉没条边.为何正确就不证明了.了解思想直接A ...

随机推荐

  1. 实现Tab键的空格功能

    有时使用编辑框需要用到按Tab键空两格,可能这时Tab键的功能不是空格而是页面切换等,这时需要设置: $(document).bind('keydown', function (event) { if ...

  2. CSS3 图形变换

    1.zoom  和  transform:scale  的区别   :    http://www.zhangxinxu.com/wordpress/2015/11/zoom-transform-sc ...

  3. php开发面试题---游戏面向对象设计与分析实例

    php开发面试题---游戏面向对象设计与分析实例 一.总结 一句话总结: 不要光空想,多看几个实例就知道自己的游戏该怎么设计了 根据实例去理解面向对象编程的的六大原则 1.英雄种类分别有:战士.法师. ...

  4. 2 USB标准请求

    2 USB标准设备请求的结构 2.1 标准请求 bmRequestType 的D6~D5为00的请求,USB协议定义了11个标准请求(bRequest),其名字与相应的bRequest的值如下表: 表 ...

  5. MySQL数据库(二)——库相关操作、表相关操作(一)、存储引擎、数据类型

    库相关操作.表相关操作(一).存储引擎.数据类型 一.库相关操作 1.创建数据库 (1)语法 create database 数据库 charset utf8; (2)数据库命名规范 可以由字母.数字 ...

  6. 【Linux】- CentOS安装docker及docker-compose

    1.安装docker,命令如下: -- 把yum包更新到最新 yum update -- 安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicema ...

  7. XX Russia Team Open, High School Programming Contest St Petersburg, Barnaul, Tbilisi, Almaty, Kremenchug, November 30, 2019

    ContestLink easy: AFI medium-easy: BDH medium: CGKL ???: EJ A. Attractive Flowers 签到. B. Blocking th ...

  8. Avito Cool Challenge 2018 C - Colorful Bricks

    题目大意: 1*n的格子 可以用m种颜色涂色 已知从第2开始到第n个格子 有k个格子与其左边的格子颜色不同 求涂色的方案数 相当于把n个格子分成k+1份 可以递推出分成k+1份的不同的方案数(其实递推 ...

  9. Cyclical Quest CodeForces - 235C 后缀自动机

    题意: 给出一个字符串,给出一些子串,问每个子串分别在母串中圆环匹配的次数, 圆环匹配的意思是将该子串拆成两段再首位交换相接的串和母串匹配,比 如aaab变成baaa,abaa,aaba再进行匹配. ...

  10. spring boot jpa 多表关联 @OneToOne @OneToMany @ManyToOne@ManyToMany

    1.一对一关联 @OneToOne import lombok.Data; import javax.persistence.*; /** * @Author: GWL * @Description: ...