首页 > 其他分享 >[CF1253F] Cheap Robot

[CF1253F] Cheap Robot

时间:2023-12-12 13:12:51浏览次数:31  
标签:le edge energy Cheap Robot robot CF1253F points rightarrow

Cheap Robot

题面翻译

给你一张 \(N\) 个点的带权无向连通图,其中结点 \(1,2,…,k\) 为充电中心。

一个机器人在图中行走,假设机器人的电池容量为 \(c\),则任何时刻,机器人的电量 \(x\) 都必须满足 \(0\le x\le c\)。如果机器人沿着一条边权为 \(w\) 的边从结点 \(i\) 走到结点 \(j\),它的电量会减少 \(w\)。机器人可以在到达某个充电中心时把电量充满。

现在有 \(q\) 个询问,每次询问机器人要从 \(a\) 点到达 \(b\) 点,电池容量至少为多少,各个询问相互独立。保证 \(a\) 点和 \(b\) 点都是充电中心。

translated by vectorwyx

题目描述

You're given a simple, undirected, connected, weighted graph with $ n $ nodes and $ m $ edges.

Nodes are numbered from $ 1 $ to $ n $ . There are exactly $ k $ centrals (recharge points), which are nodes $ 1, 2, \ldots, k $ .

We consider a robot moving into this graph, with a battery of capacity $ c $ , not fixed by the constructor yet. At any time, the battery contains an integer amount $ x $ of energy between $ 0 $ and $ c $ inclusive.

Traversing an edge of weight $ w_i $ is possible only if $ x \ge w_i $ , and costs $ w_i $ energy points ( $ x := x - w_i $ ).

Moreover, when the robot reaches a central, its battery is entirely recharged ( $ x := c $ ).

You're given $ q $ independent missions, the $ i $ -th mission requires to move the robot from central $ a_i $ to central $ b_i $ .

For each mission, you should tell the minimum capacity required to acheive it.

输入格式

The first line contains four integers $ n $ , $ m $ , $ k $ and $ q $ ( $ 2 \le k \le n \le 10^5 $ and $ 1 \le m, q \le 3 \cdot 10^5 $ ).

The $ i $ -th of the next $ m $ lines contains three integers $ u_i $ , $ v_i $ and $ w_i $ ( $ 1 \le u_i, v_i \le n $ , $ u_i \neq v_i $ , $ 1 \le w_i \le 10^9 $ ), that mean that there's an edge between nodes $ u $ and $ v $ , with a weight $ w_i $ .

It is guaranteed that the given graph is simple (there is no self-loop, and there is at most one edge between every pair of nodes) and connected.

The $ i $ -th of the next $ q $ lines contains two integers $ a_i $ and $ b_i $ ( $ 1 \le a_i, b_i \le k $ , $ a_i \neq b_i $ ).

输出格式

You have to output $ q $ lines, where the $ i $ -th line contains a single integer : the minimum capacity required to acheive the $ i $ -th mission.

样例 #1

样例输入 #1

10 9 3 1
10 9 11
9 2 37
2 4 4
4 1 8
1 5 2
5 7 3
7 3 2
3 8 4
8 6 13
2 3

样例输出 #1

12

样例 #2

样例输入 #2

9 11 3 2
1 3 99
1 4 5
4 5 3
5 6 3
6 4 11
6 7 21
7 2 6
7 8 4
8 9 3
9 2 57
9 3 2
3 1
2 3

样例输出 #2

38
15

提示

In the first example, the graph is the chain $ 10 - 9 - 2^C - 4 - 1^C - 5 - 7 - 3^C - 8 - 6 $ , where centrals are nodes $ 1 $ , $ 2 $ and $ 3 $ .

For the mission $ (2, 3) $ , there is only one simple path possible. Here is a simulation of this mission when the capacity is $ 12 $ .

  • The robot begins on the node $ 2 $ , with $ c = 12 $ energy points.
  • The robot uses an edge of weight $ 4 $ .
  • The robot reaches the node $ 4 $ , with $ 12 - 4 = 8 $ energy points.
  • The robot uses an edge of weight $ 8 $ .
  • The robot reaches the node $ 1 $ with $ 8 - 8 = 0 $ energy points.
  • The robot is on a central, so its battery is recharged. He has now $ c = 12 $ energy points.
  • The robot uses an edge of weight $ 2 $ .
  • The robot is on the node $ 5 $ , with $ 12 - 2 = 10 $ energy points.
  • The robot uses an edge of weight $ 3 $ .
  • The robot is on the node $ 7 $ , with $ 10 - 3 = 7 $ energy points.
  • The robot uses an edge of weight $ 2 $ .
  • The robot is on the node $ 3 $ , with $ 7 - 2 = 5 $ energy points.
  • The robot is on a central, so its battery is recharged. He has now $ c = 12 $ energy points.
  • End of the simulation.

Note that if value of $ c $ was lower than $ 12 $ , we would have less than $ 8 $ energy points on node $ 4 $ , and we would be unable to use the edge $ 4 \leftrightarrow 1 $ of weight $ 8 $ . Hence $ 12 $ is the minimum capacity required to acheive the mission.

The graph of the second example is described here (centrals are red nodes):

The robot can acheive the mission $ (3, 1) $ with a battery of capacity $ c = 38 $ , using the path $ 3 \rightarrow 9 \rightarrow 8 \rightarrow 7 \rightarrow 2 \rightarrow 7 \rightarrow 6 \rightarrow 5 \rightarrow 4 \rightarrow 1 $

The robot can acheive the mission $ (2, 3) $ with a battery of capacity $ c = 15 $ , using the path $ 2 \rightarrow 7 \rightarrow 8 \rightarrow 9 \rightarrow 3 $

先用 dijkstra 求出每个点距离离他最近的关键点的距离,设点 \(u\) 的距离为 \(d_u\)

设点 \(u\) 的容量为 \(x_u\),那么 一定满足 \(c-d_u\ge x_u\ge d_u\),因为他一定要能从最近的关键点走来,再走回最近的关键点。

那么点 \(u\) 到 \(v\) 有一条边权为 \(w\) 的边的话, \(d_v\le x_u-w\le c-d_u-w\)

可以得到 \(c\ge d_u+d_v+w\),现在要找一个最大边权最小的路径,发现这个就是最小生成树上的路径。

多次询问就倍增。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e5+5;
int n,m,k,q,e_num,hd[N],id[N<<2],v[N],fa[N],f[N][20],dep[N];
LL d[N],mx[N][20];
struct edge{
	int u,v,nxt;
	LL w;
}e[N<<3];
int find(int x)
{
	if(fa[x]==x)
		return x;
	return fa[x]=find(fa[x]);
}
void add_edge(int u,int v,LL w)
{
	e[++e_num]=(edge){u,v,hd[u],w};
	hd[u]=e_num;
}
struct node{
	int v;
	LL w;
	bool operator<(const node&n)const{
		return w>n.w;
	}
};
void dijkstra()
{
	priority_queue<node>q;
	memset(d,0x7f,sizeof(d));
	for(int i=1;i<=k;i++)
		q.push((node){i,d[i]=0});
	while(!q.empty())
	{
		int k=q.top().v;
		q.pop();
		if(v[k])
			continue;
		v[k]=1;
		for(int i=hd[k];i;i=e[i].nxt)
		{
			if(d[e[i].v]>d[k]+e[i].w)
			{
				d[e[i].v]=d[k]+e[i].w;
				q.push((node){e[i].v,d[e[i].v]});
			}
		}
	}
}
int cmp(int x,int y)
{
	return e[x].w<e[y].w;
}
void kruskal()
{
	static int u[N<<2],v[N<<2];
	static LL w[N<<2];
	for(int i=1;i<=n;i++)
		fa[i]=i;
	for(int i=1;i<=m;i++)
		e[id[i]].w+=d[e[id[i]].u]+d[e[id[i]].v];
	sort(id+1,id+m+1,cmp);
	for(int i=1;i<=m;i++)
		u[i]=e[id[i]].u,v[i]=e[id[i]].v,w[i]=e[id[i]].w;
	memset(hd,0,sizeof(hd));
	e_num=0;
	for(int i=1;i<=m;i++)
	{
		if(find(u[i])^find(v[i]))
		{
			fa[find(u[i])]=find(v[i]);
			add_edge(u[i],v[i],w[i]),add_edge(v[i],u[i],w[i]);
		}
	}
}
void dfs(int x,int y)
{
	f[x][0]=y,dep[x]=dep[y]+1;
	for(int i=1;i<20;i++)
		f[x][i]=f[f[x][i-1]][i-1],mx[x][i]=max(mx[x][i-1],mx[f[x][i-1]][i-1]);
	for(int i=hd[x];i;i=e[i].nxt)
		if(e[i].v^y)
			mx[e[i].v][0]=e[i].w,dfs(e[i].v,x);
}
LL ask(int x,int y)
{
	LL ans=0;
	if(dep[x]<dep[y])
		swap(x,y);
	for(int i=19;~i;i--)
		if(dep[f[x][i]]>=dep[y])
			ans=max(ans,mx[x][i]),x=f[x][i];
	if(x==y)
		return ans;
	for(int i=19;~i;i--)
		if(f[x][i]^f[y][i])
			ans=max({ans,mx[x][i],mx[y][i]}),x=f[x][i],y=f[y][i];
	return max({ans,mx[x][0],mx[y][0]});
}
int main()
{
	scanf("%d%d%d%d",&n,&m,&k,&q);
	for(int i=1,u,v,w;i<=m;i++)
		scanf("%d%d%d",&u,&v,&w),add_edge(u,v,w),id[i]=e_num,add_edge(v,u,w);
	dijkstra();
	kruskal();
	dfs(1,0);
	while(q--)
	{
		int u,v;
		scanf("%d%d",&u,&v);
		printf("%lld\n",ask(u,v));
	}
}

标签:le,edge,energy,Cheap,Robot,robot,CF1253F,points,rightarrow
From: https://www.cnblogs.com/mekoszc/p/17896543.html

相关文章

  • robots.txt禁止收录协议写法
    1. 什么是robots.txt?robots.txt 是网站和搜索引擎的协议的纯文本文件。当一个搜索引擎蜘蛛来访问站点时,它首先爬行来检查该站点根目录下是否存在robots.txt,如果存在,根据文件内容来确定访问范围,如果没有,蜘蛛就沿着链接抓取。robots.txt 放在项目的根目录下。2. robots.txt......
  • 初中英语优秀范文100篇-021Sophia the Robot-机器人索菲亚
    PDF格式公众号回复关键字:SHCZFW021记忆树1WhenitcomestoAI,Sophiatherobotismentionedagainandagain.翻译说到人工智能,总是会反复提到机器人索菲亚。简化记忆反复句子结构句子结构分析:主句:Sophiatherobotismentionedagainandagain.主语:Sophia......
  • OSCP(提高篇靶机mrRobot)
    第一步:nmap与dirb   fsocity.dic这个可能是用户或者密码的字典,发现内部存在大量重复数据 catfsocity.dic|sort|uniq>fsocietyuniq 进入登录界面test/test登录 第二步:使用hydra进行用户名密码暴力破解Invalidusername不存在该用户名  F=不存在这......
  • D. Robot Queries
    D.RobotQueriesThereisaninfinite$2$-dimensionalgrid.Initially,arobotstandsinthepoint$(0,0)$.Therobotcanexecutefourcommands:U—movefrompoint$(x,y)$to$(x,y+1)$;D—movefrompoint$(x,y)$to$(x,y-1)$;L—movefrompo......
  • CF1902 D Robot Queries 题解
    LinkCF1902DRobotQueriesQuestionRobot初始在\((0,0)\),有一个字符串\(s\),表示运行列表\(U\):y+1\(D\):y-1\(L\):x-1\(R\):x+1之后有\(Q\)次询问,有\(L,R,x,y\),问把运行序列的\([L,R]\)反转,Robot是否经过了点\((x,y)\)Solution显然,对于一个区间\([L,R]\)......
  • Web_XCTF_WriteUp | Training-WWW-Robots
    题目分析标题大致翻译:训练WWW网络爬虫。场景内部文段大致翻译:在这个小小的训练挑战中,您将学习Robots_exclusion_standard(网络爬虫排除标准)。robots.txt文件用于网络爬虫检查它们是否被允许抓取和索引您的网站或仅部分网站。有时,这些文件揭示了目录结构,而不是保护内......
  • RoboTAP笔记
    title:RoboTAP笔记banner_img:https://drive.studyinglover.com/api/raw/?path=/photos/blog/background/1679396994125.pngindex_img:https://cdn.studyinglover.com/pic/2023/08/15ff4915dff842e47e91d580d0d0fe5c.pngdate:2023-9-112:35:00categories:-笔记tags:-......
  • robots后台泄露
     [^来源:ctfshow-vip题目限免  考点:robots.txt文件泄露后台路径WP1.题目 唉,就是一道简单robots文件泄露,但是我为什么要写这个呢,因为我真的大可爱,一直搁那/robots,,,,,我说怎么没反应,,,无语,,,是robots.txt文件啊,文件我不加后缀名,我服了,我记得之前也是做过两次这种的题......
  • 【misc】[CISCN 2021初赛]robot --流量包数据提取,坐标画图
    打开附件的流量包可以发现有很多的tcp协议数据,追踪tcp协议数据看看可以发现tcp数据流中有很多类似坐标的东西,先把这些数据另存为txt保存,如何用正则表达式提取这些数据,提取脚本如下:importrewithopen("data.txt","r",encoding="utf-8")asf:    data=f.read......
  • GlibcHeap-house-of-muney分析
    目录GlibcHeap-house-of-muney分析前言利用原理ELF文件解析符号查找利用过程POC思考参考GlibcHeap-house-of-muney分析houseofmuney的学习笔记。前言遇到了好几次hosueofmuney相关的题目,之前并没有深入地分析houseofmuney的原理,只是了解了一个大概。这次详细分析一下......