跳过正文
  1. Posts/

bc #74 div1 1001 || hdu 5636 Shortest Path (floyd?)

·2 分钟

题目链接 题意:有一条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}

相关文章

poj 3660 Cow Contest (floyd,传递闭包)

·1 分钟
http://poj.org/problem?id=3660 题意:给定n个奶牛,m个奶牛的关系,a,b表示a比b强…问能确定多少个奶牛的排名。 思路:最重要的一点是。。能确定奶牛i的排名的条件是。。知道奶牛i和其他n-1个奶牛的关系。。不管是能打败奶牛i也好。。会被奶牛i打败也好。。只要不是不确定就行。。所以我们跑一遍floyd做传递闭包。得到任何两个点之间的联系。然后对于每一个点。看其他n-1个点是否和他有关系。

codeforces #338 div2 B || 615B Longtail Hedgehog

·1 分钟
题目链接 题意:给出n个点,m条边,定义一条路径的价值为【路径长度*(路径终点的度)】,求最大价值。 思路:一月份的时候写过一个回溯。。。TLE22了。。。其实也能猜到是dp..但是无奈不会写。然而其实真的不难== 我们枚举路径的终点,dp[i]表示以点i为终点能得到的最长路径长度。

codeforces 330 B. Road Construction (图论基础)

·2 分钟
题目链接 题意:n个点,m(m<n/2)条不能走的边,问最少连多少条边,使得任何两个点之间的距离最多为2. 输出最少的边数和连接的哪些边。 思路: **数据范围很关键。**数据范围很关键。数据范围很关键。

bc #73 B || hdu 5631 Rikka with Graph (并查集判断无向图的连通性)

·2 分钟
http://acm.hdu.edu.cn/showproblem.php?pid=5631 题意;给出一张n个点n+1(n<=100)条边的无向图,现在删除若干条边(至少一条边),问删完之后图依然联通的方案数。 思路:分析可知,由于只删边,不删点,n个点,最少需要n-1条边才能联通,所以最多删两条边。我们可以暴力枚举删除的两条边(或者一条边) O(n^2)的复杂度完全可以接受。剩下的问题就变成了每次删边之后判断图的连通性。 题解给出的是bfs。。。大概是bfs一遍,然后入队的点数是n就联通? 或者dfs一遍也可以? 也是标记过的点数是n就说明联通? 但是看到排名考前的人都是用到了并查集来判断…比较巧妙。