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整除. 思路:就是 ...
随机推荐
- Linux Java的环境变量搭建
JAVA JDK:http://www.oracle.com/technetwork/java/javase/downloads/index.html 下载完后,解压完并将其中的jdk文件夹移动到/u ...
- 判断一个int 型整数 是否为回文数
leetcode 上的题目 Determine whether an integer is a palindrome. Do this without extra space. 由于不能使用额外空间, ...
- JS学习笔记-数组
ECMAScript中没有提供类和接口等的定义,但它却是一门面向对象的语言,由于它能够通过其它 方式实现类似高级语言的面向对象功能,这些内容将在后面的文章中进行一步步的总结.此篇仅对JS中对象作简要说 ...
- [Immutable.js] Working with Subsets of an Immutable.js Map()
Immutable.js offers methods to break immutable structures into subsets much like Array--for instance ...
- css中的伪类
伪类用于向某些选择器添加一些特殊效果. 1):focus 伪类在元素获得焦点的时向元素添加特殊样式.一般用于输入文本域,按钮,以及超链接. a:focus{color:red;}超链接字体为红色 in ...
- CSS - 关于li中图文混排不能垂直居中的问题
图片和文字一起放在li标签下不能同时垂直居中 解决办法: 1.设置图片的position:absolute; 2.把文字加上span标签: span{ height:30px;line-heigh ...
- 鼠标滚轮(mousewheel)和DOMMouseScroll事件
IE6.0首先实现了mousewheel事件.此后,Opera.Chrome和Safari也都实现了这个事件.当用户通过鼠标滚轮与页面交互.在垂直方向上滚动页面时(无论向下还是向上),就会触发mous ...
- ListHelper
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data; ...
- Android开发实现透明通知栏
这个特性是andorid4.4支持的,最少要api19才可以使用,也就是说如果Android的机子是低于4.4,沉浸通知栏是没有效果的.下面介绍一下使用的方法,非常得简单. public void i ...
- js 日期天数相加减,格式化yyyy-MM-dd
参数格式: date :2016-03-02 days:-3(2)当为负数的时候日期往前推,为正数,日期往后推 function addDate(date, days) { var d = new D ...