Shortest Path(HDU-5636)

探讨了在路径图中,通过添加三条单位长度边来优化最短路径算法的方法。通过对新增边的枚举和Floyd算法的应用,实现了对任意两点间最短路径的有效计算,避免了直接使用大规模数组带来的内存溢出问题。

Problem Description

There is a path graph G=(V,E) with nn 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=(∑mi⋅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

题意:t 组数据,每组数据给出两个数 n、m 代表一个图有 n 个点,且相邻两个点 i 到 i+1 的距离是 1,另外再加三条边,边权同样为 1,m 代表 m 组查询,每组查询给出两个数 x、y ,要求将每组的查询结果 mi 乘以组号 i 后相加模 1E9+7 后输出

思路:实质就是一条线,线上有 n 个点,相邻两点距离为一,然后有 3 组点的距离改为 1,然后查询任意两点的距离。

由于数据量的问题,直接 Floyd,开数组 dis[1000000][1000000] 会爆,故此题需要技巧

通过题意可以看出,在不加新的三条边前,任意两点 x、y 的路径长度为 |x-y|,由于加 3 条边权为 1 的边后要求两点最短路,因此增加的边要具有缩短边权的功能,否则就没有任何作用

因此,可以直接去枚举新增的三条边,通过 Floyd 看能否缩短路径,如果可以的话就更新最小值,然后查询时,先通过给出的两点 x、y 判断原距离 |x-y|,然后与 Floyd 后的最小距离比较,找出最小的,最后直接将每组查询结果与组号相乘再相加取模即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define PI acos(-1.0)
#define E 1e-6
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define N 1001
#define LL long long
using namespace std;
int n,m;
int a[N];
int dis[N][N];
void floyd(){
    for(int k=1;k<=6; k++)
        for(int i=1;i<=6;i++)
            for(int j=0;j<=6;j++)
                dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
int main (){
    int t;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&m);
		for(int i=1;i<=6;i++) //六个点
			scanf("%d",&a[i]);

		for(int i=1;i<=6;i++)
            for(int j=1;j<=6;j++)
                dis[i][j]=abs(a[i]-a[j]);

		for(int i=1;i<=6;i+=2)//相邻距离为1
			dis[i][i+1]=dis[i+1][i]=1;

		floyd();

		LL res=0;
		for(int i=1;i<=m;i++){
            int x,y;
			scanf("%d%d",&x,&y);
			int len=abs(x-y);//原距离
			for(int j=1;j<=6;j++)
                for(int k=1;k<=6;k++)
                    len=min(len,abs(x-a[j])+abs(y-a[k])+dis[j][k]);

			res=(res+(LL)i*len%MOD)%MOD;
		}
		printf("%lld\n",res);
	}

    return 0;
}

 

Practice 1 Date: Monday, March 18th, 2013 We highly encourage being environment friendly and trying all problems on your own. Implement exercise 2.3-7. Implement priority queue. Implement Quicksort and answer the following questions. (1) How many comparisons will Quicksort do on a list of n elements that all have the same value? (2) What are the maximum and minimum number of comparisons will Quicksort do on a list of n elements, give an instance for maximum and minimum case respectively. Give a divide and conquer algorithm for the following problem: you are given two sorted lists of size m and n, and are allowed unit time access to the ith element of each list. Give an O(lg m + lgn) time algorithm for computing the kth largest element in the union of the two lists. (For simplicity, you can assume that the elements of the two lists are distinct). Practice 2 Date: Monday, April 1st, 2013 We highly encourage being environment friendly and trying all problems on your own. Matrix-chain product. The following are some instances. Longest Common Subsequence (LCS). The following are some instances. X: xzyzzyx Y: zxyyzxz X:MAEEEVAKLEKHLMLLRQEYVKLQKKLAETEKRCALLAAQANKESSSESFISRLLAIVAD Y:MAEEEVAKLEKHLMLLRQEYVKLQKKLAETEKRCTLLAAQANKENSNESFISRLLAIVAG Longest Common Substring. The following are some instances. X: xzyzzyx Y: zxyyzxz X:MAEEEVAKLEKHLMLLRQEYVKLQKKLAETEKRCALLAAQANKESSSESFISRLLAIVAD Y:MAEEEVAKLEKHLMLLRQEYVKLQKKLAETEKRCTLLAAQANKENSNESFISRLLAIVAG Max Sum. The following is an instance. (-2,11,-4,13,-5,-2) Shortest path in multistage graphs. Find the shortest path from 0 to 15 for the following graph.   A multistage graph is a graph (1) G=(V,E) with V partitioned into K >= 2 disjoint subsets such that if (a,b) is in E, then a is in Vi , and b is in Vi+1 for some subsets in the partition; and (2) | V1 | = | VK | = 1.     Practice 3 Date: Monday, April 15th, 2013 We highly encourage being environment friendly and trying all problems on your own. Knapsack Problem. There are 5 items that have a value and weight list below, the knapsack can contain at most 100 Lbs. Solve the problem both as fractional knapsack and 0/1 knapsack. A simple scheduling problem. We are given jobs j1, j2… jn, all with known running times t1, t2… tn, respectively. We have a single processor. What is the best way to schedule these jobs in order to minimize the average completion time. Assume that it is a nonpreemptive scheduling: once a job is started, it must run to completion. The following is an instance. (j1, j2, j3, j4) : (15,8,3,10) Single-source shortest paths. The following is the adjacency matrix, vertex A is the source.  A B C D E A -1 3 B 3 2 2 C D 1 5 E -3 All-pairs shortest paths. The adjacency matrix is as same as that of problem 3.(Use Floyd or Johnson’s algorithm)     Practice 4 Date: Monday, May 8th, 2013 We highly encourage being environment friendly and trying all problems on your own. 0/1 Knapsack Problem. There are 5 items that have a value and weight list below, the knapsack can contain at most 100 Lbs. Solve the problem using back-tracking algorithm and try to draw the tree generated. Solve the 8-Queen problem using back-tracking algorithm.    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值