zoukankan      html  css  js  c++  java
  • BZOJ 3430: [Usaco2014 Jan]Ski Course Rating(并查集+贪心)

    题面

    Time Limit: 10 Sec Memory Limit: 128 MB
    Submit: 136 Solved: 90
    [Submit][Status][Discuss]
    Description
    The cross-country skiing course at the winter Moolympics is described by an M x N grid of elevations (1 <= M,N <= 500), each elevation being in the range 0 .. 1,000,000,000. Some of the cells in this grid are designated as starting points for the course. The organizers of the Moolympics want to assign a difficulty rating to each starting point. The difficulty level of a starting point P should be the minimum possible value of D such that a cow can successfully reach at least T total cells of the grid (1 <= T <= MN), if she starts at P and can only move from cell to adjacent cell if the absolute difference in elevation between the cells is at most D. Two cells are adjacent if one is directly north, south, east, or west of the other. Please help the organizers compute the difficulty rating for each starting point.

    给定一个n*m的高度矩阵,一个点的难度P是指 最小的D使得以这个点为起点,可到达的格子数量至少为T,一个格子可以到另外一个格子必须保证两个格子相邻且高度差不超过D,相邻是指4-相邻。再给定一个同样大小的01矩阵,求出所有1处格子的难度和。

    Input

    • Line 1: The integers M, N, and T.

    • Lines 2..1+M: Each of these M lines contains N integer elevations.

    • Lines 2+M..1+2M: Each of these M lines contains N values that are either 0 or 1, with 1 indicating a cell that is a starting point.

    Output

    • Line 1: The sum of difficulty ratings of all starting points (note that this may not fit into a 32-bit integer, even though individual difficulty ratings will).

    Sample Input
    3 5 10

    20 21 18 99 5

    19 22 20 16 17

    18 17 40 60 80

    1 0 0 0 0

    0 0 0 0 0

    0 0 0 0 1
    Sample Output
    24

    OUTPUT DETAILS: The difficulty rating of the upper-left starting point is 4, and for the lower-right it is 20.

    解题思路

      首先这道题肯定要用并查集来维护连通性,按照边权排序后一条一条的加边,在并查集中还要维护一下每个联通块的大小和联通块内起点的数量,因为每个点只会被更新一次,所以当两个联通块合并时大小(>=t),而合并前没有时就统计答案。

    代码

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    
    using namespace std;
    const int MAXN = 505*505;
    typedef long long LL;
    
    inline int rd(){
    	int x=0,f=1;char ch=getchar();
    	while(!isdigit(ch)) f=ch=='-'?0:1,ch=getchar();
    	while(isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
    	return f?x:-x;
    }
    
    int n,m,siz[MAXN],fa[MAXN],t,cnt,num,a[505][505],sum[MAXN];
    LL ans;
    
    struct Edge{
    	int u,v,w;
    	friend bool operator<(const Edge A,const Edge B){
    		return A.w<B.w;
    	}
    }edge[MAXN<<2];
    
    inline int calc(int x,int y){
    	return (x-1)*m+y;
    }
    
    inline void add(int x,int y,int w){
    	edge[++cnt].u=x;edge[cnt].v=y;edge[cnt].w=w;
    }
    
    int find(int x){
    	if(fa[x]==x) return x;
    	return fa[x]=find(fa[x]);
    }
    
    int main(){
    	n=rd(),m=rd(),t=rd();
    	for(int i=1;i<=n;i++)
    		for(int j=1;j<=m;j++){
    			a[i][j]=rd();
    			if(i!=1) add(calc(i-1,j),calc(i,j),abs(a[i][j]-a[i-1][j]));
    			if(j!=1) add(calc(i,j-1),calc(i,j),abs(a[i][j]-a[i][j-1]));
    		}
    	for(int i=1;i<=n;i++)
    		for(int j=1;j<=m;j++)	
    			fa[calc(i,j)]=calc(i,j),siz[calc(i,j)]=rd(),sum[calc(i,j)]=1;
    	sort(edge+1,edge+1+cnt);
    	for(int i=1;i<=cnt;i++){
    		int u=find(edge[i].u),v=find(edge[i].v);
    		if(u==v) continue;
    		if(sum[u]<t && sum[u]+sum[v]>=t) ans+=(LL)siz[u]*edge[i].w;
    		if(sum[v]<t && sum[u]+sum[v]>=t) ans+=(LL)siz[v]*edge[i].w;
    		fa[u]=v;siz[v]+=siz[u];sum[v]+=sum[u];
    	}
    	printf("%lld
    ",ans);
    	return 0;
    }
    
  • 相关阅读:
    springboot项目搭建
    linux之scp
    docker文件拷贝
    vue数据绑定不刷新可能情况
    css弹框
    jqgrid跨站脚本漏洞解决
    springboot配置文件加载顺序
    git之在eclipse上玩(一)
    windows系统日志位置
    maven
  • 原文地址:https://www.cnblogs.com/sdfzsyq/p/10132613.html
Copyright © 2011-2022 走看看