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. A Bug's Life(削弱版食物链)

    Description Background  Professor Hopper is researching the sexual behavior of a rare species of bug ...

  2. 百度之星初赛A 今夕何夕

    今夕何夕 今天是2017年8月6日,农历闰六月十五. 小度独自凭栏,望着一轮圆月,发出了"今夕何夕,见此良人"的寂寞感慨. 为了排遣郁结,它决定思考一个数学问题:接下来最近的哪一年 ...

  3. nova hypervisor接口添加host_ip字段

    云平台系统用户提出一个需求,要求根据物理机主机名或者IP查询其上虚拟机列表.根据主机名查询好办,nova的list接口提供了host参数:按主机IP查询就不那么直接了,需要先将IP反解析成主机名,然后 ...

  4. sql优化系列3(收集来源http://bbs.csdn.net/topics/250004467)

    如何加快查询速度? 1.升级硬件   2.根据查询条件,建立索引,优化索引.优化访问方式,限制结果集的数据量. 3.扩大服务器的内存 4.增加服务器CPU个数 5.对于大的数据库不要设置数据库自动增长 ...

  5. IOS开发---菜鸟学习之路--(二十一)-利用正则表达式解析URL获取其中的参数

    因为项目需要解析URL当中参数的部分,在网上搜索了一下都没有相关的资料. 然后就自己写了一个 其实我就是通过正则表达式来处理URL 进行解析的 好了直接上代码吧 也是非常的简单,大家拷贝过去就可以使用 ...

  6. python矩阵和向量的转置问题

    numpy有很多方法进行转置,这里由于时间和精力限制(主要是我实在比较懒,有一个基本上一直能使的,就懒得看其他的了),其他方法我没研究,这里我总结的东西,如果有问题,欢迎各路大佬拍砖 一.创建矩阵: ...

  7. js日期处理

    Js获取当前日期时间及其它操作 var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整 ...

  8. HLG1125 循环小数2

    循环小数 II Time Limit: 1000 MS Memory Limit: 65536 K Total Submit: 155(55 users) Total Accepted: 92(51 ...

  9. Weixin API -- 微信js接口

    今天在开发项目的时候,由于需要在微信中实现分享功能(分享成功后实现页面跳转并记录).问度娘,找了很久,终于找到一个不错的方法.收藏起来以后备用,也希望对大家有所帮助! 在github的地址:https ...

  10. POJ2152 Fire 【树形dp】

    题目链接 POJ2152 题解 经典老题,还真暴力 \(n \le 1000\),所以可以\(O(n^2)\)做 所以可以枚举每个点依附于哪一个点 设\(f[u]\)表示以\(u\)为根的子树的最小代 ...