题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5636

题解:

1、暴力枚举:

#include<cmath>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std; typedef long long LL;
const int maxn = 1e5 + ;
const int mod = 1e9 + ; int n, m;
int a[], b[]; int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%d%d", &n, &m);
for (int i = ; i < ; i++) scanf("%d%d", a + i, b + i);
LL ans = ;
int x, y;
for (int i = ; i <= m; i++) {
scanf("%d%d", &x, &y);
//一条新路线都不走
int tmp = abs(x - y); //只走一条
for (int j = ; j < ; j++) {
tmp = min(tmp, abs(x - a[j]) + abs(b[j] - y) + );
tmp = min(tmp, abs(x - b[j]) + abs(a[j] - y) + );
} //走两条
for (int j = ; j < ; j++) {
for (int k = ; k < ; k++) {
if (j == k) continue;
//x -> j开头 -> j结尾 -> k开头 -> k结尾 -> y
tmp = min(tmp, abs(x - a[j]) + abs(b[j] - a[k]) + abs(b[k] - y) + );
//x -> j开头 -> j结尾 -> k结尾 -> k开头 -> y
tmp = min(tmp, abs(x - a[j]) + abs(b[j] - b[k]) + abs(a[k] - y) + );
//x -> j结尾 -> j开头 -> k开头 -> k结尾 -> y
tmp = min(tmp, abs(x - b[j]) + abs(a[j] - a[k]) + abs(b[k] - y) + );
//x -> j结尾 -> j开头 -> k结尾 -> k开头 -> y
tmp = min(tmp, abs(x - b[j]) + abs(a[j] - b[k]) + abs(a[k] - y) + );
}
} //走三条
for (int j = ; j < ; j++) {
for (int k = ; k < ; k++) {
if (j == k) continue;
for (int w = ; w < ; w++) {
if (w == j || w == k) continue;
tmp = min(tmp, abs(x - a[j]) + abs(b[j] - a[k]) + abs(b[k] - a[w]) + abs(b[w] - y) + );
tmp = min(tmp, abs(x - a[j]) + abs(b[j] - a[k]) + abs(b[k] - b[w]) + abs(a[w] - y) + );
tmp = min(tmp, abs(x - a[j]) + abs(b[j] - b[k]) + abs(a[k] - a[w]) + abs(b[w] - y) + );
tmp = min(tmp, abs(x - a[j]) + abs(b[j] - b[k]) + abs(a[k] - b[w]) + abs(a[w] - y) + ); tmp = min(tmp, abs(x - b[j]) + abs(a[j] - a[k]) + abs(b[k] - a[w]) + abs(b[w] - y) + );
tmp = min(tmp, abs(x - b[j]) + abs(a[j] - a[k]) + abs(b[k] - b[w]) + abs(a[w] - y) + );
tmp = min(tmp, abs(x - b[j]) + abs(a[j] - b[k]) + abs(a[k] - a[w]) + abs(b[w] - y) + );
tmp = min(tmp, abs(x - b[j]) + abs(a[j] - b[k]) + abs(a[k] - b[w]) + abs(a[w] - y) + );
}
}
}
ans += ((LL)i*tmp)%mod;
ans %= mod;
}
printf("%lld\n", ans);
}
return ;
}

2、floyd:

对三条新边对应的关键顶点重新建图,跑一遍floyd最短路,对于查询(xi,yi),我们枚举任意两个关键节点(aj,bj),求min(|xi-aj|+len+|bj-yi|),其中len表示新图里面aj到bj的最短距离。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL; const int maxn = 1e5 + ;
const int mod = 1e9 + ;
const int INF = 0x3f3f3f3f; int n, m;
int a[],mat[][]; void init() {
memset(mat, 0x3f, sizeof(mat));
for (int i = ; i < ; i++) mat[i][i] = ;
} int main() {
int tc;
scanf("%d", &tc);
while (tc--) {
init();
scanf("%d%d", &n, &m);
for (int i = ; i < ; i++) {
scanf("%d%d", a + i, a + i + );
}
//新图
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
mat[i][j] = abs(a[i] - a[j]);
}
}
for (int i = ; i < ; i++) {
mat[i][i + ] = mat[i + ][i] = ;
}
//floyd
for (int k = ; k < ; k++) {
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
mat[i][j] = min(mat[i][j], mat[i][k] + mat[k][j]);
}
}
}
LL ans = ;
for (int i = ; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
LL z = abs(x-y);
//枚举所有情况
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
int tmp = abs(x - a[i]) + mat[i][j] + abs(a[j] - y);
z = min(z, (LL)tmp);
}
}
ans += z*i;
ans %= mod;
}
printf("%lld\n", ans);
}
return ;
}
  

HDU 5636 Shortest Path的更多相关文章

  1. HDU 5636 Shortest Path 暴力

    Shortest Path 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5636 Description There is a path graph ...

  2. HDU 5636 Shortest Path(Floyed,枚举)

    Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Tot ...

  3. HDU 5636 Shortest Path 分治+搜索剪枝

    题意:bc round 74 分析(官方题解): 你可以选择分类讨论, 但是估计可能会写漏一些地方. 只要抽出新增边的端点作为关键点, 建立一个新图, 然后跑一遍floyd就好了. 复杂度大概O(6^ ...

  4. HDU 5636 Shortest Path(Floyd)

    题目链接  HDU5636 n个点,其中编号相邻的两个点之间都有一条长度为1的边,然后除此之外还有3条长度为1的边. m个询问,每次询问求两个点之前的最短路. 我们把这三条边的6个点两两算最短路, 然 ...

  5. hdu 3631 Shortest Path(Floyd)

    题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...

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

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

  7. 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 ...

  8. hdu 3631 Shortest Path

    floyd算法好像很奇妙的样子.可以做到每次加入一个点再以这个点为中间点去更新最短路,效率是n*n. #include<cstdio> #include<cstring> #i ...

  9. HDU 4479 Shortest path 带限制最短路

    题意:给定一个图,求从1到N的递增边权的最短路. 解法:类似于bellman-ford思想,将所有的边先按照权值排一个序,然后依次将边加入进去更新,每条边只更新一次,为了保证得到的路径是边权递增的,每 ...

随机推荐

  1. 【memcached的常用操作】

    memcache是一个KEY-VALUE存储缓存数据库,常用作网站数据请求的存储; 提供多种API: 语法简单类似于redis; #设置一个键值存储 #添加一个键值存储 #获取键值 #删除键值 #清空 ...

  2. 一条常用的 Sql

    select  *   from  table  where  条件1 .... group  by  字段......  Having  条件1.....Limit 0,10; 1. 根据where ...

  3. zookeeper无法启动"Unable to load database on disk

    QuorumPeerMain,ResourceManager都没有起来 resourcemanager.log如下 2018-09-28 23:17:02,787 FATAL org.apache.h ...

  4. MQTT入门2 -- “Error: Invalid password hash for user nick.”和“Connection Refused: not authorised.”

    原文地址:https://www.cnblogs.com/NickQ/p/9277315.html 问题描述: 搭建好mosqitto环境后,利用无密码验证方式,成功通过测试. 但修改配置文件将匿名访 ...

  5. Anaconda快速加载opencv

    刚刚发现了两种Anaconda快速加载opencv的方法,亲测有效: 第一种: 直接在Navigator Environment 中搜opencv 如果搜不到,登陆Anaconda Cloud官网 h ...

  6. 阻止Quartus优化掉信号

    使用SignalTap II Logic Analyzer观察信号,有时要观察的信号会被Quartus优化掉,这种情况下可以给信号指定属性.以下例子均使用Verilog. 1. 如果是组合逻辑信号,可 ...

  7. 自己第一次使用ANTLR遇到的问题

    觉得既然是第一次尝试ANTLR嘛,那就来个简单点的,parse Windows 的 hosts 文件吧,结果...... 先上一段 grammar: grammar hosts; hostfile: ...

  8. package html to native application

    npm install nw -g npm . https://github.com/nwjs/nw.js/ https://github.com/nwjs/nw.js/wiki/How-to-run ...

  9. C语言第三周

    一. 字符串常量 只要有一对双引号括起来的字符序列就是字符串常量.列如"hello"接"123" 注意:"a"是字符串常量'a'是字符常量. ...

  10. 罗佳琪的第三次预备作业——虚拟机的安装及Linux的初步学习

    虚拟机的安装及Linux的初步学习 坎坷的安装过程 首先我按照老师给的基于VirtualBox虚拟机安装Ubuntu图文教程进行了下载,下载很顺利但是安装时出现了问题. 起初我以为是电脑位数问题,但我 ...