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. Python2.7.3 Tkinter Entry(文本框) 说明

      Python学习记录--关于Tkinter Entry(文本框)的选项.方法说明,以及一些示例. 属性(Options) background(bg) borderwidth(bd) cursor ...

  3. write-ups

    https://github.com/MarioVilas/write-ups https://github.com/Deplorable-Mountaineer/Robot_Dynamite htt ...

  4. 1438. Shopaholic

    Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Lindsay is a shopaholic. Whenever th ...

  5. agc016D - XOR Replace(图论 智商)

    题意 题目链接 给出两个长度为\(n\)的数组\(a, b\) 每次可以将\(a\)中的某个数替换为所有数\(xor\)之和. 若\(a\)数组可以转换为\(b\)数组,输出最少操作次数 否则输出\( ...

  6. Django web框架之模板继承

    模板继承 (extend) Django模版引擎中最强大也是最复杂的部分就是模版继承了.模版继承可以让您创建一个基本的“骨架”模版,它包含您站点中的全部元素,并且可以定义能够被子模版覆盖的 block ...

  7. OpenCL学习笔记(二):并行编程概念理解

    欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术.应用感兴趣的同学加入. 并行编程的需求是显而易见的,其 ...

  8. AC日记——妖梦斩木棒 洛谷 P3797

    妖梦斩木棒 思路: 略坑爹: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 200005 #define m ...

  9. vmware + ubuntu 64 安装 node.js v8.9.3

    第一次使用虚拟机,第一次使用linux系统,第一次安装使用node.js 虚拟机安装不用多说,安装好之后下载ubuntu 64位版本文件 在vm中点击“创建新的虚拟机”,选择下载的ubuntu iso ...

  10. node修改全局环境路径 与 全局后出现sh:exe command not found

    修改全局环境路径 当安装nodeJs时候需要修改全局环境的指向,先看看npm config get prefix  全局环境在哪里 然后执行更换命令,一个是主文件一个是缓存文件 npm config ...