Shortest Path

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 285    Accepted Submission(s): 92

Problem 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
1
10 2
2 4 5 7 8 10
1 5
3 1
 
Sample Output
7
 
Source
 
bc 题对于我们这种渣渣来说  太感人了
看了题解做的 比赛的时候 传统的最短路都会超时不用想  想dfs 但姿势表达太渣  
求最短路  初始时  单链 每点间距离为1 另添加距离为1的3条边 也就是由关键的6个点
关键6个点floyd 处理  具体看代码 理解这个都好久 还是太弱
在询问中  6*6 种与查询区间枚举比较找到 最短路
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#define ll __int64
using namespace std;
ll t;
ll n,q;
ll a[];
ll dis[][];
int main()
{
scanf("%I64d",&t);
for(int i=;i<=t;i++)
{
memset(dis,,sizeof(dis));
scanf("%I64d %I64d",&n,&q);
scanf("%I64d %I64d %I64d %I64d %I64d %I64d",&a[],&a[],&a[],&a[],&a[],&a[]);
ll sum=;
for(int j=;j<=;j++)
for(int k=;k<=;k++)
{
dis[j][k]=abs(a[j]-a[k]);
}
dis[][]=;
dis[][]=;
dis[][]=;
dis[][]=;
dis[][]=;
dis[][]=;
for(int i=;i<=;i++)
for(int k=;k<=;k++)
for(int m=;m<=;m++)
{
if(dis[k][i]+dis[i][m]<dis[k][m])
dis[k][m]=dis[k][i]+dis[i][m];
}
for(int j=;j<=q;j++)
{
ll aa,bb,t;
scanf("%I64d %I64d",&aa,&bb);
if(aa>bb)
{
t=aa;
aa=bb;
bb=t;
}
ll ans=abs(bb-aa);
for(int k=;k<=;k++)
for(int m=;m<=;m++)
{
if(ans>abs(aa-a[k])+abs(bb-a[m])+dis[k][m])
ans=abs(aa-a[k])+abs(bb-a[m])+dis[k][m];
}
sum=(sum+j*ans)%;
}
printf("%I64d\n",sum);
}
return ;
}

HDU 5636 关键点的 floyd 最短路问题的更多相关文章

  1. 74(2B)Shortest Path (hdu 5636) (Floyd)

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

  2. hdu 5636 搜索 BestCoder Round #74 (div.2)

    Shortest Path  Accepts: 40  Submissions: 610  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: ...

  3. HDU 5636 Shortest Path(Floyd)

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

  4. HDU 5636 Shortest Path

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

  5. hdu 1874(Dijkstra + Floyd)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Me ...

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

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

  7. HDU 2066 最短路floyd算法+优化

    http://acm.hdu.edu.cn/showproblem.php?pid=206 题意 从任意一个邻居家出发 到达任意一个终点的 最小距离 解析 求多源最短路 我想到的是Floyd算法 但是 ...

  8. hdu 2066 ( 最短路) Floyd & Dijkstra & Spfa

    http://acm.hdu.edu.cn/showproblem.php?pid=2066 今天复习了一下最短路和最小生成树,发现居然闹了个大笑话-----我居然一直写的是Floyd,但我自己一直以 ...

  9. HDU 1317 XYZZY(floyd+bellman_ford判环)

    http://acm.hdu.edu.cn/showproblem.php?pid=1317 题意: 给出一个有向图,每到达一个点,都会加上或减去一些能量,我们要做的就是判断从1出发是否能到达n.初始 ...

随机推荐

  1. 使用JavaScript判断手机是处于横屏还是竖屏

    移动端的浏览器一般都支持window.orientation这个参数,通过这个参数可以判断出手机是处在横屏还是竖屏状态.从而根据实际需求而执行相应的程序.通过添加监听事件onorientationch ...

  2. JAVA基础学习之路(八)[1]String类的基本特点

    String类的两种定义方式: 直接赋值 通过构造方法赋值 //直接赋值 public class test2 { public static void main(String args[]) { S ...

  3. docker学习2

    今天继续学习docker! 搜索镜像 docker search centos 下载镜像 docker pull name(镜像名字) 查看镜像docker images 字段含义分析: TAG:仓库 ...

  4. ubuntu server guide 学习笔记

    1. 软件包 1.1. dpkg dpkg -l dpkg -l | grep apache2 dpkg -L ufw dpkg -S /etc/host.conf dpkg -i zip_3.0-4 ...

  5. CryptoZombies学习笔记——Lesson1

    CryptoZombies是一个学习以太坊开发的平台,我将在这里记录学习过程中的一些笔记. 课程网址:cryptozombies.io 首先是第一课——Lesson1:Making the Zombi ...

  6. Centos6更新yum repo

    163开源镜像站是国内比较老的一个网站.很多人都在使用. step 1/3 备份原镜像文件: cd /etc/yum.repos.d mv CentOS-Base.repo CentOS-Base.r ...

  7. 阿里云搭建bind服务,外网ip不能用来解析问题解决

    options { listen-on port 53 { any; }; //端口开放any listen-on-v6 port 53 { ::1; }; directory "/var/ ...

  8. 第四次作业之psp

    psp 进度条 博文字数累积折线图 代码行数累积折线图 psp饼状图

  9. Calculator PartⅢ

    GitHub/object-oriented The title of the work 这次敲代码耗时相对较短,但是始终无法完成debug步骤,目前上传的代码可以通过编译,但运行即报停,问题调试为内 ...

  10. Calculator Part Ⅰ

    GitHub/object-oriented The title of the work 关于这次的作业,一开始我是觉得不难的,毕竟学长在已经提供了足够多的提示,实现步骤.需要那些方面的知识等等.但是 ...