find the longest of the shortest

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

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
题意:从1到n点如果切掉一条路,那么在最坏情况下的最短路是多少??
题解:这个题开始的时候没想请,去枚举每一条边,然后超时。后来发现如果被堵死的路不是最短路中的某条,那么求的最短路肯定就还会是原来的最短路,所以我们只要枚举最短路中的每段路,然后再求最短路就行了。
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <queue>
using namespace std;
const int N = ;
const int M = ;
const int INF = ;
/*struct Node{
int u,v;
}node[M];*/
int graph[N][N];
bool vis[N];
int low[N];
int pre[N];
int n,m;
int dijkstra(int s,bool flag){
for(int i=;i<=n;i++){
if(flag){
pre[i] = s;
}
low[i] = graph[s][i];
vis[i] = false;
}
vis[s] = true;
for(int i=;i<n;i++){
int Min = INF;
for(int j=;j<=n;j++){
if(low[j]<Min&&!vis[j]){
Min = low[j];
s = j;
}
}
vis[s] = true;
for(int j=;j<=n;j++){
if(low[j]>low[s]+graph[s][j]&&!vis[j]){
low[j] = low[s]+graph[s][j];
if(flag){
pre[j] = s;
}
}
}
}
return low[n];
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(i==j) graph[i][j] = ;
else graph[i][j] = INF;
}
}
for(int i=;i<m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
graph[u][v]=graph[v][u] = min(w,graph[u][v]);
//scanf("%d%d%d",&node[i].u,&node[i].v,&w);
//graph[node[i].u][node[i].v]=graph[node[i].v][node[i].u] = min(w,graph[node[i].u][node[i].v]);
}
int res = dijkstra(,);
int Max = -;
/*for(int i=0;i<m;i++){ ///枚举每一段TLE
int temp = graph[node[i].u][node[i].v];
graph[node[i].u][node[i].v]=graph[node[i].v][node[i].u] = INF;
int cost = dijkstra(1);
Max = max(cost,Max);
graph[node[i].u][node[i].v]=graph[node[i].v][node[i].u] = temp;
}*/
int temp = n;
while(temp!=){
int k = graph[temp][pre[temp]];
graph[pre[temp]][temp] = graph[temp][pre[temp]] = INF;
int cost = dijkstra(,);
Max = max(cost,Max);
graph[pre[temp]][temp] = graph[temp][pre[temp]] =k;
temp = pre[temp];
}
printf("%d\n",Max);
}
}

----update----

利用链式前向星可以考虑到重边效果,虽然这里并没有.

#include <string.h>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <queue>
using namespace std;
const int maxn = ;
const int INF = 0xfffffff;
int n,m;
struct Edge{
int u,v,w;
int next;
}edge[maxn*maxn];
int id[maxn*maxn]; //如果当前最短路中包含点v,那么 id[v] 则是 v 所用到的边的编号.
int head[maxn],tot = ;
void addedge(int u,int v,int w,int &k){
edge[k].v = v,edge[k].w = w;
edge[k].next = head[u],head[u] = k++;
}
void init(){
memset(head,-,sizeof(head));
memset(id,-,sizeof(id));
tot = ;
}
bool vis[maxn];
int d[maxn],pre[maxn];
int spfa(int s,int t,bool flag){
memset(vis,false,sizeof(vis));
for(int i=;i<=maxn;i++){
d[i] = INF;
if(flag){
pre[i] = s;
}
}
queue<int> q;
q.push(s);
vis[s] = true;
d[s] = ;
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int k=head[u];k!=-;k=edge[k].next){
int v = edge[k].v,w=edge[k].w;
if(d[u]+w<d[v]){
d[v] = d[u]+w;
if(!vis[v]){
vis[v] = true;
q.push(v);
}
if(flag){
pre[v] = u;
id[v] = k;
}
}
}
}
if(d[t]>=INF) return -;
return d[t];
}
int main(){
while(scanf("%d %d",&n,&m)!=EOF){
init();
for(int i=;i<m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w,tot);
addedge(v,u,w,tot);
}
int res;
res = spfa(,n,);
int MAX = res;
int temp=n;
while(temp!=){
int w = edge[id[temp]].w;
edge[id[temp]].w = INF;
MAX = max(spfa(,n,),MAX);
edge[id[temp]].w = w;
temp = pre[temp];
}
printf("%d\n",MAX);
}
return ;
}

hdu 1595(最短路变形好题)的更多相关文章

  1. hdu 3986(最短路变形好题)

    Harry Potter and the Final Battle Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/6553 ...

  2. HDU 2544 最短路(模板题——Floyd算法)

    题目: 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你 ...

  3. HDU 2544最短路dijkstra模板题

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  4. HDU 1596 最短路变形

    这道题怎么都是TLE,报警了,先放在这 http://acm.hdu.edu.cn/showproblem.php?pid=1596 #include <iostream> #includ ...

  5. UESTC 30 &&HDU 2544最短路【Floyd求解裸题】

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. HDU 2544 最短路 【Dijkstra模板题】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2544 思路:最短路的模板题 Dijkstra 算法是一种类似于贪心的算法,步骤如下: 1.当到一个点时, ...

  7. 最短路变形题目 HDU多校7

    Mr.Quin love fishes so much and Mr.Quin’s city has a nautical system,consisiting of N ports and M sh ...

  8. ACM: HDU 2544 最短路-Dijkstra算法

    HDU 2544最短路 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descrip ...

  9. POJ-2253.Frogger.(求每条路径中最大值的最小值,最短路变形)

    做到了这个题,感觉网上的博客是真的水,只有kuangbin大神一句话就点醒了我,所以我写这篇博客是为了让最短路的入门者尽快脱坑...... 本题思路:本题是最短路的变形,要求出最短路中的最大跳跃距离, ...

随机推荐

  1. Building a Space Station POJ - 2031

    Building a Space Station POJ - 2031 You are a member of the space station engineering team, and are ...

  2. POJ3436------ACM Computer Factory

    题目链接 ACM Computer Factory Description As you know, all the computers used for ACM contests must be i ...

  3. this.$router 和this.$route 的区别

    1. this.$router: 表示全局路由器对象,项目中通过router路由参数注入路由之后,在任何一个页面都可以通过此方法获取到路由器对象,并调用其push(), go()等方法: 2. thi ...

  4. 第八届蓝桥杯C/C++ B组省赛----分巧克力

    分巧克力 问题描述 儿童节那天有K位小朋友到小明家做客.小明拿出了珍藏的巧克力招待小朋友们. 小明一共有N块巧克力,其中第i块是Hi x Wi的方格组成的长方形. 为了公平起见,小明需要从这 N 块巧 ...

  5. Java-读取txt生成excel

    本段代码的目的是从txt文本中读取相应格式的数据,然后写入到对应格式的excel文档中 在敲本段代码的时候,也学习了一些其它知识点,如下: 1.byte[] b_charset= String.get ...

  6. oracle 基本函数

    1)字符串函数---length()函数 用于返回字符串长度  select t.name,length(t.name) from tb_person t 2)向左补全字符串---LPAD()函数 L ...

  7. java.util.ArrayList与java.util.Arrays$ArrayList区别

    本博客转载自:https://blog.csdn.net/maywehe/article/details/52553954 写demo的时候,为了避免用list.add方法,特意写了个数组然后转换成l ...

  8. Set-DnsServerGlobalQueryBlockList

    Set-DnsServerGlobalQueryBlockList Windows Server Technical Preview and Windows 10   Other Versions   ...

  9. leetcode 【 Remove Element 】python 实现

    题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...

  10. iOS-@inerface的11条规范写法

    总结一些interface声明时的规范,相关宏的介绍,定义方法时有用的修饰符,编写注释的规范,最终写出一个合格的头文件. 1.读写权限 1.1实例变量的@public,@protected,@priv ...