Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1870 Accepted Submission(s): 615 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 .
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:
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
Source
Recommend wange2014
|
题目大意:一开始给你一条长度为N的链,这个链上的相互的长度都是1,后来又给了你修建了3条边
思路:可以用Floyd的思想求出这些点中的最短路,那么查询时我们只要判断我们要走的路是抄近路快还是原本那样走快就行了,后面的查询时是6*6的,详情见代码吧,代码比较好理解
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int mmod=1e9+7;
int dis[6][6];
int n,m;
int a[6];
long long ans;
int main()
{
int t;
cin>>t;
while(t--)
{
scanf("%d%d",&n,&m);
scanf("%d%d%d%d%d%d",&a[0],&a[1],&a[2],&a[3],&a[4],&a[5]);
for(int i=0; i<6; i++)
for(int j=0; j<6; j++)
dis[i][j]=abs(a[i]-a[j]);
dis[0][1]=1,dis[1][0]=1;
dis[2][3]=1,dis[3][2]=1;
dis[4][5]=1,dis[5][4]=1;
for (int k=0; k<6; k++)
for (int i=0; i<6; i++)
for (int j=0; j<6; j++)
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
ans=0;
for(int k=1; k<=m; k++)
{
int x,y;
scanf("%d%d",&x,&y);
int mm=abs(x-y);
for(int i=0; i<6; i++)
for(int j=0; j<6; j++)
mm=min(mm,abs(a[i]-x)+abs(a[j]-y)+dis[i][j]);
mm%=mmod;
ans=(ans+(long long )mm*(long long )k)%mmod;
}
cout<<ans<<endl;
}
}