hdu 1595 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 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.
Each 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. ≤ N ≤ , ≤ M ≤ N*(N-)/. The cities are markedwith numbers from to N, Mirko is located in city , and Marica in city N.
In the next M lines are three numbers A, B and V, separated by commas. ≤ A,B ≤ N, ≤ V ≤ .Those numbers mean that there is a two-way road between cities A and B, and that it is crossable in V minutes.
In the first line of the output file write the maximum time in minutes, it could take Marica to come to Mirko.
题意:城市内有n条路,其中有某条路在修,因为这条路有很多情况,问各种情况下的最短路中最长的是哪条
思路:直接枚举最短路的所有边,将边标记为inf,再跑dijkstra算出最大值即可
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
int dirx[]={,,-,};
int diry[]={-,,,};
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 1006
#define inf 1<<26
int n,m;
int mp[N][N];
int fa[N];
void init(){
for(int i=;i<=n;i++){
fa[i]=-;
for(int j=;j<=n;j++){
if(i==j){
mp[i][j]=;
}
else{
mp[i][j]=inf;
}
}
}
}
int dijkstra(int flag){
int vis[N];
int dis[N];
for(int i=;i<=n;i++){
vis[i]=;
dis[i]=inf;
}
vis[]=;
dis[]=;
int x=n;
int st=;
while(x--){
for(int i=;i<=n;i++){ if(dis[i]>dis[st]+mp[st][i]){
dis[i]=dis[st]+mp[st][i];
if(flag){
fa[i]=st;
}
} }
int minn=inf;
for(int i=;i<=n;i++){
if(!vis[i] && dis[i]<minn){
minn=dis[i];
st=i;
}
}
vis[st]=; } return dis[n];
}
int main()
{
while(scanf("%d%d",&n,&m)==){
init();
for(int i=;i<m;i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(mp[a][b]>c){
mp[a][b]=mp[b][a]=c;
}
}
int ans=dijkstra(); for(int i=n;i!=;i=fa[i]){
int num=mp[i][fa[i]];
mp[i][fa[i]]=mp[fa[i]][i]=inf;
int cnt=dijkstra(); ans=max(ans,cnt);
mp[i][fa[i]]=mp[fa[i]][i]=num;
}
printf("%d\n",ans); }
return ;
}
hdu 1595 find the longest of the shortest(dijkstra)的更多相关文章
- 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 ...
- 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 ...
- hdu 1595 find the longest of the shortest
http://acm.hdu.edu.cn/showproblem.php?pid=1595 这道题我用spfa在枚举删除边的时候求最短路超时,改用dijkstra就过了. #include < ...
- hdu 1595 find the longest of the shortest(dijstra + 枚举)
http://acm.hdu.edu.cn/showproblem.php?pid=1595 大致题意: 给一个图.让输出从中删除随意一条边后所得最短路径中最长的. . 思路: 直接枚举每条边想必是不 ...
- HDU 1595 find the longest of the shortest【次短路】
转载请注明出处:http://blog.csdn.net/a1dark 分析:经典的次短路问题.dijkstra或者SPFA都能做.先找出最短路.然后依次删掉没条边.为何正确就不证明了.了解思想直接A ...
- 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 ...
- 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 ...
- hdu 1595(最短路变形好题)
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- HDU 5373(2015多校7)-The shortest problem(模拟%11)
题目地址:pid=5373">HDU 5373 题意:给你一个数n和操作次数t,每次操作将n的各位数之和求出来放在n的末尾形成新的n,问t次操作后得到的n能否够被11整除. 思路:就是 ...
随机推荐
- JUnit三分钟教程 ---- 快速起步
JUnit三分钟教程 ---- 快速起步 摘自http://lavasoft.blog.51cto.com/62575/65625/ JUnit是个好东西,做大点的项目离不开这东西,实际中用的时候也因 ...
- Python 协程(gevent)
协程,又叫微线程,协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来的时候,恢复先前保存的寄存器上下文和栈.因此: 协程能保留上 ...
- Binarized Neural Networks_ Training Neural Networks with Weights and Activations Constrained to +1 or −1
转载请注明出处: http://www.cnblogs.com/sysuzyq/p/6248953.html by 少侠阿朱
- php 依据字符串生成相应数组方法
php 依据字符串生成相应数组方法 比如: <?php $config = array( 'project|page|index' => 'content', 'project|page| ...
- Git 如何回到过去,然后 再 回到将来
回到过去: git log 然后 git reset --hard commit ID (那段长长代码 40位) 再,回到将来git reflog 然后 git reset --hard 前面那个代码 ...
- CSS基础笔记
之前没有开通好博客,笔记都记录在有道云,今天全部转过来!!! 1.当同一个html元素不止一个样式定义时,内联样式(在html元素内部)拥有最高的优先权:其他如内部样式表(位于<head> ...
- 修改sqlserver2008中表的schema
schema类似命名空间,相同schema中不能有同样的表名,不用schema下可以有相同的表名 修改schema的方法: 在数据库的 安全性->架构 中添加一个新的架构 找到要修改的表,右击设 ...
- Android开发实现透明通知栏
这个特性是andorid4.4支持的,最少要api19才可以使用,也就是说如果Android的机子是低于4.4,沉浸通知栏是没有效果的.下面介绍一下使用的方法,非常得简单. public void i ...
- Win7刷新环境变量
在“我的电脑”->“属性”->“高级”->“环境变量”中增加或修改环境变量后,需重启系统才能使之生效.有没有什么方法可让它即时生效呢? 下面介绍一种方法: 以修改环境变量“PATH” ...
- C#验证类 可验证:邮箱,电话,手机,数字,英文,日期,身份证,邮编,网址,IP (转)
namespace YongFa365.Validator { using System; using System.Text.RegularExpressions; /**//// <summ ...