• hdu5365Shortest Path (floyd)


    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

    题意:给你一条n个点组成的链,相邻两点的距离为1,再给你三条边,这三条边的端点都是链上的点,且每一条的距离为1。有m个询问,问你对于每两个点,从一个端点到另一个端点的最近距离是多少。

    思路:可以先初始化3条边中6个点两两之间的最短距离,这个可以用floyd做,那么对于每一个询问,两个点x1,x2的最短距离为不经过任何点,或者经过3条边中的某些边,又因为我们已经初始化出3条边中任意两个点的最短距离,所以我们只要枚举a,b,即x1到a,a到b,再b到x2的最近距离。这一题floyd初始化时关键,如果每次直接8个点floyd时间复杂度就爆了。

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<bitset>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    typedef long double ldb;
    #define inf 1000000007
    #define pi acos(-1.0)
    #define MOD 1000000007
    int dist[10][10];
    void floyd()
    {
        int i,j,k;
        for(k=1;k<=6;k++){
            for(i=1;i<=6;i++){
                for(j=1;j<=6;j++){
                    if(dist[i][j]>dist[i][k]+dist[k][j]){
                        dist[i][j]=dist[i][k]+dist[k][j];
                    }
    
                }
    
            }
    
        }
    }
    
    
    
    int main()
    {
        int n,m,i,j,T,k;
        int x[10];
        int a1,b1,a2,b2,a3,b3;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d",&n,&m);
            scanf("%d%d%d%d%d%d",&x[1],&x[2],&x[3],&x[4],&x[5],&x[6]);
            for(i=1;i<=6;i++){
                for(j=1;j<=6;j++){
                    dist[i][j]=abs(x[i]-x[j]);
                }
            }
            dist[1][2]=dist[2][1]=min(dist[1][2],1);
            dist[3][4]=dist[4][3]=min(dist[3][4],1);
            dist[5][6]=dist[6][5]=min(dist[5][6],1);
            floyd();
    
    
            ll sum=0;
            for(k=1;k<=m;k++){
                scanf("%d%d",&x[7],&x[8]);
                int ans=abs(x[7]-x[8]);
                for(i=1;i<=6;i++){
                    for(j=1;j<=6;j++){
                        ans=min(ans,abs(x[7]-x[i] )+abs(x[8]-x[j])+dist[i][j] );
                        ans=min(ans,abs(x[7]-x[j] )+abs(x[8]-x[i])+dist[i][j] );
    
                    }
                }
    
    
    
                sum=(sum+(ll)ans*(ll)k)%MOD;
                //printf("%d
    ",floyd());
    
    
            }
            printf("%lld
    ",sum);
    
    
        }
        return 0;
    
    
    }
    


  • 相关阅读:
    IIS支持apk文件下载
    【转】Winform输入法控制
    WebRequest请求Url中文乱码
    c#实现Form窗体始终在桌面最前端显示
    在静态页面中使用 Vue.js
    NPOI 的使用姿势
    C# 模拟 HTTP POST请求
    WinForm 绑定到嵌套对象上的属性
    彻底清除 Windows 服务
    WPF DataGrid 绑定行双击行命令
  • 原文地址:https://www.cnblogs.com/herumw/p/9464522.html
Copyright © 2020-2023  润新知