HDU 5636 Shortest Path-(DFS 搜索)

版权声明:本文为博主原创文章,转载请标明原博客。 https://blog.csdn.net/sdau_fangshifeng/article/details/86499718

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5636

Shortest Path

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

题目大意:

  1. 给你一条链,每两个节点之间的距离是1,然后加上3条边。
  2. m个询问,[l,r]之间的最短路
  3. 加上的边的距离是1

思路:

  1. 通过搜索,可以找出走3条捷径的所有情况,然后取出最小值即可

This is the code

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x7f7f7f7f      //2139062143
#define LL_INF 0x7f7f7f7f7f7f7f7f //9187201950435737471
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
const int maxn=100005;
const int mod = 1e9+7;
LL tx[5],ty[5];
int vis[5];
LL s,t;
LL ans;

void DFS(LL now,LL len)
{
    if(len+abs(t-now)<ans)
        ans=(len+abs(t-now))%mod;
    for(int i =1; i <= 3; i++)
    {
        if(!vis[i])
        {
            vis[i] = 1;
            DFS(tx[i],abs(len+abs(ty[i]-now))+1);
            DFS(ty[i],abs(len+abs(tx[i]-now))+1);
            vis[i] = 0;
        }
    }
}

int main()
{
    int T,n,q;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%lld",&n,&q);
        for(int i=1; i<=3; i++)
            scanf("%lld%lld",&tx[i],&ty[i]);
        LL sum=0;
        for(LL i=1 ; i<=q ; i++)
        {
            scanf("%lld%lld",&s,&t);
            ans=abs(s-t);
            DFS(s,0);
            sum=(sum+(ans*i)%mod)%mod;
        }
        printf("%lld\n",sum);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sdau_fangshifeng/article/details/86499718
今日推荐