Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 285 Accepted Submission(s): 92
Problem Description
There is a path graph 1.
You are given the graph and several queries about the shortest path between some pairs of vertices.
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 6.
Output
For each test cases, output an integer i-th query.
Sample Input
1
10 2
2 4 5 7 8 10
1 5
3 1
Sample Output
7
Source
bc 题对于我们这种渣渣来说 太感人了
看了题解做的 比赛的时候 传统的最短路都会超时不用想 想dfs 但姿势表达太渣
求最短路 初始时 单链 每点间距离为1 另添加距离为1的3条边 也就是由关键的6个点
关键6个点floyd 处理 具体看代码 理解这个都好久 还是太弱
在询问中 6*6 种与查询区间枚举比较找到 最短路
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<vector> 5 #include<cmath> 6 #define ll __int64 7 using namespace std; 8 ll t; 9 ll n,q; 10 ll a[10]; 11 ll dis[10][10]; 12 int main() 13 { 14 scanf("%I64d",&t); 15 for(int i=1;i<=t;i++) 16 { 17 memset(dis,0,sizeof(dis)); 18 scanf("%I64d %I64d",&n,&q); 19 scanf("%I64d %I64d %I64d %I64d %I64d %I64d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6]); 20 ll sum=0; 21 for(int j=1;j<=6;j++) 22 for(int k=1;k<=6;k++) 23 { 24 dis[j][k]=abs(a[j]-a[k]); 25 } 26 dis[1][2]=1; 27 dis[2][1]=1; 28 dis[3][4]=1; 29 dis[4][3]=1; 30 dis[5][6]=1; 31 dis[6][5]=1; 32 for(int i=1;i<=6;i++) 33 for(int k=1;k<=6;k++) 34 for(int m=1;m<=6;m++) 35 { 36 if(dis[k][i]+dis[i][m]<dis[k][m]) 37 dis[k][m]=dis[k][i]+dis[i][m]; 38 } 39 for(int j=1;j<=q;j++) 40 { 41 ll aa,bb,t; 42 scanf("%I64d %I64d",&aa,&bb); 43 if(aa>bb) 44 { 45 t=aa; 46 aa=bb; 47 bb=t; 48 } 49 ll ans=abs(bb-aa); 50 for(int k=1;k<=6;k++) 51 for(int m=1;m<=6;m++) 52 { 53 if(ans>abs(aa-a[k])+abs(bb-a[m])+dis[k][m]) 54 ans=abs(aa-a[k])+abs(bb-a[m])+dis[k][m]; 55 } 56 sum=(sum+j*ans)%1000000007; 57 } 58 printf("%I64d\n",sum); 59 } 60 return 0; 61 }