HDU Problem 5636 Shortest Path 【Floyd】
来源:互联网 发布:d3.js 颜色渐变 编辑:程序博客网 时间:2024/06/01 08:44
Shortest Path
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 (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.
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 integern 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 nextm lines, each contains two integers si and ti (1≤si,ti≤n) , denoting a query.
The sum of values ofm in all test cases doesn't exceed 106 .
The first line contains two integer
In the next
The sum of values of
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
110 22 4 5 7 8 101 53 1
Sample Output
7
Source
BestCoder Round #74 (div.2)
Recommend
wange2014 | We have carefully selected several similar problems for you: 5867 5866 5865 5864 5863
思路:如果没有传送门的存在,距离是abs(x - y),但是现在多了传送门,总共也就6个,可以预处理每个传送门的距离,两次for循环,试一下每次从传送门a进入,再从任意一个出去的距离。
#include <map>#include <set>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <iostream>#include <stack>#include <cmath>#include <string>#include <vector>#include <cstdlib>//#include <bits/stdc++.h>#define space " "using namespace std;//typedef long long LL;typedef __int64 Int;typedef pair<int,int> 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;}
0 0
- HDU Problem 5636 Shortest Path 【Floyd】
- HDU 5636:Shortest Path floyd
- HDU 5636:Shortest Path【Floyd】
- hdu 5636 Shortest Path【floyd+思维】
- HDU 5636 Shortest Path floyd应用
- hdu 5636 Shortest Path(Floyd最短路)
- HDU 5636 Shortest Path(Floyd)
- 【HDU】3631 Shortest Path 【floyd】
- hdu 3631 Shortest Path (floyd)
- hdu 3631 Shortest Path【Floyd】
- hdu 3631 Shortest Path (floyd)
- HDU 3631 Shortest Path Floyd
- HDU-5636(Shortest Path)(floyd最短路径)
- HDU:5636 Shortest Path(floyd+最短路径)
- hdu 3631 Shortest Path(floyd插点法)
- HDU 3631 Shortest Path(Floyd变形)
- hdu 3631 Shortest Path(Floyd)
- hdu 3631 Shortest Path(Floyd)
- 前端自动化工具grunt介绍
- HDOJ-【Gym Class】
- 【MySQL】数据库操作基础笔记
- 巧妙解决Unable to delete directory: 盘符\项目名称\app\build\output\apk
- C++01背包-装箱问题
- HDU Problem 5636 Shortest Path 【Floyd】
- POJ 3420 Quad Tiling(轮廓线DP+矩阵快速幂)
- 英国安全公司高管承认攻击对手公司
- React Native商城项目实战11 - 个人中心头部内容
- ECSHOP调用商品一级分类
- 暑期十测 round2
- Linux系统中“动态库”和“静态库”那点事儿
- 2017届华为优招软件技术岗面试经历
- poi解析excel导入MySQL数据库