Shortest Path

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 627    Accepted Submission(s): 204

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
 
Source

如果想做出这道题, 重要的是思路和知识的熟练掌握, 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)的更多相关文章

  1. HDU 5636 关键点的 floyd 最短路问题

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

  2. JAVA之单源最短路径(Single Source Shortest Path,SSSP问题)dijkstra算法求解

    题目简介:给定一个带权有向图,再给定图中一个顶点(源点),求该点到其他所有点的最短距离,称为单源最短路径问题. 如下图,求点1到其他各点的最短距离 准备工作:以下为该题所需要用到的数据 int N; ...

  3. 单源最短距离 Single Source Shortest Path

    单源最短距离_示例程序_图模型_用户指南_MaxCompute-阿里云 https://help.aliyun.com/document_detail/27907.html 单源最短距离 更新时间:2 ...

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

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

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

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

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

  9. hdu 3631 Shortest Path(Floyd)

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

随机推荐

  1. oracle 分页 where 三层

    查询[start,start+limit],包含start,包含start+limit,如start=21,limit=10结果就是21到30,包含21和30SELECT * FROM (SELECT ...

  2. SQL查询有两门以上不及格的学生及查询出全部及格的学生

    1.表结构: /*学生*/ create table student( sno int not null primary key, sname ) ); /*课程*/ create table cen ...

  3. MySql数据库 sql查询增加序号的伪列

    在查询数据库的时候,我们有时候需要对查询出来的数据加上序列,1,2,3,……n 例如:我们根据表的某个字段排序后,要对这些数据加上序列,这个时候序号常常不是我们建表时设置好的自增的主键id,怎么办呢? ...

  4. c10k C10M

    高性能网络编程(二):上一个10年,著名的C10K并发连接问题     阅读(22369) | 评论(9)收藏10 淘帖1 赞4   JackJiang Lv.9    1 年前 | |只看大图 1. ...

  5. 循环&信息添加&颜色修改

            #import "AViewController.h" @interface AViewController () <UIActionSheetDelegat ...

  6. Luogu 2279 [HNOI2003]消防局的设立 - 贪心

    Description 给定一棵树形图, 建若干个消防站, 消防站能够覆盖到距离不超过2的点, 求最少需要建几个消防站才能覆盖所有点 Solution 从深度最深的点开始, 在它的爷爷节点上建, 每建 ...

  7. tp5查看版本

    5.0 base.php 5.1 echo \think\facade\App::version();//用这行代码查看版本

  8. [Robot Framework] Robot Framework用Execute Javascript对XPath表示的元素执行Click操作

    Execute Javascript document.evaluate("//a[contains(@href,'createBook')]", document, null, ...

  9. MVVM模式理解

    MVVM 是Model-View-ViewModel 的缩写,它是一种基于前端开发的架构模式,其核心是提供对View 和 ViewModel 的双向数据绑定,这使得ViewModel 的状态改变可以自 ...

  10. java mail 读取邮件列表,

    // 准备连接服务器的会话信息 Properties props = new Properties(); props.setProperty("mail.store.protocol&quo ...