bc #74 div1 1001 || hdu 5636 Shortest Path (floyd?)
题目链接 题意:有一条n个节点的链,节点i和节点j的距离为abs(i-j) 现在新增加三条边,距离也都为1,然后给出m个询问,每组询问给出两个点s,t,问s,t之间的最短距离。 思路:比赛的时候没搞出来。 观察特点,对于大多数点来说,都是没有直接的改变,只是增加了三条边。总的思路是:之前s到t的距离为abs(s-t),通过枚举中间经过的特殊点,观察是否能使得距离减小。
1/* ***********************************************
2Author :111qqz
3Created Time :2016年03月31日 星期四 17时18分34秒
4File Name :code/hdu/5636.cpp
5************************************************ */
6
7#include <cstdio>
8#include <cstring>
9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=1E5+7;
34const LL MOD =1E9+7;
35int n,m;
36int z[N];
37LL a[10];
38LL dp[10][10];
39int main()
40{
41 #ifndef ONLINE_JUDGE
42 freopen("code/in.txt","r",stdin);
43 #endif
44 int T;
45 ios::sync_with_stdio(false);
46 cin>>T;
47 while (T--){
48 cin>>n>>m;
49 for ( int i = 1 ;i <= 6 ; i++) cin>>a[i];
50 for ( int i = 1 ; i <= 6 ;i ++)
51 {
52 for ( int j = 1 ; j <= 6 ; j++) //初始化 //以这六个点构建了张新图,有边相连权为1,否则为点距。
53 dp[i][j] = abs(a[i]-a[j]);
54 }
55
56 for ( int i = 1 ;i <= 6 ; i+=2)
57 {
58 if (a[i]!=a[i+1]) dp[i][i+1]=dp[i+1][i]=1;
59 }
60
61
62 for ( int k = 1 ;k <= 6 ; k++)
63 for ( int i = 1 ; i <= 6 ; i++)
64 for ( int j = 1 ; j <= 6 ; j++)
65 dp[i][j] = min(dp[i][j],dp[i][k]+dp[k][j]);
66
67 LL ans = 0 ;
68 LL cas = 0 ;
69 while (m--)
70 {
71 cas++;
72 LL s,t;
73 cin>>s>>t;
74 LL res = abs(s-t);
75
76 for ( int i = 1 ; i <= 6 ; i++) //枚举经过的中间点
77 for ( int j = 1 ; j <= 6 ; j++)
78 res = min(res,abs(s-a[i])+dp[i][j]+abs(t-a[j]));
79 // cout<<"res:"<<res<<endl;
80 ans = (ans + (cas*res)%MOD)%MOD;
81 }
82 cout<<ans<<endl;
83 }
84
85 #ifndef ONLINE_JUDGE
86 fclose(stdin);
87 #endif
88 return 0;
89}