74(2B)Shortest Path (hdu 5636) (Floyd)
Shortest Path
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 627 Accepted Submission(s): 204
You are given the graph and several queries about the shortest path between some pairs of vertices.
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.
如果想做出这道题, 重要的是思路和知识的熟练掌握, Floyd模板并不难, 但怎么将它巧妙的用到了题中是值得思考的问题,还是自己掌握的不熟练, 一看别人的就懂, 但让自己写却毫无头绪
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <cmath>
#include <iostream> using namespace std; #define MOD (1000000000+7) int main()
{
int T;
scanf("%d", &T); while(T--)
{
int n, m, i, j, k, l, r, u, v, a[10];
int dp[10][10];
long long res=0, len; scanf("%d%d", &n, &m); for(i=1; i<=6; i++)
scanf("%d", &a[i]); for(i=1; i<=6; i++) ///相当于对dp初始化
for(j=1; j<=6; j++)
dp[i][j] = abs(a[i]-a[j]); if(a[1]!=a[2]) dp[1][2] = dp[2][1] = 1; ///如果两点不相等的话就让两点的距离为1
if(a[3]!=a[4]) dp[3][4] = dp[4][3] = 1;
if(a[5]!=a[6]) dp[5][6] = dp[6][5] = 1; for(k=1; k<=6; k++)
for(i=1; i<=6; i++)
for(j=1; j<=6; j++)
dp[i][j] = min(dp[i][j], dp[i][k]+dp[k][j]); for(i=1; i<=m; i++)
{
scanf("%d%d", &l, &r);
len = abs(l-r); for(u=1; u<=6; u++)
for(v=1; v<=6; v++)
len = min(len, (long long)(abs(a[u]-l)+dp[u][v]+abs(a[v]-r))); res = (res+len*i)%MOD;
} printf("%I64d\n", res); }
return 0;
}
74(2B)Shortest Path (hdu 5636) (Floyd)的更多相关文章
- HDU 5636 关键点的 floyd 最短路问题
Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- JAVA之单源最短路径(Single Source Shortest Path,SSSP问题)dijkstra算法求解
题目简介:给定一个带权有向图,再给定图中一个顶点(源点),求该点到其他所有点的最短距离,称为单源最短路径问题. 如下图,求点1到其他各点的最短距离 准备工作:以下为该题所需要用到的数据 int N; ...
- 单源最短距离 Single Source Shortest Path
单源最短距离_示例程序_图模型_用户指南_MaxCompute-阿里云 https://help.aliyun.com/document_detail/27907.html 单源最短距离 更新时间:2 ...
- HDU 4725 The Shortest Path in Nya Graph (最短路 )
This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just ...
- 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 ...
- 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 ...
- HDU 4725 The Shortest Path in Nya Graph(最短路径)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
- 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 ...
- hdu 3631 Shortest Path(Floyd)
题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...
随机推荐
- AsyncTask的缺陷
导语:在开发Android应用的过程中,我们需要时刻注意保障应用的稳定性和界面响应性,因为不稳定或者响应速度慢的应用将会给用户带来非常差的交互体验.在越来越讲究用户体验的大环境下,用户也许会因为应用的 ...
- Liunx cp
功能: 复制文件或目录 使用权限:所有使用者说明: cp指令用于复制文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中.若同 ...
- postfix发信提示 Error: too many connectino from
查看提示,很明显是提示连接数过多导致的. 有提示上面的信息,看提示的IP地址是一个网关的地址,使用netstat -ano|grep ':25'|wc -l 看了下,25端口的连接的IP地址,几乎全是 ...
- POJ 3169.Layout 最短路
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11612 Accepted: 5550 Descripti ...
- There are stopped jobs
问题背景 系统:ubuntu,当输入exit退出shell时,出现There are stopped jobs 无法退出shell 解决办法 找到这个stopped job然后终止它 jobs 或者 ...
- 常用的 composer 命令
一.列表内容 composer composer list二.查看当前镜像源 composer config -l -g [repositories.packagist.org.type] compo ...
- boost的accumulator rolling_mean的使用
Boost.Accumulators is both a library for incremental statistical computation as well as an extensibl ...
- 【算法专题】工欲善其事必先利其器—— 常用函数和STL
一. 常用函数 #include <stdio.h> int getchar( void ); //读取一个字符, 一般用来去掉无用字符 char *ge ...
- spring学习 十七 scope属性,单例多例
Scope属性是<bean>中的属性,取值可以有, singleton 默认值, 单例, prototype 多例, 每次获取重新实例化, request 每次请求重新实例化, sessi ...
- SpringMVC作用域传值几种方式
一.SpringMVC 作用域传值的几种方式 1 使用原生Servlet 1.1 在 HandlerMethod 参数中添加作用域对象 1.1.1 ServletContext不能在方法参数中获取, ...