【Dijstra堆优化】HDU 3986 Harry Potter and the Final Battle
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的更多相关文章
- 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 ...
- hdu 3986 Harry Potter and the Final Battle
一个水题WA了60发,数组没开大,这OJ也不提示RE,光提示WA...... 思路:先求出最短路,如果删除的边不是最短路上的,那么对结果没有影响,要有影响,只能删除最短路上的边.所以枚举一下最短路上的 ...
- HDU-6290_奢侈的旅行(Dijstra+堆优化)
奢侈的旅行 Time Limit: 14000/7000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) Problem De ...
- hdu 2066 Dijstra 堆优化
嗯 有广搜的意思 #include<cstdio> #include<iostream> #include<queue> #include<vector> ...
- 复习最短路 spfa+dijstra堆优化
题目很简单,, 但是wa了三次,, 用<vector>之前一定要记得clear()...简单说下 spfa的问题 和bell_forman有点类似 每次取出一个点 然后更新 并把更新了的节 ...
- hdu3986Harry Potter and the Final Battle
给你一个无向图,然后找出当中的最短路, 除去最短路中的随意一条边,看最糟糕的情况下, 新的图中,第一个点到末点的最短路长度是多少. 我的做法是: 首先找出最短路,然后记录路径, 再一条一条边的删, 删 ...
- 最短路-朴素版Dijkstra算法&堆优化版的Dijkstra
朴素版Dijkstra 目标 找到从一个点到其他点的最短距离 思路 ①初始化距离dist数组,将起点dist距离设为0,其他点的距离设为无穷(就是很大的值) ②for循环遍历n次,每层循环里找出不在S ...
- hdu 3986(最短路变形好题)
Harry Potter and the Final Battle Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/6553 ...
- 堆优化dijstra
因为spfa没事就被卡一卡,所以堆优化dijstra就显得很重要,在最短路或者其模型里边,最少有一条边是没有被更新过的,也就是它是最短的,同理从这个点开始也有一条边最短,所以每次就找最短的然后松弛操作 ...
随机推荐
- P1664 每日打卡心情好
题目背景 在洛谷中,打卡不只是一个简单的鼠标点击动作,通过每天在洛谷打卡,可以清晰地记录下自己在洛谷学习的足迹.通过每天打卡,来不断地暗示自己:我又在洛谷学习了一天,进而帮助自己培养恒心.耐心.细心. ...
- 【转】JobScheduler
JobScheduler JobScheduler是Android L(API21)新增的特性,用于定义满足某些条件下执行的任务.它的宗旨是把一些不是特别紧急的任务放到更合适的时机批量处理,这样可以有 ...
- SIRI课程表
wen 周一@0@{今天没课哦}周一 周二今天共一节课 第2节,可编程控制器应用,11号教学楼1 0 3房间 周二 周三今天共三节课 第二节,过程控制系统,2号楼2 1 0房间,第三节机械制造技术11 ...
- anzhuaggeoip
1.因启动geoip模块,需要先安装GeoIP # wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz # tar xv ...
- 在windows上安装Jenkins---tomcat流
在windows上安装Jenkins有两种方式: (1)jar流 在命令行中运行:java -jar jenkins.war 浏览器访问 localhost:8080,创建初始管理员帐号即可. (2) ...
- log4j 日志分级处理
log4j 配置文件: log4j.rootLogger=debug,stdout,debug,info,errorlog4j.appender.stdout=org.apache.log4j.Con ...
- Java实现Web页面前数字字母验证码实现
最近公司做项目开发中用到了验证码实现功能,将实现代码分享出来, 前段页面实现代码: 为了表达清晰,样式部分代码去掉了,大家根据自己的需求,自己添加样式. 页面JS代码:触发变动验证码改变的JS 后台 ...
- zabbix设置发送消息的时间
需求:比如我有两个报警的媒介:邮件和微信,但是下班之后,晚上我不希望手机一直响,打扰我睡觉,邮件无所谓,可以24h发送 分析:那现在就需要把微信分时间段发送:比如06:00-24:00 这个时间点 ...
- vueCode 常用代码总结 20190116
<template>props 传参<in-body :mbx="['首页','','']"> props 代码使用<BreadcrumbItem&g ...
- function语句注意事项
function语句 在Javascript中定义一个函数,有两种写法: function foo() { } 和 var foo = function () { } 两种写法完全等价.但是在解析的时 ...