Shortest Path

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5636

Description

There is a path graph G=(V,E) with n vertices. Vertices are numbered from 1 to n and there is an edge with unit length between i and i+1 (1≤i<n). To make the graph more interesting, someone adds three more edges to the graph. The length of each new edge is 1.

You are given the graph and several queries about the shortest path between some pairs of vertices.

Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains two integer n and m (1≤n,m≤105) -- the number of vertices and the number of queries. The next line contains 6 integers a1,b1,a2,b2,a3,b3 (1≤a1,a2,a3,b1,b2,b3≤n), separated by a space, denoting the new added three edges are (a1,b1), (a2,b2), (a3,b3).

In the next m lines, each contains two integers si and ti (1≤si,ti≤n), denoting a query.

The sum of values of m in all test cases doesn't exceed 106.

Output

For each test cases, output an integer S=(∑i=1mi⋅zi) mod (109+7), where zi is the answer for i-th query.

Sample Input

4 61

10 2

2 4 5 7 8 10

1 5

3 1

Sample Output

7

Hint

题意

有一条长度为n的链. 节点i和i+1之间有长度为1的边. 现在又新加了3条边, 每条边长度都是1. 给出m个询问, 每次询问两点之间的最短路.

题解:

暴力枚举每种情况就好了

反正我是真暴力枚举 -.-

你路径要么从起点走到某条边的起点,然后出来,然后再走到某条边的起点,某条边的终点这样。

暴力就好了。

代码

#include<stdio.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std; int a[4],b[4];
const int mod = 1e9+7;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
long long Ans = 0;
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=3;i++)
scanf("%d%d",&a[i],&b[i]);
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
int ans = abs(y-x);
for(int j=1;j<=3;j++)
{
ans = min(ans,abs(a[j]-x)+abs(b[j]-y)+1);
ans = min(ans,abs(b[j]-x)+abs(a[j]-y)+1);
}
for(int j=1;j<=3;j++)
{
for(int k=1;k<=3;k++)
{
if(j==k)continue;
ans = min(ans,abs(a[j]-x)+abs(a[k]-b[j])+abs(y-b[k])+2);
ans = min(ans,abs(b[j]-x)+abs(a[k]-a[j])+abs(y-b[k])+2);
ans = min(ans,abs(a[j]-x)+abs(b[k]-b[j])+abs(y-a[k])+2);
ans = min(ans,abs(b[j]-x)+abs(b[k]-a[j])+abs(y-a[k])+2);
}
}
for(int j=1;j<=3;j++)
{
for(int k=1;k<=3;k++)
{
if(j==k)continue;
for(int t=1;t<=3;t++)
{
if(t==j||t==k)continue;
ans = min(ans,abs(a[j]-x)+abs(a[k]-b[j])+abs(a[t]-b[k])+abs(y-b[t])+3);
ans = min(ans,abs(b[j]-x)+abs(a[k]-a[j])+abs(a[t]-b[k])+abs(y-b[t])+3);
ans = min(ans,abs(a[j]-x)+abs(b[k]-b[j])+abs(a[t]-a[k])+abs(y-b[t])+3);
ans = min(ans,abs(b[j]-x)+abs(b[k]-a[j])+abs(a[t]-a[k])+abs(y-b[t])+3); ans = min(ans,abs(a[j]-x)+abs(a[k]-b[j])+abs(b[t]-b[k])+abs(y-a[t])+3);
ans = min(ans,abs(b[j]-x)+abs(a[k]-a[j])+abs(b[t]-b[k])+abs(y-a[t])+3);
ans = min(ans,abs(a[j]-x)+abs(b[k]-b[j])+abs(b[t]-a[k])+abs(y-a[t])+3);
ans = min(ans,abs(b[j]-x)+abs(b[k]-a[j])+abs(b[t]-a[k])+abs(y-a[t])+3);
}
}
}
Ans = (Ans+1ll*i*ans)%mod;
}
printf("%lld\n",Ans);
}
}

HDU 5636 Shortest Path 暴力的更多相关文章

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

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

  2. HDU 5636 Shortest Path

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

  3. HDU 5636 Shortest Path 分治+搜索剪枝

    题意:bc round 74 分析(官方题解): 你可以选择分类讨论, 但是估计可能会写漏一些地方. 只要抽出新增边的端点作为关键点, 建立一个新图, 然后跑一遍floyd就好了. 复杂度大概O(6^ ...

  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 - 3631 Shortest Path(Floyd最短路)

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

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

  8. hdu 3631 Shortest Path

    floyd算法好像很奇妙的样子.可以做到每次加入一个点再以这个点为中间点去更新最短路,效率是n*n. #include<cstdio> #include<cstring> #i ...

  9. HDU 4479 Shortest path 带限制最短路

    题意:给定一个图,求从1到N的递增边权的最短路. 解法:类似于bellman-ford思想,将所有的边先按照权值排一个序,然后依次将边加入进去更新,每条边只更新一次,为了保证得到的路径是边权递增的,每 ...

随机推荐

  1. Ubuntu安装pip

    首先打开终端 在终端输入:sudo apt-get install python-pip python-dev build-essential [+] 如果需要在Python3下安装pip,那么在py ...

  2. lsb_release查看当前系统的发行版信息

    Linux除了用uname -r查看系统版本信息外,还可以用lsb_release. 安装: yum install -y redhat-lsb-core 使用: lsb_release -a

  3. ltsdangerous加密解密

    前言 在做QQ第三方登录时,用户跳转到QQ登录界面登录成功后,会在URL返回一个code参数.前端把code发送给后端.后端收到后,会查询出openid.然后判断openid是否存在,如果存在就可以绑 ...

  4. jQuery实现,动态自动定位弹窗。JS分页,Ajax请求

    工作中碰到一个问题,一个页面中碰到多个地方需要弹窗数据. 网上找了一圈,没有找到合适的,所以自己写了一个. 兼容IE7+,chrome.其它未测试. 需求:点击任意的输入框(也可其它元素,代码中有注释 ...

  5. display:flex代替float

    昨天做一个css的东西,在开始用js的时候才发现被float占位了 因为float浮动起来了,我清除了浮动,但是还是占位 然后我同事就告诉我其实可以不用float来左右浮动 在父元素上用display ...

  6. AC日记——[POI2014]KUR-Couriers 洛谷 P3567

    [POI2014]KUR-Couriers 思路: 卡空间,sb题: 代码: #include <bits/stdc++.h> using namespace std; #define m ...

  7. jstree无限级菜单ajax按需动态加载子节点

    业余时间研究了一下jstree,更新非常快已经是3.0了,首先看一下效果截图: 1.页面引入样式和脚本(注意路径根据实际情况) <link href="~/Scripts/vakata ...

  8. win10的VMware虚机host-only模式下,虚拟机无法ping通物理机,而物理机能ping通虚机

    1.打开控制面板—->Windows防火墙(win10操作系统) 2.点击最上面的”允许应用或功能通过xxxxx” 3.勾上上图的“文件和打印机共享” 然后点确定.

  9. 创建 OpenStack云主机 (十五)

    创建过程 创建虚拟网络 创建m1.nano规格的主机(相等于定义虚拟机的硬件配置) 生成一个密钥对(openstack的原理是不使用密码连接,而是使用密钥对进行连接) 增加安全组规则(用iptable ...

  10. javascript获取dom的下一个节点方法

    需求说明: 获取当前节点左节点或者右节点(兄弟节点): css: <style type="text/css"> a:focus { outline: none; } ...