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步以内到目标状态 ...
随机推荐
- 监控SQL Server的job执行情况
在服务器没有设置发邮件并且不允许发邮件的情况下, 可以通过下列语句来检查SQL Server 的job的执行情况 select top 150 a.run_date,a.run_time, b.nam ...
- spring的配置模式与注解模式基础
“依赖注入”是spring的核心特征,在Web服务器(如Tomcat)加载时,它会根据Spring的配置文件中配置的bean或者是通过注解模式而扫描并装载的bean实例自动注入到Application ...
- Searching in a rotated and sorted array
Given a sorted array that has been rotated serveral times. Write code to find an element in this arr ...
- Otto Product Classification Winner's Interview: 2nd place, Alexander Guschin ¯\_(ツ)_/¯
Otto Product Classification Winner's Interview: 2nd place, Alexander Guschin ¯\_(ツ)_/¯ The Otto Grou ...
- PAT-乙级-1039. 到底买不买(20)
1039. 到底买不买(20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 小红想买些珠子做一串自己喜欢的珠串 ...
- glibc库详解及与POSIX,system V这些库之间关系的说明
自己想了解下关于system v,在网上看到一篇详细的说明,与大家分享一下,原文地址http://hi.baidu.com/tekuba/item/570887775696542e5c178918 以 ...
- IText 中文字体解决方案 生成doc文档
IText生成doc文档需要三个包:iTextAsian.jar,iText-rtf-2.1.4.jar,iText-2.1.4.jar 亲测无误,代码如下: import com.lowagie.t ...
- ibatis的there is no statement named xxx in this SqlMap
报错情况如下: com.ibatis.sqlmap.client.SqlMapException: There is no statement named Control.insert-control ...
- 第一个C语言代码
#include<stdio.h> void main() { int g1,g2,g3,r1,r2,r3,n; int m=0; float ave; i ...
- SGU 101 修改
感谢这里. test4确实是个不连通的case,奇怪的是我用check函数跟if (check() == false)来判断这个case,当不连通时就死循环,得到的结果是不一样的,前者得到WA,后者得 ...