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. leetcode-位1的个数(位与运算)

    位1的个数 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量). 示例 : 输入: 11 输出: 3 解释: 整数 11 的二进制表示为 00000 ...

  2. lintcode112 删除排序链表中的重复元素

    删除排序链表中的重复元素   给定一个排序链表,删除所有重复的元素每个元素只留下一个. 您在真实的面试中是否遇到过这个题? Yes 样例 给出 1->1->2->null,返回 1- ...

  3. 【shell 练习5】编写简单的多级菜单

    一.简单的多级菜单 [root@web129 ~]# cat menu.sh #!/bin/bash #shell菜单演示 function menu() { echo -e `date` cat & ...

  4. 开源自动驾驶仿真平台 AirSim (3) - 运行 AirSim

    AirSim 的官方 Github: https://github.com/Microsoft/AirSim 之前配置了很多,终于要让 AirSim 自己跑起来了. 我们需要把 AirSim 这个插件 ...

  5. c++容器 STL

    2019-01-24 22:30:32 记录学习PAT的一些知识,有待更新 注:本文是对Algorithm 算法笔记 的总结 C++标准库模板(Standard Template Library,ST ...

  6. Python3 Tkinter-Radionbutton

    1.创建单选按钮 from tkinter import * root=Tk() Radiobutton(root,text='b1').pack() Radiobutton(root,text='b ...

  7. Chameleon-mini简介

    ChameleonMini(变色龙)原德国大学在研究RFID安全时所设计的一块针对多频段多类型RFID模拟的硬件,其设计本身支持ISO14443和ISO15693标准协议,最简单直接的用法就是把获取到 ...

  8. Bcp 使用心得【转】

    在做这方面研究的时候,的确遇到了不少麻烦. 首先在做bcp的时候,要开通大数据量访问权限 一.基于sql语句的导入导出 如果是基于SQL语句的导入导出,需要使用存储过程“master..xp_cmds ...

  9. nginx配置和网站的部署

    环境: CentOS Linux release 7.3.1611 (Core) nginx version: nginx/1.13.4 PHP 5.4.16 (cli) (built: Nov 6 ...

  10. js中的数组对象排序

    一.普通数组排序 js中用方法sort()为数组排序.sort()方法有一个可选参数,是用来确定元素顺序的函数.如果这个参数被省略,那么数组中的元素将按照ASCII字符顺序进行排序.如: var ar ...