HDU 5636 Shortest Path 分治+搜索剪枝
题意:bc round 74
分析(官方题解):
你可以选择分类讨论, 但是估计可能会写漏一些地方.
只要抽出新增边的端点作为关键点, 建立一个新图, 然后跑一遍floyd就好了. 复杂度大概O(6^2m)
注:然后我不会这种,这种floyd我觉得复杂度应该是复杂度应该是O(8^3m)
大概在千万级别,其实应该可以过,然后,其实只需要求单元最短路就行,然后是不是可以dij,然后就快一点
反正我也没写
我在比赛的时候写的是分治,考虑走不走新加的边每次走几条,以及走的顺序就好
然后全排列,时间复杂度是O(3^4m)的,大概在800w,然后由于这是单组样例800w*T
然后就超时了,然后比完赛,我就加了个剪枝,当前长度大于答案的时候,就返回
然后就1516ms过了,所以,以后记住要剪枝,不要图省事
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long LL;
const int N=1e9+;
int o[],x[],y[],n,m,s,t,ans,T;
void dfs(int pos,int now,int sum)
{
if(sum>=ans)return;
ans=min(ans,abs(t-now)+sum);
int i=o[pos];
if(pos==)
{
ans=min(ans,abs(t-now)+sum);
return;
}
int tmp=sum+abs(now-x[i])+;
dfs(pos+,y[i],tmp);
tmp=sum+abs(now-y[i])+;
dfs(pos+,x[i],tmp);
dfs(pos+,now,sum);
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=; i<; ++i)
{
scanf("%d%d",&x[i],&y[i]);
if(x[i]>y[i])swap(x[i],y[i]);
}
LL res=;
for(int i=; i<=m; ++i)
{
scanf("%d%d",&s,&t);
ans=0x3f3f3f3f;
o[]=,o[]=,o[]=;
dfs(,s,);
o[]=,o[]=,o[]=;
dfs(,s,);
o[]=,o[]=,o[]=;
dfs(,s,);
o[]=,o[]=,o[]=;
dfs(,s,);
o[]=,o[]=,o[]=;
dfs(,s,);
o[]=,o[]=,o[]=;
dfs(,s,);
ans=min(ans,abs(s-t));
LL p=ans,q=i;
res=(res+q*p%N)%N;
}
printf("%I64d\n",res);
}
return ;
}
HDU 5636 Shortest Path 分治+搜索剪枝的更多相关文章
- HDU 5636 Shortest Path 暴力
Shortest Path 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5636 Description There is a path graph ...
- HDU 5636 Shortest Path(Floyed,枚举)
Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Tot ...
- HDU 5636 Shortest Path
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5636 题解: 1.暴力枚举: #include<cmath> #include<c ...
- HDU 5636 Shortest Path(Floyd)
题目链接 HDU5636 n个点,其中编号相邻的两个点之间都有一条长度为1的边,然后除此之外还有3条长度为1的边. m个询问,每次询问求两个点之前的最短路. 我们把这三条边的6个点两两算最短路, 然 ...
- hdu 3631 Shortest Path(Floyd)
题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...
- hdu 5113(2014北京—搜索+剪枝)
题意:有N*M的棋盘,用K种颜色去染,要求相邻块不能同色.已知每种颜色要染的块数,问能不能染,如果能,输出任一种染法. 最开始dfs失败了- -,优先搜索一行,搜完后进入下一列,超时.本来以为搜索不行 ...
- HDU - 3631 Shortest Path(Floyd最短路)
Shortest Path Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u SubmitStat ...
- HDU - 4725_The Shortest Path in Nya Graph
The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- 【HDU 6171】Admiral(搜索+剪枝)
多校10 1001 HDU 6171 Admiral 题意 目标状态是第i行有i+1个i数字(i=0-5)共6行.给你初始状态,数字0可以交换上一行最近的两个和下一行最近的两个.求20步以内到目标状态 ...
随机推荐
- Android Studio 单刷《第一行代码》系列目录
前言(Prologue) 本系列将使用 Android Studio 将<第一行代码>(书中讲解案例使用Eclipse)刷一遍,旨在为想入坑 Android 开发,并选择 Android ...
- shuffle过程中的信息传递
依据Spark1.4版 Spark中的shuffle大概是这么个过程:map端把map输出写成本地文件,reduce端去读取这些文件,然后执行reduce操作. 那么,问题来了: reducer是怎么 ...
- 浏览器对象模型(BOM)
BOM结构 用户浏览网页的时候,浏览器会自动创建一些对象,这些对象存放着浏览器窗口的属性和相关信息,也就是大家熟称的BOM.浏览器对象模型是一个层次化的对象集,我们可以通过window对象访问所有对象 ...
- 到底该不该熟悉掌握struts2的ONGL呢?
其实,在学习网站开发过程中,其实不掌握ONGL也是可以的.我们完全可以使用JSTL和EL来代替OGNL. 只要存在要往页面传输内容,我们直接把对象放在request范围即可,这样我们就可以在jsp中使 ...
- FileZilla Server 防火墙端口开启设置 windows 2008 win
入站规则 添加21端口, 程序FileZilla server.exe 出站规则 %SystemRoot%\System32\ftp.exe
- javaweb学习总结(四十四)——监听器(Listener)学习
一.监听器介绍 1.1.监听器的概念
- Qt: 把内容写进字符串中与C++很相似(使用QTextStream包装QString)
#include <iostream>#include <QChar>#include <QFile>#include <QTextStream>#in ...
- QT小技巧(书上没有的)
1. Layout本身不能控制隐藏和显示,但是可以在外面专门套一个Widget,然后控制这个Widget就可以达到相应的效果了. 2. 空目录居然也存在 if (QDir(""). ...
- 理解TCP/IP协议
TCP/IP协议是Transmission Control Protocol/Internet Protocol的简写,中译名为传输控制协议/因特网互联协议. 单从TCP/IP协议这个名称看,好多人误 ...
- open/write/read
C语言中open函数 作用:打开和创建文件. 简述: 1 2 3 4 #include <fcntl.h> int open(const char *pathname, int flags ...