hdu 1595(最短路变形好题)
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
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.
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.
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
#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(最短路变形好题)的更多相关文章
- hdu 3986(最短路变形好题)
Harry Potter and the Final Battle Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/6553 ...
- HDU 2544 最短路(模板题——Floyd算法)
题目: 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你 ...
- HDU 2544最短路dijkstra模板题
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- HDU 1596 最短路变形
这道题怎么都是TLE,报警了,先放在这 http://acm.hdu.edu.cn/showproblem.php?pid=1596 #include <iostream> #includ ...
- UESTC 30 &&HDU 2544最短路【Floyd求解裸题】
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 2544 最短路 【Dijkstra模板题】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2544 思路:最短路的模板题 Dijkstra 算法是一种类似于贪心的算法,步骤如下: 1.当到一个点时, ...
- 最短路变形题目 HDU多校7
Mr.Quin love fishes so much and Mr.Quin’s city has a nautical system,consisiting of N ports and M sh ...
- ACM: HDU 2544 最短路-Dijkstra算法
HDU 2544最短路 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
- POJ-2253.Frogger.(求每条路径中最大值的最小值,最短路变形)
做到了这个题,感觉网上的博客是真的水,只有kuangbin大神一句话就点醒了我,所以我写这篇博客是为了让最短路的入门者尽快脱坑...... 本题思路:本题是最短路的变形,要求出最短路中的最大跳跃距离, ...
随机推荐
- python3.7 time模块
#!/usr/bin/env python __author__ = "lrtao2010" #python3.7 time模块 #time模块没有time.py文件,是内置到解释 ...
- psutil——获取系统信息的Python第三方模块
本文摘自廖雪峰大神个人网站:https://www.liaoxuefeng.com/wiki/1016959663602400/1183565811281984 用Python来编写脚本简化日常的运维 ...
- manjaro中文输入法已安装但切换不了解决方法
情况如图所示,输入法安装了,但Ctrl+空格键或者鼠标选择切换都不行 解决方法: 打开家目录下面的.xprofile文件,如果没有这个文件就新建一个,加入下面内容 保存文件,退出. 重启电脑就可以了
- Labyrinth POJ - 1383
Labyrinth POJ - 1383 The northern part of the Pyramid contains a very large and complicated labyrint ...
- 笔记-编程-IO模型
笔记-编程-IO模型 1. 简介 常用IO模型 1) 同步阻塞IO(Blocking IO) 2) 同步非阻塞IO(Non-blocking IO) 3) IO ...
- 快速从mysqldump文件中恢复一个表
快速从较大的mysqldump文件中恢复一个表到数据库中: 1.先获取目标表(md_gas_check_record)在文件中的位置 [publish@LF-PRO-DB-01 ~]$ cat dby ...
- JVM——Java类加载机制总结
)解析:解析阶段是把虚拟机中常量池的符号引用替换为直接引用的过程. 2.3 初始化 类初始化时类加载的最后一步,前面除了加载阶段用户可以通过自定义类加载器参与以外,其余都是虚拟机主导和控制.到了初始化 ...
- cf976d Degree Set
ref #include <algorithm> #include <iostream> #include <cstdio> #include <vector ...
- Stephen 博客正式开通 【个人公众号:Stephen 】
个人博客开通. 个人公众号:Stephen
- Leetcode 542.01矩阵
01矩阵 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 1: 输入: 0 0 0 0 1 0 0 0 0 输出: 0 0 0 0 1 0 ...