HDU 5636 Shortest Path 暴力
Shortest Path
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5636
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
4 61
10 2
2 4 5 7 8 10
1 5
3 1
Sample Output
7
Hint
题意
有一条长度为n的链. 节点i和i+1之间有长度为1的边. 现在又新加了3条边, 每条边长度都是1. 给出m个询问, 每次询问两点之间的最短路.
题解:
暴力枚举每种情况就好了
反正我是真暴力枚举 -.-
你路径要么从起点走到某条边的起点,然后出来,然后再走到某条边的起点,某条边的终点这样。
暴力就好了。
代码
#include<stdio.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
int a[4],b[4];
const int mod = 1e9+7;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
long long Ans = 0;
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=3;i++)
scanf("%d%d",&a[i],&b[i]);
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
int ans = abs(y-x);
for(int j=1;j<=3;j++)
{
ans = min(ans,abs(a[j]-x)+abs(b[j]-y)+1);
ans = min(ans,abs(b[j]-x)+abs(a[j]-y)+1);
}
for(int j=1;j<=3;j++)
{
for(int k=1;k<=3;k++)
{
if(j==k)continue;
ans = min(ans,abs(a[j]-x)+abs(a[k]-b[j])+abs(y-b[k])+2);
ans = min(ans,abs(b[j]-x)+abs(a[k]-a[j])+abs(y-b[k])+2);
ans = min(ans,abs(a[j]-x)+abs(b[k]-b[j])+abs(y-a[k])+2);
ans = min(ans,abs(b[j]-x)+abs(b[k]-a[j])+abs(y-a[k])+2);
}
}
for(int j=1;j<=3;j++)
{
for(int k=1;k<=3;k++)
{
if(j==k)continue;
for(int t=1;t<=3;t++)
{
if(t==j||t==k)continue;
ans = min(ans,abs(a[j]-x)+abs(a[k]-b[j])+abs(a[t]-b[k])+abs(y-b[t])+3);
ans = min(ans,abs(b[j]-x)+abs(a[k]-a[j])+abs(a[t]-b[k])+abs(y-b[t])+3);
ans = min(ans,abs(a[j]-x)+abs(b[k]-b[j])+abs(a[t]-a[k])+abs(y-b[t])+3);
ans = min(ans,abs(b[j]-x)+abs(b[k]-a[j])+abs(a[t]-a[k])+abs(y-b[t])+3);
ans = min(ans,abs(a[j]-x)+abs(a[k]-b[j])+abs(b[t]-b[k])+abs(y-a[t])+3);
ans = min(ans,abs(b[j]-x)+abs(a[k]-a[j])+abs(b[t]-b[k])+abs(y-a[t])+3);
ans = min(ans,abs(a[j]-x)+abs(b[k]-b[j])+abs(b[t]-a[k])+abs(y-a[t])+3);
ans = min(ans,abs(b[j]-x)+abs(b[k]-a[j])+abs(b[t]-a[k])+abs(y-a[t])+3);
}
}
}
Ans = (Ans+1ll*i*ans)%mod;
}
printf("%lld\n",Ans);
}
}
HDU 5636 Shortest Path 暴力的更多相关文章
- HDU 5636 Shortest Path(Floyed,枚举)
Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Tot ...
- HDU 5636 Shortest Path
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5636 题解: 1.暴力枚举: #include<cmath> #include<c ...
- HDU 5636 Shortest Path 分治+搜索剪枝
题意:bc round 74 分析(官方题解): 你可以选择分类讨论, 但是估计可能会写漏一些地方. 只要抽出新增边的端点作为关键点, 建立一个新图, 然后跑一遍floyd就好了. 复杂度大概O(6^ ...
- HDU 5636 Shortest Path(Floyd)
题目链接 HDU5636 n个点,其中编号相邻的两个点之间都有一条长度为1的边,然后除此之外还有3条长度为1的边. m个询问,每次询问求两个点之前的最短路. 我们把这三条边的6个点两两算最短路, 然 ...
- hdu 3631 Shortest Path(Floyd)
题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...
- HDU - 3631 Shortest Path(Floyd最短路)
Shortest Path Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u SubmitStat ...
- HDU - 4725_The Shortest Path in Nya Graph
The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- hdu 3631 Shortest Path
floyd算法好像很奇妙的样子.可以做到每次加入一个点再以这个点为中间点去更新最短路,效率是n*n. #include<cstdio> #include<cstring> #i ...
- HDU 4479 Shortest path 带限制最短路
题意:给定一个图,求从1到N的递增边权的最短路. 解法:类似于bellman-ford思想,将所有的边先按照权值排一个序,然后依次将边加入进去更新,每条边只更新一次,为了保证得到的路径是边权递增的,每 ...
随机推荐
- python中的binascii模块
binascii模块拿来干嘛的? 答:进制转换xxoo #!/usr/bin/env python # encoding:utf-8 # by i3ekr import binascii s = &q ...
- MySQL 8.0 正式版 8.0.11 发布:比 MySQL 5.7 快 2 倍
ySQL 8.0 正式版 8.0.11 已发布,官方表示 MySQL 8 要比 MySQL 5.7 快 2 倍,还带来了大量的改进和更快的性能! 注意:从 MySQL 5.7 升级到 MySQL 8. ...
- linux下pthread_cancel无法取消线程的原因【转】
转自:http://blog.csdn.net/huangshanchun/article/details/47420961 版权声明:欢迎转载,如有不足之处,恳请斧正. 一个线程可以调用pthrea ...
- C++之指针,引用与数组
引用只是对象的另一个名字,通过在变量名前面添加"&”符号来定义,而指针保存的是另一个对象的地址,它们两都提供了间接访问所服务变量的途径. 但是它们的差别还是挺大的: 先从它们的值说起 ...
- 在linux下有没有什么软件可以连接windows上的MSSQL SERVER
在linux下有没有什么软件可以连接windows上的MSSQL SERVER GUI的http://dbeaver.jkiss.org/ http://bbs.csdn.net/topics/391 ...
- Vim常用命令(转)—默写版
1.光标移动 上: 下: 左: 『字母小写』 右: 上一行行首: 『减号』 下一行行首: 行首: 『数字0』 行尾: 单词词尾或后一个单词词尾: 后一个单词词首: 单词词首或前一个单词词首: 跳转到特 ...
- C++ 输入ctrl+z 不能再使用cin的问题
问题介绍: 程序步骤是开始往容器里面写数据,以Ctrl+Z来终止输入流,然后需要输入一个数据,来判断容器中是否有这个数据. 源代码如下: #include<iostream> #inclu ...
- 利用BeanUtils工具类封装表单数据
一.BeanUtils工具类的使用 1.首先导入BeanUtils工具类的jar包 commons-beanutils-1.8.0.jar commons-logging-1.1.1.jar 2.se ...
- HDU-3065
病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- resteasy json
https://www.cnblogs.com/toSeeMyDream/p/5763725.html