Hdu 5636 Shortest Path【最短路+暴力】

Shortest Path

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


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

题意:

给出1-n,n 个数字,代表n 个顶点,每个顶点到达其他顶点的距离是两个顶点对应的数字的差值,现在新加了三条边,每条边的距离都是1,并且给出新加边连接的顶点

后面有m次查询,每次查询给出两个顶点,求两点间最短距离,计算最短距离和查询的编号(查询编号从1开始)乘积的累加值。


题解:

看题目发现明显是多源最短路,但是直接跑多源的最短的floyd算法,肯定超时!、

题目给出了很好的前提条件:在新加入边之前,所有的顶点之间的最短路程,就是他们的编号的差值(当然是绝对值)!

那么现在单独研究这新加的三条边:

新边加入后,肯定引起最短路变化,但是只是影响了一部分,不必要对那些没影响的顶点进行最短路更新的操作

所以,完全可以只在新加入的几个顶点之间跑一次floyd 算法,求出这几个点之间的最短路的最短的路,然后在计算某两个点的最短路时,跑一次动态的最短路floyd更新,枚举这几个点对最短路径的影响,就可以得到任意两点的最短路了!


另外的技巧就是(个人感觉算是离散化),跑局部最短路的时候,用下标代替顶点进行更新!


个人心得:

第一次见到这样的题目,感觉真的是涨见识了,原本全是自己会的东西,但是根据题目条件限制,竟然可以这样灵活的变动,差点认不出来了...

确实学什么东西都是要深刻理解其中的本质,这是能灵活运用知识的前提,自己还要学的东西还有很多,继续努力吧!!!!


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h> 
using namespace std;
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,m,x[6]={0},dis[6][6]={0};
		scanf("%d%d",&n,&m); 
		for(int i=0;i<6;++i)//三条边,六个点 
		{
			scanf("%d",&x[i]);
		}
		for(int i=0;i<6;++i)//新加的三条边的原始距离 
		{
			for(int j=0;j<i;++j)
			{
				dis[i][j]=dis[j][i]=abs(x[i]-x[j]);
			}
		}
		for(int i=0;i<6;i+=2)//新加的边 
		{
			dis[i][i+1]=dis[i+1][i]=1;
		}
		for(int i=0;i<6;++i)//三个点跑floyd最短路算法 
		{
			for(int j=0;j<6;++j)
			{
				for(int k=0;k<6;++k)
				{
					dis[j][k]=min(dis[j][k],dis[j][i]+dis[i][k]);
				}
			}
		}
		long long ans=0;
		for(int i=1;i<=m;++i)
		{
			int a,b;
			scanf("%d%d",&a,&b);
			int len=abs(a-b),mod=1e9+7;
			for(int j=0;j<6;++j)
			{
				for(int k=0;k<6;++k)
				{
					int tp=abs(a-x[j])+abs(b-x[k])+dis[j][k];//暴力枚举 
					len=min(tp,len);
				} 
			}
			ans=(ans+(long long)i*len%mod)%mod;
		}
		printf("%lld\n",ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值