find the longest of the shortest

Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2424    Accepted Submission(s): 846

Problem Description
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.
 
Input
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. 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.
 
Output
In 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
 
Author
ailyanlu
 
Source
本题的大概题意是先求出最短路,之后再最短路中依次删掉每一条边,再求最短路,取最长的便是结果
dijkstra算法代码实现
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
const int INF=0x7fffffff;
int map[maxn][maxn];
bool vis[maxn];
int pre[maxn];
int n,m;
int dis[maxn];
void dijkstra(int start){
for(int i=;i<=n;i++)
dis[i]=INF;
memset(vis,false,sizeof(vis));
dis[]=;
for(int i=;i<=n;i++){
int k=-;
int tmin=INF;
for(int j=;j<=n;j++){
if(!vis[j]&&dis[j]<tmin){
tmin=dis[j];
k=j;
}
} vis[k]=true;
for(int j=;j<=n;j++){
if(map[k][j]!=INF)
if(!vis[j]&&dis[k]+map[k][j]<dis[j]){
dis[j]=dis[k]+map[k][j];
if(start)
pre[j]=k;
}
}
}
} int main(){
while(scanf("%d%d",&n,&m)!=EOF){ for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(i==j)
map[i][j]=;
else
map[i][j]=map[j][i]=INF;
}
} int u,v,w;
for(int i=;i<=m;i++){
scanf("%d%d%d",&u,&v,&w);
map[u][v]=map[v][u]=w;
}
memset(pre,,sizeof(pre));
dijkstra();
int ans=dis[n];
// printf("---->%d\n",ans);
for(int i=n;i!=;i=pre[i]){
int temp=map[i][pre[i]];
map[i][pre[i]]=INF;
map[pre[i]][i]=INF;
dijkstra();
if(dis[n]>ans)
ans=dis[n];
// printf("--->%d\n",temp);
map[i][pre[i]]=temp;
map[pre[i]][i]=temp;
}
printf("%d\n",ans); }
return ;
}

spfa算法实现

#include<stdio.h>
#include<queue>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std;
const int MAXN=;
const int INF=0x7fffffff;
struct Edge
{
int v;
int cost;
Edge(int _v=,int _cost=):v(_v),cost(_cost) {}
};
vector<Edge>E[MAXN];
void addedge (int u,int v,int w)
{
E[u].push_back(Edge(v,w));
E[v].push_back(Edge(u,w));
}
bool vis[MAXN];//在队列标志
int dist[MAXN];
int pre[MAXN];
int n,m;
void spfa(int x,int y,int judge)
{
memset(vis,false,sizeof(vis));
for(int i=; i<=n; i++)
dist[i]=INF;
vis[]=true;
dist[]=;
queue<int>que;
while(!que.empty())
que.pop();
que.push();
while(!que.empty())
{
int u=que.front();
que.pop();
vis[u]= false;
for(int i=; i<E[u].size(); i++)
{
int v=E[u][i].v;
if((u==x&&v==y)||(u==y&&v==x))
continue;
if(dist[v]>dist[u]+E[u][i].cost)
{
dist[v]=dist[u]+E[u][i].cost;
if(judge)
pre[v]=u;
if(!vis[v])
{
vis[v]= true;
que.push(v);
}
}
}
} }
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=;i<=n;i++)
E[i].clear();
int u,v,w;
for(int i=;i<=m;i++ ){
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
}
memset(pre,,sizeof(pre));
spfa(,,);
int ans=dist[n];
for(int i=n;i!=;i=pre[i]){
spfa(i,pre[i],);
int temp=dist[n];
if(temp>ans)
ans=temp;
}
printf("%d\n",ans);
}
return ;
}

hdu1595 最短路问题(dijkstra&&spfa)的更多相关文章

  1. 最短路问题 Floyd+Dijkstra+SPFA

    参考博客:https://blog.csdn.net/qq_35644234/article/details/60875818 题目来源:http://acm.hdu.edu.cn/showprobl ...

  2. POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)

    传送门 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46727   Acce ...

  3. 怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33015   Accepted ...

  4. 图上最短路(Dijkstra, spfa)

    单源最短路径 题目描述 如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 输入输出格式 输入格式: 第一行包含三个整数N.M.S,分别表示点的个数.有向边的个数.出发点的编号. 接下来 ...

  5. hdu 2066 ( 最短路) Floyd & Dijkstra & Spfa

    http://acm.hdu.edu.cn/showproblem.php?pid=2066 今天复习了一下最短路和最小生成树,发现居然闹了个大笑话-----我居然一直写的是Floyd,但我自己一直以 ...

  6. 几个小模板:topology, dijkstra, spfa, floyd, kruskal, prim

    1.topology: #include <fstream> #include <iostream> #include <algorithm> #include & ...

  7. dijkstra spfa prim kruskal 总结

    最短路和最小生成树应该是很早学的,大家一般都打得烂熟,总结一下几个问题 一  dijkstra  O((V+E)lgV) //V节点数 E边数 dijkstra不能用来求最长路,因为此时局部最优解已经 ...

  8. HDU Today HDU杭电2112【Dijkstra || SPFA】

    http://acm.hdu.edu.cn/showproblem.php?pid=2112 Problem Description 经过锦囊相助,海东集团最终度过了危机,从此.HDU的发展就一直顺风 ...

  9. find the safest road HDU杭电1596【Dijkstra || SPFA】

    pid=1596">http://acm.hdu.edu.cn/showproblem.php?pid=1596 Problem Description XX星球有非常多城市,每一个城 ...

随机推荐

  1. Maven 中maven-assembly-plugin插件的使用笔记 SpringBoot环境

    首先创建一个多模块的SpringBoot项目 项目结构 父pom的内容如下: <?xml version="1.0" encoding="UTF-8"?& ...

  2. python2含有中文路径报错解决办法[\xe4\xbf\xa1\xe6\x81\xaf]

    如图所示 百度的解决办法大多数是针对python3版本的,在脚本开头加# -*- coding:utf-8 -*-,但是python2版本加了编码格式,还是报错,具体解决办法是:path =unico ...

  3. Ubuntu 16.04 换国内源

    官方渠道,图形界面,操作简单,可以说对新手及其友好!! 依次打开:搜索,软件与更新,第一个和第三个勾上,下载自,其它,然后在中国条目下选择你想使用的镜像站点,然后点“选择服务器”,然乎点击“关闭”,选 ...

  4. 字符编码:Unicode和UTF-8的关系

    今天中午,我突然想搞清楚Unicode和UTF-8之间的关系,于是就开始在网上查资料. 结果,这个问题比我想象的复杂,从午饭后一直看到晚上9点,才算初步搞清楚. 下面就是我的笔记,主要用来整理自己的思 ...

  5. 标签中的name属性和ID属性的区别

    标签中的name属性和ID属性的区别 2018年05月13日 10:17:44 tssit 阅读数:760   编程这么久,细想了一下,发现这个问题还不是很清楚,汗!看了几篇文章,整理了一下,分享下! ...

  6. scanf()的使用

    scanf函数称为格式输入函数,即按用户指定的格式从键盘上把数据输入到指定的变量之中. 如下面代码: #include<stdio.h> int main() { int a,b; sca ...

  7. python-判断alter是否存在

    from selenium import webdriver import time from selenium.webdriver.support.ui import WebDriverWait f ...

  8. C#语言命名的9种规范

    下面介绍C#语言命名的9种规范: a) 类 [规则1-1]使用Pascal规则命名类名,即首字母要大写. [规则1-2]使用能够反映类功能的名词或名词短语命名类. [规则1-3]不要使用“I”.“C” ...

  9. shell脚本,awk在需要的行上打打印空行。

    注解: 判断每行中是否包含字母a,包含了,就将$1的值赋值给变量a,然后判断变量a是否存在,存在打印一个空行,在将变量的值使用空变量b赋值,最后在打印输出. 结果就是在包含有字符a的行上打印一个空行.

  10. [BZOJ] 4145: [AMPPZ2014]The Prices

    设\(f[S][i]\)表示考虑到第\(i\)家店,已经买了集合\(S\)内的物品 一个朴素的想法是枚举子集转移 \[ f[S][i]=\min\{f[T][i-1]+cost[S\oplus T][ ...