Shortest Path

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

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
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5659 5658 5657 5655 5654 
 最近在学习最短路问题,floyd写法。
void floyd(){
    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的更多相关文章

  1. hdu-----(2807)The Shortest Path(矩阵+Floyd)

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

  2. zoj 2760 How Many Shortest Path 最大流

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...

  3. The Shortest Path in Nya Graph

    Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...

  4. hdu 3631 Shortest Path(Floyd)

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

  5. Shortest Path(思维,dfs)

    Shortest Path  Accepts: 40  Submissions: 610  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: ...

  6. (中等) 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 ...

  7. 【ZOJ2760】How Many Shortest Path

    How Many Shortest Path 标签: 网络流 描述 Given a weighted directed graph, we define the shortest path as th ...

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

  9. OSPF(Open Shortest Path First)

    1.概述 路由协议OSPF全称为Open Shortest Path First,也就开放的最短路径优先协议,因为OSPF是由IETF开发的,所以所有厂商都可以用. OSPF的流量使用IP协议号. O ...

随机推荐

  1. 素数槽csuoj

    超时代码: #include <iostream> using namespace std;//写一个函数判断是否是素数bool isPrime(int num){int i=2;//co ...

  2. vertor容器

    头文件#include<vector> 1.创建vector对象 1.不指定容器大小  vector <int> v; 2.指定容器大小 vector <double&g ...

  3. javascript: 字符串拼接有问题

    点击某个链接,出现js错误:Uncaught SyntaxError: Unexpected token ILLEGAL 原因: $str .= sprintf('&nbsp&nbsp ...

  4. OSPF 原理

    关于OSPF的数据结构Link-State Protocol Data Structures链路状态路由器与距离矢量路由器,可以知道关于整个网络的更多信息Neighbor table:also kno ...

  5. php 特定类型测试函数

    is_array() 检查变量是否是数组 is_double().is_float().is_real() 检查变量是否是浮点数 is_long().is_int().is_integer()检查变量 ...

  6. zabbix metrics

    http://blog.csdn.net/hengyunabc/article/details/44072285

  7. Batch Sort

    Batch Sort time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  8. php 执行效率

    用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则 不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册中 ...

  9. php 个推的例子

    个推   http://docs.getui.com/server/php/start/ <?php /** * Created by PhpStorm. * User: xiaochao * ...

  10. List、Set、Map集合存放null解析及HashMap、Hashtable异同点解析

    1.List.Set.Map集合存放null解析: @Test public void CollectionTest() { // 测试List List<Object> list = n ...