ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer 最大生成树+LCA

After the long vacation, the maze designer master has to do his job. A tour company gives him a map which is a rectangle. The map consists of N \times MN×M little squares. That is to say, the height of the rectangle is NN and the width of the rectangle is MM. The master knows exactly how the maze is going to use. The tour company will put a couple in two different squares in the maze and make them seek each other. Of course,the master will not make them find each other easily. The only thing the master does is building some wall between some little squares. He knows in that way, wherever the couple is put, there is only one path between them. It is not a difficult thing for him, but he is a considerate man. He also knows that the cost of building every wall between two adjacent squares is different(Nobody knows the reason). As a result, he designs the maze to make the tour company spend the least money to build it.

Now, here's your part. The tour company knows you're the apprentice of the master, so they give you a task. you're given QQ qustions which contain the information of where the couple will be put. You need to figure out the length of the shortest path between them.

However,the master doesn't tell you how he designs the maze, but he believes that you, the best student of himself, know the way. So he goes on vacation again.

Input

The first line of the input contains two integers NN and MM (1 \le N,M \le 5001≤N,M≤500), giving the number of rows and columns of the maze.

The next N \times MN×M lines of the input give the information of every little square in the maze, and their coordinates are in order of (1,1)(1,1) , (1,2)(1,2) \cdots⋯ (1,M)(1,M) , (2,1)(2,1) , (2,2)(2,2) , \cdots⋯ , (2,M)(2,M) , \cdots⋯ ,(N,M)(N,M).

Each line contains two characters DD and RR and two integers aa , bb (0 \le a,b \le 20000000000≤a,b≤2000000000 ), aa is the cost of building the wall between it and its lower adjacent square, and bb is the cost of building the wall between it and its right adjacent square. If the side is boundary, the lacking path will be replaced with X 00.

The next line contains an integer QQ (1 \le Q \le 1000001≤Q≤100000 ), which represents the number of questions.

The next QQ lines gives four integers, x_1x1​, y_1y1​, x_2x2​, y_2y2​ ( 1 \le x_11≤x1​ , x_2 \le Nx2​≤N , 1 \le y_11≤y1​ , y_2 \le My2​≤M ), which represent two squares and their coordinate are (x_1x1​ , y_1y1​) and (x_2x2​ , y_2y2​).

(xx,yy) means row xx and column yy.

It is guaranteed that there is only one kind of maze.

Output

For each question, output one line with one integer which represents the length of the shortest path between two given squares.

样例输入复制

3 3
D 1 R 9
D 7 R 8
D 4 X 0
D 2 R 6
D 12 R 5
D 3 X 0
X 0 R 10
X 0 R 11
X 0 X 0
3
1 1 3 3
1 2 3 2
2 2 3 1

样例输出复制

4
2
2

题目来源

ACM-ICPC 2018 徐州赛区网络预赛

编辑代码

 

题意:

一个n*m的格子,每个点可以往右和往下连边,给你对应的连边的权值,问你最少建立多少花费的边使所有点之间都可以互相达到,并且保证路径是唯一的,问你这样花费最小是多少?

分析:

最小花费建立路障,最大花费建立边,即最大生成树即可。

树上最短距离,直接跑LCA即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long long LL;
const int N=500005;
struct Edge
{
    int u,v;
    LL w;
} edge[N];
bool operator < (Edge a,Edge b)
{
    return a.w>b.w;
}
int cnt;
void add(int u,int v,int w)
{
    edge[++cnt].u=u;
    edge[cnt].v=v;
    edge[cnt].w=w;

}
int n,m;
char op1[10],op2[10];
LL w1,w2;
int getid(int x,int y)
{
    return 600*x+y;
}
int fa[N*10+1];
int getfa(int x)
{
	if(x==fa[x]) return x;
	return fa[x]=getfa(fa[x]);
} 
const int SIZE = 500010;
struct LCA
{
  
    int f[SIZE][20], d[SIZE];
    LL dist[SIZE];
    int ver[2 * SIZE], Next[2 * SIZE], edge[2 * SIZE], head[SIZE];
    int tot, t;
    queue<int> q;

    void add(int x, int y, int z)
    {
        ver[++tot] = y;
        edge[tot] = z;
        Next[tot] = head[x];
        head[x] = tot;
    }

   // 预处理
    void bfs(int sx)
    {
    	while(!q.empty()) q.pop();
        q.push(sx);
        d[sx] = 1;
        while (q.size())
        {
            int x = q.front();
            q.pop();
            for (int i = head[x]; i; i = Next[i])
            {
                int y = ver[i];
                if (d[y])
                    continue;
                d[y] = d[x] + 1;
                dist[y] = dist[x] + edge[i];
                f[y][0] = x;
                for (int j = 1; j <= t; j++)
                    f[y][j] = f[f[y][j - 1]][j - 1];
                q.push(y);
            }
        }
    }

   //回答一个询问
    int getlca(int x, int y)
    {
        if (d[x] > d[y])
            swap(x, y);
        for (int i = t; i >= 0; i--)
            if (d[f[y][i]] >= d[x])
                y = f[y][i];
        if (x == y)
            return x;
        for (int i = t; i >= 0; i--)
            if (f[x][i] != f[y][i])
                x = f[x][i], y = f[y][i];
        return f[x][0];
    }
    void init(int maxn) //maxn为节点下标的最大值
    {
		t = (int)(log(maxn) / log(2)) + 1;
		for (int i = 1; i <= maxn; i++)
			head[i] = d[i] = 0;
		tot = 0;
		
    }
}lca;

LL Kruskal()
{
	sort(edge+1,edge+cnt+1);
    for(int i=1;i<=N*10;i++)
    	fa[i]=i;
	LL res=0;
	int num=0;
	for(int i=1;i<=cnt;i++)
	{
		int fu=getfa(edge[i].u);
		int fv=getfa(edge[i].v);
		if(fu==fv) continue;
		lca.add(edge[i].u,edge[i].v,1);
		//cout<<edge[i].u<<" "<<edge[i].v<<endl;
		lca.add(edge[i].v,edge[i].u,1);
		fa[fu]=fv;
		res+=edge[i].w;
		if(num==n*m-1)
			break;
		
	}
	return res;
}


int main()
{
    scanf("%d%d",&n,&m);
    cnt=0;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            scanf("%s%lld%s%lld",op1,&w1,op2,&w2);
            if(op1[0]=='D')
            {
                add(getid(i,j),getid(i+1,j),w1);
            }
            if(op2[0]=='R')
            {
                add(getid(i,j),getid(i,j+1),w2);
            }
        }
    }
    lca.init(getid(n,m)+1);
    Kruskal();
    
    lca.bfs(getid(1,1));
    int q;
    scanf("%d",&q);
    int x1,y1,x2,y2,u,v;
    while(q--)
	{
		
		scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
		u=getid(x1,y1);
		v=getid(x2,y2);
		//cout<<u<<" "<<v<<endl;
		printf("%lld\n", lca.dist[u] + lca.dist[v] - 2 * lca.dist[lca.getlca(u, v)]);
	}
    
    
    

    return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值