Shortest Path
Shortest Path
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1494 Accepted Submission(s): 476
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.
10 2
2 4 5 7 8 10
1 5
3 1
for(int k = 1; k<=6; k++)
for(int i = 1; i<=6; i++)
for(int j = 1; j<=6; j++)
d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
}
#include <iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9+;
int d[][];
int a[];
void floyd(){
for(int k = ; k<=; k++)
for(int i = ; i<=; i++)
for(int j = ; j<=; j++)
d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
}
void solve(){
int t,n,m;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i = ; i<=; i++) scanf("%d",&a[i]);
for(int i = ; i<=; i++){
for(int j = ; j<=; j++){
d[i][j] = abs(a[i] - a[j]);
}
}
for(int i = ; i<=; i+=) d[i][i+] = d[i+][i] = ;
floyd();
long long sum = ; for(int k = ; k<=m; k++){
int x,y;
scanf("%d%d",&x,&y);
int ans = abs(x-y);
for(int i = ; i<=; i++)
for(int j = ; j<=; j++)
ans = min(ans,(abs(x-a[i])+d[i][j]+abs(y-a[j])));
sum = (sum + (long long)ans*k%mod)%mod;
}
printf("%I64d\n",sum);
}
}
int main()
{
solve();
return ;
}
卷珠帘
Shortest Path的更多相关文章
- hdu-----(2807)The Shortest Path(矩阵+Floyd)
The Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- zoj 2760 How Many Shortest Path 最大流
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...
- The Shortest Path in Nya Graph
Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...
- hdu 3631 Shortest Path(Floyd)
题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...
- Shortest Path(思维,dfs)
Shortest Path Accepts: 40 Submissions: 610 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
- (中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
- 【ZOJ2760】How Many Shortest Path
How Many Shortest Path 标签: 网络流 描述 Given a weighted directed graph, we define the shortest path as th ...
- [Swift]LeetCode847. 访问所有节点的最短路径 | Shortest Path Visiting All Nodes
An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. graph.lengt ...
- OSPF(Open Shortest Path First)
1.概述 路由协议OSPF全称为Open Shortest Path First,也就开放的最短路径优先协议,因为OSPF是由IETF开发的,所以所有厂商都可以用. OSPF的流量使用IP协议号. O ...
随机推荐
- 素数槽csuoj
超时代码: #include <iostream> using namespace std;//写一个函数判断是否是素数bool isPrime(int num){int i=2;//co ...
- vertor容器
头文件#include<vector> 1.创建vector对象 1.不指定容器大小 vector <int> v; 2.指定容器大小 vector <double&g ...
- javascript: 字符串拼接有问题
点击某个链接,出现js错误:Uncaught SyntaxError: Unexpected token ILLEGAL 原因: $str .= sprintf('   ...
- OSPF 原理
关于OSPF的数据结构Link-State Protocol Data Structures链路状态路由器与距离矢量路由器,可以知道关于整个网络的更多信息Neighbor table:also kno ...
- php 特定类型测试函数
is_array() 检查变量是否是数组 is_double().is_float().is_real() 检查变量是否是浮点数 is_long().is_int().is_integer()检查变量 ...
- zabbix metrics
http://blog.csdn.net/hengyunabc/article/details/44072285
- Batch Sort
Batch Sort time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- php 执行效率
用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则 不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册中 ...
- php 个推的例子
个推 http://docs.getui.com/server/php/start/ <?php /** * Created by PhpStorm. * User: xiaochao * ...
- List、Set、Map集合存放null解析及HashMap、Hashtable异同点解析
1.List.Set.Map集合存放null解析: @Test public void CollectionTest() { // 测试List List<Object> list = n ...