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. java===java基础学习(16)---final

    final-----概念 1.当不希望父类的某个方法被子类覆盖(override)时,可以用final关键字修饰. 2.当不希望类的某个变量的值被修改时,可以用final修饰.如果要用final,则必 ...

  2. python基础===列表类型的所有方法

    链表类型有很多方法,这里是链表类型的所有方法: append(x) 把一个元素添加到链表的结尾,相当于a[len(a):] = [x] extend(L) 通过添加指定链表的所有元素来扩充链表,相当于 ...

  3. 通过call_usermodehelper()在内核态执行用户程序【转】

    转自:http://edsionte.com/techblog/archives/category/linux%E5%86%85%E6%A0%B8%E7%BC%96%E7%A8%8B 背景 如何在Li ...

  4. 【bzoj3223】文艺平衡树

    #include<bits/stdc++.h> #define N 300005 #define rat 4 using namespace std; struct Node{ int s ...

  5. python内建方法

    abs all any apply basestring bin bool buffer bytearray bytes callable chr classmethod cmp coerce com ...

  6. springboot在不同环境下进行不同的配置

    原文链接:http://www.cnblogs.com/java-zhao/p/5469183.html 不同的环境设置一个配置文件,例如:dev(开发)环境下的配置设置在application-de ...

  7. JQUERY 提取多个元素 a img 的 src href

    <div class="abc"><a href="1.html"><img src="1.jpg"/> ...

  8. javascript方法--apply()

    今天琢磨了一下apply,以前对这个方法觉得比较懵,今天一琢磨确实觉得挺好玩的. 一开始把MDN的apply文档看了一遍,感觉不是很理解,而且有一些东西也是知道但是比较模糊,所以还是一步一步来,不懂查 ...

  9. 21:序列化django对象

    django的序列化框架提供了一个把django对象转换成其他格式的机制,通常这些其他的格式都是基于文本的并且用于通过一个管道发送django对象,但一个序列器是可能处理任何一个格式的(基于文本或者不 ...

  10. POJ - 2478

    Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12802   Accepted: 4998 D ...