http://acm.hdu.edu.cn/showproblem.php?pid=3986

【题意】

  • 给定一个有重边的无向图,T=20,n<=1000,m<=5000
  • 删去一条边,使得1~n的最短路最长
  • 求最短路最长是多少

【思路】

  • 一定是删最短路上的边
  • 可以先跑一个Dijkstra,求出最短路,然n后枚举删边
  • 朴素的Dijkstra为n^2,枚举删边(n条)需要的总时间复杂度是n^3
  • 堆优化Dijkstra(nlogn),总复杂度为n^2logn
  • 有多重边,用邻接矩阵不方便,用邻接表方便,删去的边标记一下就好

【Accepted】

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<vector> using namespace std;
typedef long long ll;
const int maxm=5e4*+;
const int maxn=1e3+;
const int inf=0x3f3f3f3f;
int n,m;
struct node
{
int to;
int nxt;
int w;
}e[maxm];
int head[maxn];
int tot;
bool vis[maxn];
int dis[maxn];
int pv[maxn];
int pe[maxn];
typedef pair<int,int> pii;
void init()
{
memset(head,-,sizeof(head));
tot=;
} void add(int u,int v,int w)
{
e[tot].to=v;
e[tot].w=w;
e[tot].nxt=head[u];
head[u]=tot++;
} int Dij(int x)
{
priority_queue<pii,vector<pii>,greater<pii> >pq;
memset(vis,false,sizeof(vis));
memset(dis,inf,sizeof(dis));
dis[]=;
pq.push(make_pair(dis[],));
// pq.push(pii(dis[1],1));
while(!pq.empty())
{
pii cur=pq.top();
pq.pop();
int u=cur.second;
if(vis[u]) continue;
vis[u]=true;
for(int i=head[u];i!=-;i=e[i].nxt)
{
if(i==x) continue;
int w=e[i].w;
int v=e[i].to;
if(dis[u]+w<dis[v])
{
dis[v]=dis[u]+w;
pq.push(make_pair(dis[v],v));
if(x==-)
{
pv[v]=u;
pe[v]=i;
}
}
}
}
return dis[n];
} int Solve()
{
if(Dij(-)==inf)
{
return -;
}
int ans=;
for(int i=n;i!=;i=pv[i])
{
ans=max(ans,Dij(pe[i]));
if(ans==inf)
{
return -;
}
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
scanf("%d%d",&n,&m);
while(m--)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
int ans=Solve();
printf("%d\n",ans);
}
return ;
}

堆优化的Dijkstra

【Dijstra堆优化】HDU 3986 Harry Potter and the Final Battle的更多相关文章

  1. hdu 3986 Harry Potter and the Final Battle (最短路径)

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

  2. hdu 3986 Harry Potter and the Final Battle

    一个水题WA了60发,数组没开大,这OJ也不提示RE,光提示WA...... 思路:先求出最短路,如果删除的边不是最短路上的,那么对结果没有影响,要有影响,只能删除最短路上的边.所以枚举一下最短路上的 ...

  3. HDU-6290_奢侈的旅行(Dijstra+堆优化)

    奢侈的旅行 Time Limit: 14000/7000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) Problem De ...

  4. hdu 2066 Dijstra 堆优化

    嗯 有广搜的意思 #include<cstdio> #include<iostream> #include<queue> #include<vector> ...

  5. 复习最短路 spfa+dijstra堆优化

    题目很简单,, 但是wa了三次,, 用<vector>之前一定要记得clear()...简单说下 spfa的问题 和bell_forman有点类似 每次取出一个点 然后更新 并把更新了的节 ...

  6. hdu3986Harry Potter and the Final Battle

    给你一个无向图,然后找出当中的最短路, 除去最短路中的随意一条边,看最糟糕的情况下, 新的图中,第一个点到末点的最短路长度是多少. 我的做法是: 首先找出最短路,然后记录路径, 再一条一条边的删, 删 ...

  7. 最短路-朴素版Dijkstra算法&堆优化版的Dijkstra

    朴素版Dijkstra 目标 找到从一个点到其他点的最短距离 思路 ①初始化距离dist数组,将起点dist距离设为0,其他点的距离设为无穷(就是很大的值) ②for循环遍历n次,每层循环里找出不在S ...

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

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

  9. 堆优化dijstra

    因为spfa没事就被卡一卡,所以堆优化dijstra就显得很重要,在最短路或者其模型里边,最少有一条边是没有被更新过的,也就是它是最短的,同理从这个点开始也有一条边最短,所以每次就找最短的然后松弛操作 ...

随机推荐

  1. 动手实现 Redux(三):纯函数(Pure Function)简介

    我们接下来会继续优化我们的 createStore 的模式,让它使我们的应用程序获得更好的性能. 但在开始之前,我们先用一节的课程来介绍一下一个函数式编程里面非常重要的概念 —— 纯函数(Pure F ...

  2. mvc不登录的情况下无法跳转至其他页面--解决方法之一

    在每个控制器里,加以下方法 /// <summary> /// 在调用视图之前拦截非法用户 /// </summary> /// <param name="fi ...

  3. Java-每日编程练习题③

    一.计算圆周率 中国古代数学家研究出了计算圆周率最简单的办法: PI=4/1-4/3+4/5-4/7+4/9-4/11+4/13-4/15+4/17...... 这个算式的结果会无限接近于圆周率的值, ...

  4. hihocoder1079 离散化

    思路:线段树 + 离散化. 测试用例: 3 10 1 10 1 3 6 10 实现: #include <bits/stdc++.h> using namespace std; typed ...

  5. 51nod 1432 独木舟

    基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 n个人,已知每个人体重.独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人.显然要求总重量不超过独木舟承 ...

  6. HttpClient 接口调用

    String url = "http://127.0.0.1:8080/api"; //然后根据表名获取公司信息 HttpPost httppost = new HttpPost( ...

  7. Matlab 图像转极坐标系

    实心圆环 imgVP1=flip(imgVP1,1);  % 水平翻转 polarVP1=polarVolinPlot(imgVP1); % 调用函数空心圆环 [m,n,~]=size(imgVP2) ...

  8. 7-Java-C(冰雹数)

    题目描述: 任意给定一个正整数N, 如果是偶数,执行: N / 2 如果是奇数,执行: N * 3 + 1 生成的新的数字再执行同样的动作,循环往复. 通过观察发现,这个数字会一会儿上升到很高, 一会 ...

  9. instance of type of object.prototype.tostring 区别

    typeof typeof 是一个操作符,其右侧跟一个一元表达式,并返回这个表达式的数据类型.   返回的结果用该类型的字符串(全小写字母)形式表示,包括以下 6 种:   number.boolea ...

  10. C-基础:数组名与取地址符&

    指出下面代码的输出,并解释为什么.(不错,对地址掌握的深入挖潜) main() { ]={,,,,}; ); printf(),*(ptr-)); } 输出:2,5     *(a+1)就是a[1], ...