hdu 5636 Shortest Path

来源:互联网 发布:mysql修改连接密码 编辑:程序博客网 时间:2024/05/21 04:21
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<cmath>/*                                   Shortest PathTime Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1439    Accepted Submission(s): 456Problem DescriptionThere 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. InputThere 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. OutputFor each test cases, output an integer S=(∑i=1mi?zi) mod (109+7), where zi is the answer for i-th query. Sample Input110 22 4 5 7 8 101 53 1*/ //  注意这是在一条直线上的 using namespace std;int a[6],dis[6][6];int n,m;int M=1e9+7;/*void floyd(){int i,j,k;    return ;}*/int main(){int i,j,k;int x,y;int t;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);for(i=0;i<6;i++)   //这里的存储非常巧妙 {scanf("%d",&a[i]);}//floyd();for(i=0;i<6;i++)  // 打个比方 a[0]=2; a[5]=10;  那么dis[0][5]=8  存储的距离就是点2和点10的距离 {for(j=0;j<6;j++){dis[i][j]=abs(a[i]-a[j]);}}for(i=0;i<6;i+=2)  //打个比方 a[0]=2; a[1]=4;  那么点2和点4之间的距离就应该更新为1 {dis[i][i+1]=dis[i+1][i]=1;}for(i=0;i<6;i++)  //对几个点之间的距离进行更新 {for(j=0;j<6;j++){for(k=0;k<6;k++){dis[j][k]=min(dis[j][k],dis[j][i]+dis[i][k]);}}}int sum=0;for(k=1;k<=m;k++){scanf("%d%d",&x,&y);int ans=abs(x-y);for(i=0;i<6;i++){for(j=0;j<6;j++){ans=min(ans,abs(x-a[i])+dis[i][j]+abs(y-a[j]));}}sum=(sum+(long long)k*ans%M)%M;}printf("%d\n",sum%M); //注意别漏掉了这个sum%M  ,我错了好多次 }return 0;}

0 0
原创粉丝点击