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 ...
随机推荐
- linux查找某个文件中单词出现的次数
文件名称:list 查找单词名称:test 操作命令: (1)more list | grep -o test | wc -l (2)cat list | grep -o test | wc -l ( ...
- Linux/Unix 系统分析命令速查手册
1.Hardware CPU information: cat /proc/cpuinfo 物理core个数: 统计core 逻辑CPU个数:统计processor Memory informatio ...
- Linux内核与根文件系统的关系1
Linux内核与根文件系统的关系开篇题外话:对于Linux初学者来说,这是一个很纠结的问题,但这也是一个很关键的问题!一语破天机: “尽管内核是 Linux 的核心,但文件却是用户与操作系统交互所采用 ...
- ORACLE和SQL SERVER的数据同步常用方法
ORACLE和SQL SERVER的数据同步常用方法 1. 自己编程,或者第三方工具2. 在sqlserver中,使用linkedserver,访问oracle,然后编写job进行数据同步3. 在or ...
- poj 1860 Currency Exchange (最短路bellman_ford思想找正权环 最长路)
感觉最短路好神奇呀,刚开始我都 没想到用最短路 题目:http://poj.org/problem?id=1860 题意:有多种从a到b的汇率,在你汇钱的过程中还需要支付手续费,那么你所得的钱是 mo ...
- Java实现文件复制
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /** * & ...
- javascript 库
http://overapi.com/javascript/ 查javascript http://slimerjs.org/ 自动测试? http://www.cnblogs.com/lhb25/p ...
- JPA简单知识
,JPA(Java Persistence API):通过注解或XML描述对象--关系表的映射关系,并将运行期的实体对象持久化到数据库中. JPA是一套规范,不是某个ORM产品,它主要包括以下3方面的 ...
- WCF发布后远程访问的域名解析问题
环境: VS2010 sp1,.net framework 4.0,windows server 2003 x64 ,iis 6.0 症状: WCF开发测试,本地调用都正常.发布后,在浏览器中访问ht ...
- jvm常量池 vsv为什么1000 == 1000返回为False,而100 == 100会返回为True?
在jvm规范中,每个类型都有自己的常量池.常量池是某类型所用常量的一个有序集合,包括直接常量(基本类型,String)和对其他类型.字段.方法的符号引用.之所以是符号引用而不是像c语言那样,编译时直接 ...