hdu5365Shortest Path (floyd)
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.
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.
where zi is
the answer for i-th
query.
10 2
2 4 5 7 8 10
1 5
3 1
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)的更多相关文章
- HDU3631:Shortest Path(Floyd)
Problem Description When YY was a boy and LMY was a girl, they trained for NOI (National Olympiad in ...
- HDU - 3631 Shortest Path(Floyd最短路)
Shortest Path Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u SubmitStat ...
- [ZOJ2760]How Many Shortest Path(floyd+最大流)
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 题意:给你一个一个n*n(n<=100)的有向图,问你从s到 ...
- sdut1282Find the Path (floyd变形)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1282 感觉这题就比较有意思了 ,虽说是看了别人 ...
- 【ACM程序设计】求短路 Floyd算法
最短路 floyd算法 floyd是一个基于贪心思维和动态规划思维的计算所有点到所有点的最短距离的算法. P57-图-8.Floyd算法_哔哩哔哩_bilibili 对于每个顶点v,和任一顶点对(i, ...
- 【转】最短路&差分约束题集
转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- [matlab] 22.matlab图论实例 最短路问题与最小生成树 (转载)
最短路问题之 Floyd 某公司在六个城市 c1c1,c2c2,….,c6c6 中有分公司,从 cici 到 cjcj 的直接航程票价记在下述矩阵的 (ii,jj) 位置上. (∞∞表示无直接航路), ...
- 【HDOJ图论题集】【转】
=============================以下是最小生成树+并查集====================================== [HDU] How Many Table ...
随机推荐
- 【SpringBoot1.x】 Docker
SpringBoot1.x Docker 核心概念 Docker 是一个开源的应用容器引擎,是一个轻量级容器技术.Docker 支持将软件编译成一个镜像,然后在镜像中各种软件做好配置,将镜像发布出去, ...
- Linux学习笔记 | 配置nginx
目录 一.Nginx概述 二.why Nginx? 三.Linux安装Nginx APT源安装 官网源码安装 四.nginx相关文件的配置 html文件:/var/www/html/index.htm ...
- Pandas数据分析练手题(十题)
数据集下载地址:https://github.com/Rango-2017/Pandas_exercises --------------------------------------------- ...
- a[i][j] 和 a[j][i] 有什么区别?
本文以一个简单的程序开头--数组赋值: int LEN = 10000;int[][] arr = new int[LEN][LEN]; for (int i = 0; i < LEN; i++ ...
- Python利用最优化算法求解投资内部收益率IRR【一】
一. 内部收益率和净现值 内部收益率(Internal Rate of Return, IRR)其实要和净现值(Net Present Value, NPV)结合起来讲.净现值指的是某个投资项目给公司 ...
- 前端知识(二)05-Eslint语法规范检查-谷粒学院
目录 一.ESLint简介 二.启用ESLint 1.ESLint插件安装 2.插件的扩展设置 3.确认开启语法检查 三.ESLint规则说明 1.规则说明 2.语法规则 一.ESLint简介 ESL ...
- JS实现计算器,带三角函数,根号
极简主义网页计算器. 实现了按键特效,可响应键盘按键,实时显示计算结果. 可切换模式,拓展高级功能,包括根号.三角函数.括号等. 效果如下: 代码如下: html: <!DOCTYPE html ...
- ovs-fields
1. 字段匹配 精确匹配 field=value,如,nw_src=10.1.2.3. 按位匹配 field=value/mask,如,nw_src=10.1.0.0/255.255.0.0,nw_s ...
- JavaScript中的深拷贝和浅拷贝!【有错误】还未修改!请逛其他园子!
JavaScript中的深拷贝和浅拷贝! 浅拷贝 1.浅拷贝只是拷贝一层,更深层次对象级别的只拷贝引用.{也就是拷贝的是地址!简而言之就是在新的对象中修改深层次的值也会影响原来的对象!} // 2.深 ...
- CISCO 如何重置3850交换机密码
SUMMARY STEPS: Connect a terminal or PC to the switch. Set the line speed on the emulation software ...