题意: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 分治+搜索剪枝的更多相关文章

  1. HDU 5636 Shortest Path 暴力

    Shortest Path 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5636 Description There is a path graph ...

  2. HDU 5636 Shortest Path(Floyed,枚举)

    Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Tot ...

  3. HDU 5636 Shortest Path

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5636 题解: 1.暴力枚举: #include<cmath> #include<c ...

  4. HDU 5636 Shortest Path(Floyd)

    题目链接  HDU5636 n个点,其中编号相邻的两个点之间都有一条长度为1的边,然后除此之外还有3条长度为1的边. m个询问,每次询问求两个点之前的最短路. 我们把这三条边的6个点两两算最短路, 然 ...

  5. hdu 3631 Shortest Path(Floyd)

    题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...

  6. hdu 5113(2014北京—搜索+剪枝)

    题意:有N*M的棋盘,用K种颜色去染,要求相邻块不能同色.已知每种颜色要染的块数,问能不能染,如果能,输出任一种染法. 最开始dfs失败了- -,优先搜索一行,搜完后进入下一列,超时.本来以为搜索不行 ...

  7. HDU - 3631 Shortest Path(Floyd最短路)

    Shortest Path Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u SubmitStat ...

  8. 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 ...

  9. 【HDU 6171】Admiral(搜索+剪枝)

    多校10 1001 HDU 6171 Admiral 题意 目标状态是第i行有i+1个i数字(i=0-5)共6行.给你初始状态,数字0可以交换上一行最近的两个和下一行最近的两个.求20步以内到目标状态 ...

随机推荐

  1. Sharepoint delegate control

    <?xml version="1.0" encoding="utf-8"?> <Elements xmlns="http://sch ...

  2. poj City Horizon (线段树+二分离散)

    http://poj.org/problem?id=3277 City Horizon Time Limit: 2000MS   Memory Limit: 65536K Total Submissi ...

  3. js 判断文件是否存在(转载)

     js 判断文件是否存在(转载) var fso,s=filespec; // filespec="C:/path/myfile.txt"fso=new ActiveXObject ...

  4. http://blog.csdn.net/itplus/article/details/10088625

    http://blog.csdn.net/itplus/article/details/10088625 DBSCAN

  5. [转载]HTML5 Audio/Video 标签,属性,方法,事件汇总

    <audio> 标签属性: src:音乐的URL preload:预加载 autoplay:自动播放 loop:循环播放 controls:浏览器自带的控制条 <audio id=& ...

  6. c++中的隐藏、重载、覆盖(重写)

    转自c++中的隐藏.重载.覆盖(重写) 1 重载与覆盖 成员函数被重载的特征: (1)相同的范围(在同一个类中): (2)函数名字相同: (3)参数不同: (4)virtual关键字可有可无. 覆盖是 ...

  7. java 布尔值一种赋值方法

    在研读jmeter的代码时发现一个很常见的布尔值赋值方式,记录下来备忘: public static void main(String[] args) { // TODO Auto-generated ...

  8. EntityFreamWork和Mvc 精品知识点

    定义了DbRepository<TEntity>:IRepository<TEntity> ,SimpleDbContext继承了DbContext, UnitOfWork:I ...

  9. linux下添加PATH环境变量

    添加PATH环境变量,第1种方法:[root@lx_web_s1 ~]# export PATH=/usr/local/webserver/mysql/bin:$PATH 再次查看: [root@lx ...

  10. Qt之高级网络操作(HTTP/FTP快速上手)Qt之QHttpPart和QHttpMultiPart

    http://blog.csdn.net/u011012932/article/details/52535755 http://blog.csdn.net/u011012932/article/det ...