zoukankan      html  css  js  c++  java
  • 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


    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:  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]);
    }
     1 #include <iostream>
     2 #include<cstdio>
     3 #include<queue>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<cmath>
     7 using namespace std;
     8 const int maxn = 105;
     9 const int INF = 0x3f3f3f3f;
    10 const int mod = 1e9+7;
    11 int d[7][7];
    12 int a[7];
    13 void floyd(){
    14     for(int k = 1; k<=6; k++)
    15         for(int i = 1; i<=6; i++)
    16             for(int j = 1; j<=6; j++)
    17                 d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
    18 }
    19 void solve(){
    20     int t,n,m;
    21     scanf("%d",&t);
    22     while(t--){
    23         scanf("%d%d",&n,&m);
    24         for(int i = 1; i<=6; i++) scanf("%d",&a[i]);
    25         for(int i = 1; i<=6; i++){
    26             for(int j = 1; j<=6; j++){
    27                 d[i][j] = abs(a[i] - a[j]);
    28             }
    29         }
    30         for(int i = 1; i<=6; i+=2) d[i][i+1] = d[i+1][i] = 1;
    31         floyd();
    32         long long sum = 0;
    33 
    34         for(int k = 1; k<=m; k++){
    35             int x,y;
    36             scanf("%d%d",&x,&y);
    37             int ans = abs(x-y);
    38             for(int i = 1; i<=6; i++)
    39                 for(int j = 1; j<=6; j++)
    40                 ans = min(ans,(abs(x-a[i])+d[i][j]+abs(y-a[j])));
    41             sum = (sum + (long long)ans*k%mod)%mod;
    42         }
    43         printf("%I64d
    ",sum);
    44     }
    45 }
    46 int main()
    47 {
    48     solve();
    49     return 0;
    50 }
    卷珠帘
  • 相关阅读:
    k8s 集群文件共享
    .net core 使用IOptionsXXX读取配置
    docker Dockerfile
    k8s 部署tomcatservice (NodePort通过节点向外提供服务)
    k8s 用Rinetd对外tomcatservice
    k8s 部署应用
    坑爹的matlab除法
    Cygwin/Git与Git Source Control Provider结合时初始目录
    手机电话号码从excel导入的最简单方法
    快捷方式改变电源计划,设置关闭显示器时间
  • 原文地址:https://www.cnblogs.com/littlepear/p/5372625.html
Copyright © 2011-2022 走看看