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大神一句话就点醒了我,所以我写这篇博客是为了让最短路的入门者尽快脱坑...... 本题思路:本题是最短路的变形,要求出最短路中的最大跳跃距离, ...
随机推荐
- 动态规划:HDU2844-Coins(多重背包的二进制优化)
Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- 动态规划:HDU1789-Doing Homework again
Doing Homework again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- Android广告页循环播放
摘要:项目要求做一个广告页,实现几秒更换一次广告页,下方还有指示第几张广告页,同样也支持手动左滑或右滑. 1.准备好粘贴5个有关广告页的类. ①BaseViewPager==>自定义高度的Vie ...
- Android Shader渲染器:BitmapShader图片渲染
public class BitmapShader extends Shader BitmapShader, Shader家族的 专门处理图片渲染的 构造方法: public BitmapShade ...
- python re模块详解
re模块 re模块使用python拥有全部的正则表达式功能 1 2 3 4 re.I(re.IGNORECASE): 忽略大小写(括号内是完整写法) re.M(MULTILINE):(多行模式,改变 ...
- AngularJs快速上手掌握
一.前言 对于前端系列,自然少不了AngularJs的介绍了.在前面文章中,我们介绍了如何使用KnockoutJs来打造一个单页面程序,后面一篇文章将介绍如何使用AngularJs的开发一个单页面应用 ...
- nmon系统监控
nmon系统监控篇 本文目录 1 nmon的安装 2 nmon控制台指令 3 输出监控文件 一 下载nmon 本次使用的是nmon_linux_14i.tar.gz 二 放入linux中后解压 gzi ...
- MessageBox:弹出窗口
Ext.onReady(function () { Ext.MessageBox.alert("提示信息!","Hello World!"); }); Ext, ...
- POJ 3368:Frequent values(线段树区间合并)
题目大意,给出一段非降序列,求一些区间中出现频率最高的数的出现次数. 分析: 显然,区间中一个数多次出现必然是连续的,也就是最长的连续相等的一段. 用线段树解决,维护三个信息:一个区间最长连续的区间的 ...
- [AHOI2014&&JSOI2014][bzoj3876] 支线剧情 [上下界费用流]
题面 传送门 思路 转化模型:给一张有向无环图,每次你可以选择一条路径走,花费的时间为路径上边权的总和,问要使所有边都被走至少一遍(可以重复),至少需要花费多久 走至少一遍,等价于覆盖这条边 也就是说 ...