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步以内到目标状态 ...
随机推荐
- 【BZOJ 1079】[SCOI2008]着色方案
Description 有n个木块排成一行,从左到右依次编号为1~n.你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块.所有油漆刚好足够涂满所有木块,即c1+c2+...+ck=n.相邻两个木 ...
- div蒙版+可移动
<html> <head> <title></title> <script src="jquery-1.8.2.js&q ...
- [SQL SERVER系列]读书笔记之SQL注入漏洞和SQL调优
最近读了程序员的SQL金典这本书,觉得里面的SQL注入漏洞和SQL调优总结得不错,下面简单讨论下SQL注入漏洞和SQL调优. 1. SQL注入漏洞 由于“'1'='1'”这个表达式永远返回 true, ...
- SC命令执行出现Access is denied
在命令行中先是打开远程链接:net use \\computername(or ip)\ipc$ "password" /user:"[domain\]username& ...
- python学习笔记21(正则表达式)
正则表达式模式: 模式 描述 ^ 匹配的开始的 $ 匹配行尾 . 匹配除换行符的任何单个字符.使用-m选项允许其匹配换行符也是如此. [...] 匹配括号内任何单个字符 [^...] 匹配非单个字符集 ...
- vs中使用过的扩展和好的nuget库
扩展 ReAttach ReAttach gives you an easy way to ReAttaching your prior debug targets. ReAttach stores ...
- Hardwood Species
http://poj.org/problem?id=2418 #include<cstdio> #include<cstring> #include<string> ...
- HDU4704+费马小定理
费马小定理题意:求s1+s2+s3+...+sn;si表示n划分i个数的n的划分的个数,如n=4,则s1=1,s2=3 利用隔板定理可知,就是求(2^n-1)%mod-----Y 现在已知 ...
- 玩转redis
http://www.cnblogs.com/huangxincheng/p/5002794.html
- Java Web开发 之小张老师总结EL、JSP、Servlet变量
EL 11 JSP 9 Servlet JSP类别 pageContext pageContext * 作用域 pageScope pageContext.getAttribute() * reque ...