博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU Problem 5636 Shortest Path 【Floyd】
阅读量:7071 次
发布时间:2019-06-28

本文共 2866 字,大约阅读时间需要 9 分钟。

 

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 1587    Accepted Submission(s): 513

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 
(1i<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 
(1n,m105) -- the number of vertices and the number of queries. The next line contains 6 integers 
a1,b1,a2,b2,a3,b3 
(1a1,a2,a3,b1,b2,b3n), 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 
(1si,tin), 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=1mizi) 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
 

 

Recommend
wange2014   |   We have carefully selected several similar problems for you:            
 

题意:有一个节点数为n的链表,每个节点相距一个单位的长度,但是有三个传送门,问你每次给出两点的最短距离是多少

 

思路:如果没有传送门的存在,距离是abs(x - y),但是现在多了传送门,总共也就6个,可以预处理每个传送门的距离,两次for循环,试一下每次从传送门a进入,再从任意一个出去的距离。

#include #include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
#define space " "using namespace std;//typedef long long LL;typedef __int64 Int;typedef pair
paii;const int INF = 0x3f3f3f3f;const double ESP = 1e-5;const double Pi = acos(-1);const int MOD = 1e9 + 7;const int MAXN = 1e5 + 10;Int d[7][7], a[7];Int abs_long(Int x) { if (x < 0) return -x; return x;}int main() { int t, m, n; scanf("%d", &t); while (t--) { scanf("%d%d", &n, &m); for (int i = 0; i < 3; i++) { scanf("%I64d%I64d", &a[i], &a[i + 3]); } //初始化传送门之间的距离 for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { d[i][j] = abs_long(a[i] - a[j]); } } for (int i = 0; i < 3; i++) { d[i][i + 3] = d[i + 3][i] = 1; } for (int k = 0; k < 6; k++) { for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } Int x, y; Int ans = 0, temp, sum; for (int k = 1; k <= m; k++) { scanf("%I64d%I64d", &x, &y); temp = abs_long(x - y); //用每一个传送门检查最短距离 for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { sum = abs_long(x - a[i]) + d[i][j] + abs_long(y - a[j]); temp = min(sum, temp); } } ans = (ans + temp*k)%MOD; } printf("%I64d\n", ans); } return 0;}

 

 

 

转载于:https://www.cnblogs.com/cniwoq/p/6770803.html

你可能感兴趣的文章
字符集之间转换读取和写入
查看>>
Linux下防御DDOS攻击的操作梳理
查看>>
完整的区块链会员生态解决方案
查看>>
FTP服务之匿名用户访问
查看>>
spring-mvc.xml与applicationContext.xml的区别
查看>>
iOS获取手机与屏幕属性
查看>>
这家AI芯片独角兽吊打英伟达,吹捧还是硬实力?
查看>>
寒冬下2年android的搞笑求职历程
查看>>
19.Shell编程进阶,数组,字符串,(for,select,while read line)
查看>>
怎样快速将文字转换成语音?这种操作很简单
查看>>
Oracle 树操作 (select…start with…connect by…prior)
查看>>
重新打开MyEclipse 后,发现SVN 不能用了,而且是引用直接失效问题
查看>>
ios 禁止横屏
查看>>
【非凡程序员】  OC第十一节课 (代码块)
查看>>
Java正则表达式详解(三)
查看>>
无线网络布署方式
查看>>
loadrunner 乱码问题设置总结
查看>>
php安装redis 和redis扩展
查看>>
Javascript将html转成pdf,下载(html2canvas 和 jsPDF)
查看>>
org.apache.jasper.jasperException
查看>>