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

题意:给你一条n个点组成的链,相邻两点的距离为1,再给你三条边,这三条边的端点都是链上的点,且每一条的距离为1。有m个询问,问你对于每两个点,从一个端点到另一个端点的最近距离是多少。

思路:可以先初始化3条边中6个点两两之间的最短距离,这个可以用floyd做,那么对于每一个询问,两个点x1,x2的最短距离为不经过任何点,或者经过3条边中的某些边,又因为我们已经初始化出3条边中任意两个点的最短距离,所以我们只要枚举a,b,即x1到a,a到b,再b到x2的最近距离。这一题floyd初始化时关键,如果每次直接8个点floyd时间复杂度就爆了。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 1000000007
#define pi acos(-1.0)
#define MOD 1000000007
int dist[10][10];
void floyd()
{
int i,j,k;
for(k=1;k<=6;k++){
for(i=1;i<=6;i++){
for(j=1;j<=6;j++){
if(dist[i][j]>dist[i][k]+dist[k][j]){
dist[i][j]=dist[i][k]+dist[k][j];
} } } }
} int main()
{
int n,m,i,j,T,k;
int x[10];
int a1,b1,a2,b2,a3,b3;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
scanf("%d%d%d%d%d%d",&x[1],&x[2],&x[3],&x[4],&x[5],&x[6]);
for(i=1;i<=6;i++){
for(j=1;j<=6;j++){
dist[i][j]=abs(x[i]-x[j]);
}
}
dist[1][2]=dist[2][1]=min(dist[1][2],1);
dist[3][4]=dist[4][3]=min(dist[3][4],1);
dist[5][6]=dist[6][5]=min(dist[5][6],1);
floyd(); ll sum=0;
for(k=1;k<=m;k++){
scanf("%d%d",&x[7],&x[8]);
int ans=abs(x[7]-x[8]);
for(i=1;i<=6;i++){
for(j=1;j<=6;j++){
ans=min(ans,abs(x[7]-x[i] )+abs(x[8]-x[j])+dist[i][j] );
ans=min(ans,abs(x[7]-x[j] )+abs(x[8]-x[i])+dist[i][j] ); }
} sum=(sum+(ll)ans*(ll)k)%MOD;
//printf("%d\n",floyd()); }
printf("%lld\n",sum); }
return 0; }

hdu5365Shortest Path (floyd)的更多相关文章

  1. HDU3631:Shortest Path(Floyd)

    Problem Description When YY was a boy and LMY was a girl, they trained for NOI (National Olympiad in ...

  2. HDU - 3631 Shortest Path(Floyd最短路)

    Shortest Path Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u SubmitStat ...

  3. [ZOJ2760]How Many Shortest Path(floyd+最大流)

    题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 题意:给你一个一个n*n(n<=100)的有向图,问你从s到 ...

  4. sdut1282Find the Path (floyd变形)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1282 感觉这题就比较有意思了 ,虽说是看了别人 ...

  5. 【ACM程序设计】求短路 Floyd算法

    最短路 floyd算法 floyd是一个基于贪心思维和动态规划思维的计算所有点到所有点的最短距离的算法. P57-图-8.Floyd算法_哔哩哔哩_bilibili 对于每个顶点v,和任一顶点对(i, ...

  6. 【转】最短路&差分约束题集

    转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...

  7. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  8. [matlab] 22.matlab图论实例 最短路问题与最小生成树 (转载)

    最短路问题之 Floyd 某公司在六个城市 c1c1,c2c2,….,c6c6 中有分公司,从 cici 到 cjcj 的直接航程票价记在下述矩阵的 (ii,jj) 位置上. (∞∞表示无直接航路), ...

  9. 【HDOJ图论题集】【转】

    =============================以下是最小生成树+并查集====================================== [HDU] How Many Table ...

随机推荐

  1. 【剑指 Offer】03.数组中重复的数字

    题目描述 找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次.请找出数组中 ...

  2. 【Flutter】布局类组件之对齐和相对定位

    前言 如果只想简单的调整一个子元素在父元素中的位置的话,使用Align组件会更简单一些. 接口描述 const Align({ Key key, // 需要一个AlignmentGeometry类型的 ...

  3. CODING x 腾讯兔小巢,打破研发团队与用户反馈的最后一道壁垒

    任何产品的更新迭代都离不开用户的使用反馈.产品经理日常需要奔走到一线部门了解用户的使用反馈:一线运营或业务团队日常需要向产品经理转述用户的问题场景及催促需求的进度.中间需要消耗大量的精力来进行信息转达 ...

  4. 微信小程序API交互的自定义封装

    目录 1,起因 2,优化成果 3,实现思路 4,完整代码 1,起因 哪天,正在蚂蚁森林疯狂偷能量的我被boss叫过去,告知我司要做一个线上直播公开课功能的微信小程序,博主第一次写小程序,复习了下文档, ...

  5. 【Nginx】使用keepalive和nginx搭载高可用

    首先介绍一下Keepalived,它是一个高性能的服务器高可用或热备解决方案,Keepalived主要来防止服务器单点故障的发生问题,可以通过其与Nginx的配合实现web服务端的高可用. Keepa ...

  6. 【Linux】ABRT has detected 1 problem(s). For more info run: abrt-cli list --since 1548988705

    ------------------------------------------------------------------------------------------------- | ...

  7. kubernetes之为每个命名空间的pod设置默认的requests以及limits

    一  为啥需要为命名空间里面添加pod添加默认的requests和limits? 通过前面的学习我们已经知道,如果节点上面的pod没有设置requests和limits,这些容器就会受那些设置了的控制 ...

  8. 登陆到 SAP 系统后的用户出口

    增强类型:smod 增强名称:SUSR0001 组件(退出功能模块):EXIT_SAPLSUSF_001 功能:用户每次登陆SAP系统后都会调用这个SUSR0001增强,可以在FUNCTION EXI ...

  9. uni-app开发经验分享十五: uni-app 蓝牙打印功能

    最近在做uni-app项目时,遇到了需要蓝牙打印文件的功能需要制作,在网上找到了一个教程,这里分享给大家. 引入tsc.js 简单得引入到自己所需要得页面中去,本次我们只要到了标签模式,他同时还有账单 ...

  10. css animation @keyframes 动画

    需求:语音播放动态效果 方案:使用如下图片,利用 css animation @keyframes  做动画 html <span class="horn" :class=& ...