zoukankan      html  css  js  c++  java
  • HDU 5636 关键点的 floyd 最短路问题

    Shortest Path

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


    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
     
    bc 题对于我们这种渣渣来说  太感人了
    看了题解做的 比赛的时候 传统的最短路都会超时不用想  想dfs 但姿势表达太渣  
    求最短路  初始时  单链 每点间距离为1 另添加距离为1的3条边 也就是由关键的6个点
    关键6个点floyd 处理  具体看代码 理解这个都好久 还是太弱
    在询问中  6*6 种与查询区间枚举比较找到 最短路
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<vector>
     5 #include<cmath>
     6 #define ll __int64
     7 using namespace std;
     8 ll t;
     9 ll n,q;
    10 ll a[10];
    11 ll dis[10][10];
    12 int main()
    13 {
    14     scanf("%I64d",&t);
    15     for(int i=1;i<=t;i++)
    16     {
    17         memset(dis,0,sizeof(dis));
    18         scanf("%I64d %I64d",&n,&q);
    19         scanf("%I64d %I64d %I64d %I64d %I64d %I64d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6]);
    20         ll sum=0;
    21         for(int j=1;j<=6;j++)
    22                 for(int k=1;k<=6;k++)
    23             {
    24                 dis[j][k]=abs(a[j]-a[k]);
    25             }
    26         dis[1][2]=1;
    27         dis[2][1]=1;
    28         dis[3][4]=1;
    29         dis[4][3]=1;
    30         dis[5][6]=1;
    31         dis[6][5]=1;
    32         for(int i=1;i<=6;i++)
    33          for(int k=1;k<=6;k++)
    34              for(int m=1;m<=6;m++)
    35         {
    36             if(dis[k][i]+dis[i][m]<dis[k][m])
    37                 dis[k][m]=dis[k][i]+dis[i][m];
    38         }
    39         for(int j=1;j<=q;j++)
    40         {
    41             ll aa,bb,t;
    42             scanf("%I64d %I64d",&aa,&bb);
    43             if(aa>bb)
    44             {
    45                 t=aa;
    46                 aa=bb;
    47                 bb=t;
    48             }
    49             ll ans=abs(bb-aa);
    50             for(int k=1;k<=6;k++)
    51                 for(int m=1;m<=6;m++)
    52             {
    53                 if(ans>abs(aa-a[k])+abs(bb-a[m])+dis[k][m])
    54                     ans=abs(aa-a[k])+abs(bb-a[m])+dis[k][m];
    55             }
    56             sum=(sum+j*ans)%1000000007;
    57         }
    58      printf("%I64d
    ",sum);
    59     }
    60     return 0;
    61 }
    View Code
  • 相关阅读:
    Codeforces Global Round 11 E Xum
    【NFLSPC #2】Polynomial
    【SHOI2015】脑洞治疗仪 题解 (线段树)
    CDQ分治与整体二分 学习笔记
    二维树状数组 学习笔记
    博弈论 学习笔记
    【JSOI2007】文本生成器 题解(AC自动机+动态规划)
    【NOI2018】归程 题解(kruskal重构树+最短路)
    【NOI2017】游戏 题解(2-SAT+缩点)
    【BZOJ4398】福慧双修 题解(建图优化)
  • 原文地址:https://www.cnblogs.com/hsd-/p/5246277.html
Copyright © 2011-2022 走看看