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. kuangbin 并查集

    A : Wireless Network  POJ - 2236 题意:并查集,可以有查询和修复操作 题解:并查集 #include<iostream> #include<cstdi ...

  2. 并查集:HDU5326-Work(并查集比较简单灵活的运用)

    Work HDU原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=5326 Time Limit: 2000/1000 MS (Java/Others) M ...

  3. 从头开始学习数据库及ADO.NET之PostgreSql字段约束——竹子整理

    约束数据表列执行的规则.这些是用来防止无效的数据被输入到数据库中..这确保数据库中的数据的准确性和可靠性. 约束可以是列级或表级.仅适用于表级约束被应用到整个表的列级约束.为列定义的数据类型,本身是一 ...

  4. HDU 4919 Exclusive or 数学

    题意: 定义 \[f(n)=\sum\limits_{i=1}^{n-1}(i\oplus (n-i))\] 求\(f(n),n \leq 10^{500}\) 分析: 这个数列对应OEIS的A006 ...

  5. JSP自定义tld方法标签

    卧槽 我们可以通过tld文件,自定义一个方法标签,以便在页面中使用,目录通常放在WEB-INF下面的tlds文件夹: 引入方式示例,直接在jsp上引入tld标签文件: <%@ taglib pr ...

  6. Django补充知识点——用户管理

    内容概要 1.Form表单2.Ajax3.布局,Django母板4.序列化5.Ajax相关6.分页7.XSS攻击8.CSRF9.CBV.FBV 10.类中用装饰器的两种方法 11.上传文件 12.数据 ...

  7. linux学习(三) -- lnmp环境切换php版本,并安装相应redis扩展

    原创文章,转载请注明出处   我想配置的环境是ubuntu+nginx+mysql+php+redis,其中php装两个版本,php7和php56 ubuntu+nginx+mysql+php的环境配 ...

  8. STL学习笔记5--map and multimap

    Maps是一种关联式容器,包含“关键字/值”对. Multimaps和maps很相似,但是MultiMaps允许重复的元素. 简单介绍: 1.声明,首先包含头文件 “map” map <int, ...

  9. PyInstaller打包python脚本

    用python写的工具写好了,想打包然后发给测试同事使用,最后选择了PyInstaller,支持Windows.Linux.OS X,支持打包成一个文件夹或单个EXE文件.   我是直接在线安装的,在 ...

  10. Python3 HTMLTestRunner自动化测试报告美化

    # FileName : MyHTMLTestRunner.py # Author : wangyinghao # DateTime : 2019/1/9 21:04 # SoftWare : PyC ...