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. 【isJson( jsonObj )】判断是否是JSON实例

    判断是否是JSON实例: 原型:isJson( jsonObj ) 说明:判断对象是否是JSON实例 返回:[true | false] 示例: <% Set jsonObj1 = toJson ...

  2. typescript 学习记录

    类型判断: typeJudge() { //typeof 用来判断变量类型 var s: string = 'egret'; var isString: boolean = typeof s === ...

  3. Vuejs 基础与语法

    Vue 实例 创建第一个实例 {{}} 被称之为插值表达式.可以用来进行文本插值. <!DOCTYPE html> <html lang="en"> < ...

  4. Memcache的客户端连接系列(三) C++

    关键词: Memcached   C++ 客户端 声明:本文并非原创,转自华为云帮助中心的分布式缓存服务(Memcached)的用户指南.客户端连接方法通用,故摘抄过来分享给大家. C++客户端示例 ...

  5. Centos7下部署activeMQ消息队列服务

    #1.下载activeMQlinux包 http://activemq.apache.org/activemq-5100-release.html   下载linux的activeMQ包 #2.使用X ...

  6. maven 安装、配置

    简介: maven 大大提高项目开发速度     编译---打包---测试--安装   一条龙 maven将项目构建的过程标准化,每一个阶段使用一个命令完成,下面是构建过程一些阶段 清理 mvn cl ...

  7. Balanced Lineup:线段树:区间最值 / RMQ

    不要被线段树这个名字和其长长的代码吓到. D - Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ...

  8. matlab 常用集合相关的函数

    Matlab常用的集合相关的函数如下:     union(A,B)              %求集合A和集合B的并集     intersect(A,B)             %求集合A和集合 ...

  9. 【转载】图解Java常用数据结构(一)

    图解Java常用数据结构(一)  作者:大道方圆 原文:https://www.cnblogs.com/xdecode/p/9321848.html 最近在整理数据结构方面的知识, 系统化看了下Jav ...

  10. 【转】使用CNPM搭建私有NPM

    最近的Node项目中因为数据模型等问题,需要有一个对各个模块进行统一的管理,如果把私有的模型publish到公共的npm不太合适,所以决定使用cnpm搭建一个私有的npm,同时也可以对项目常用的npm ...