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),通过枚举中间经过的特殊点,观察是否能使得距离减小。
/* ***********************************************
Author :111qqz
Created Time :2016年03月31日 星期四 17时18分34秒
File Name :code/hdu/5636.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=1E5+7;
7const LL MOD =1E9+7;
8int n,m;
9int z[N];
10LL a[10];
11LL dp[10][10];
12int main()
13{
14 #ifndef ONLINE_JUDGE
15 freopen("code/in.txt","r",stdin);
16 #endif
17 int T;
18 ios::sync_with_stdio(false);
19 cin>>T;
20 while (T--){
21 cin>>n>>m;
22 for ( int i = 1 ;i <= 6 ; i++) cin>>a[i];
23 for ( int i = 1 ; i <= 6 ;i ++)
24 {
25 for ( int j = 1 ; j <= 6 ; j++) //初始化 //以这六个点构建了张新图,有边相连权为1,否则为点距。
26 dp[i][j] = abs(a[i]-a[j]);
27 }
1 for ( int i = 1 ;i <= 6 ; i+=2)
2 {
3 if (a[i]!=a[i+1]) dp[i][i+1]=dp[i+1][i]=1;
4 }
1 for ( int k = 1 ;k <= 6 ; k++)
2 for ( int i = 1 ; i <= 6 ; i++)
3 for ( int j = 1 ; j <= 6 ; j++)
4 dp[i][j] = min(dp[i][j],dp[i][k]+dp[k][j]);
1 LL ans = 0 ;
2 LL cas = 0 ;
3 while (m--)
4 {
5 cas++;
6 LL s,t;
7 cin>>s>>t;
8 LL res = abs(s-t);
1 for ( int i = 1 ; i <= 6 ; i++) //枚举经过的中间点
2 for ( int j = 1 ; j <= 6 ; j++)
3 res = min(res,abs(s-a[i])+dp[i][j]+abs(t-a[j]));
4 // cout<<"res:"<<res<<endl;
5 ans = (ans + (cas*res)%MOD)%MOD;
6 }
7 cout<<ans<<endl;
8 }
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}