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 K (Java/Others)
Total Submission(s): 2524 Accepted Submission(s):
888
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.
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.
time in minutes, it could take Marica to come to Mirko.
5 7
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAX 1010
#define INF 0x3f3f3f
using namespace std;
int low[MAX];
int map[MAX][MAX];
int vis[MAX],p[MAX];
int n,m;
void init()
{
int i,j;
for(i=0;i<MAX;i++)
for(j=0;j<MAX;j++)
map[i][j]=i==j?0:INF;
}
int dj(int v)
{
int i,j,min,next;
memset(vis,0,sizeof(vis));
memset(low,INF,sizeof(low));
low[1]=0;
for(i=1;i<=n;i++)
{
min=INF;
for(j=1;j<=n;j++)
{
if(!vis[j]&&min>low[j])
{
next=j;
min=low[j];
}
}
vis[next]=1;
for(j=1;j<=n;j++)
{
if(!vis[j]&&low[j]>low[next]+map[next][j])
{
low[j]=low[next]+map[next][j];
if(v)
p[j]=next;//记录寻找最短路时i的上一个点的位置
}
}
}
return low[n];
}
int main()
{
int a,b,c,i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
if(map[a][b]>c)
map[a][b]=map[b][a]=c;
}
memset(p,0,sizeof(p));
int maxx=dj(1);
for(i=n;i!=1;i=p[i])//倒着遍历最短路
{
int x=map[i][p[i]];
map[i][p[i]]=map[p[i]][i]=INF;
maxx=max(maxx,dj(0));
map[i][p[i]]=map[p[i]][i]=x;
}
printf("%d\n",maxx);
}
return 0;
}
hdu 1595 find the longest of the shortest【最短路枚举删边求删除每条边后的最短路,并从这些最短路中找出最长的那条】的更多相关文章
- 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(迪杰斯特拉,减去一条边,求最大最短路)
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://blog.csdn.net/a1dark 分析:经典的次短路问题.dijkstra或者SPFA都能做.先找出最短路.然后依次删掉没条边.为何正确就不证明了.了解思想直接A ...
- 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 ...
- hdu 1595 find the longest of the shortest
http://acm.hdu.edu.cn/showproblem.php?pid=1595 这道题我用spfa在枚举删除边的时候求最短路超时,改用dijkstra就过了. #include < ...
- leetcode 5 :Longest Palindromic Substring 找出最长回文子串
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- FCC JS基础算法题(3):Find the Longest Word in a String (找出最长单词)
题目描述: 在句子中找出最长的单词,并返回它的长度.函数的返回值应该是一个数字. 基本思路,将字符串转换成数组,然后得出数组中单个元素的长度,对长度进行排序,返回最大的一个 代码: function ...
- 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 ...
随机推荐
- 使用nginx做为静态服务器--监听两个域名配置
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; ...
- ANDROID_MARS学习笔记_S01_011ProgressBar
文档是这样来设置样式 <ProgressBar android:layout_width="wrap_content" android:layout_height=" ...
- 【HDOJ】1979 Fill the blanks
预处理+搜索剪枝.4*4的边界上的数字必须是奇数. /* 1979 */ #include <iostream> #include <sstream> #include < ...
- jquery网页倒计时效果,秒杀,限时抢购!
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- oracle 根据汉字返回拼音函数
参见戴明明的博客,oracle 根据汉字返回拼音函数,由于他的博客里没有提供完整的代码,研究了一个多小时,才弄出来: 上来贴代码吧.. --------------Type Definition CR ...
- POJ 2135 Farm Tour(最小费用最大流,变形)
题意:给一个无向图,FJ要从1号点出发到达n号点,再返回到1号点,但是路一旦走过了就会销毁(即回去不能经过),每条路长度不同,那么完成这趟旅行要走多长的路?(注:会有重边,点号无序,无向图!) 思路: ...
- JSON--List集合转换成JSON对象
转自:http://www.cnblogs.com/xmaomao/p/3184542.html 1. 简单的手动放置 键值对 到JSONObject,然后在put到JSONArray对象里 List ...
- GP(Geoprocessing)服务的发布与调用
转自:http://www.cnblogs.com/gisangela/archive/2011/01/06/1927702.html 1.什么是GP服务 GP服务是Geoprocessing服务的简 ...
- HDU4614 Vases and Flowers 二分+线段树
分析:感觉一看就是二分+线段树,没啥好想的,唯一注意,当开始摆花时,注意和最多能放的比大小 #include<iostream> #include<cmath> #includ ...
- FOJ 1608 Huge Mission 线段树
每个节点维护一个最小值,更新发现如果大于最小值,直接向下更新.速度还可以.. #include<cstdio> #include<algorithm> #include< ...