[题解]AT_abc212_f [ABC212F] Greedy Takahashi
思路
通常来说,对于倍增问题 表示的是以 为起点,走 步的答案。
但是,对此此题,如果对于点倍增,因为每一个点可能会有多条边经过,所以,不能对点进行倍增。
但如果对边进行倍增,无论怎么走 步后的位置一定相同,所以考虑对每一条边倍增。
定义 表示,在第 个城市,时间为 时,第一辆能坐上的车;特别地,如果没有则为 。
其余的与普通倍增相同。
Code
#include <bits/stdc++.h>
#define fst first
#define snd second
#define re register
using namespace std;
typedef pair<int,int> pii;
const int N = 1e5 + 10,M = 25;
int n,m,q;
int dp[N][M];
set<pii> st[N];
struct edge{
int a;
int b;
int s;
int t;
}arr[N];
inline int read(){
int r = 0,w = 1;
char c = getchar();
while (c < '0' || c > '9'){
if (c == '-') w = -1;
c = getchar();
}
while (c >= '0' && c <= '9'){
r = (r << 3) + (r << 1) + (c ^ 48);
c = getchar();
}
return r * w;
}
inline int get(int x,int k){
auto it = st[x].lower_bound({k,0});
if (it == st[x].end()) return -1;
return (*it).snd;
}
int main(){
memset(dp,-1,sizeof(dp));
n = read();
m = read();
q = read();
for (re int i = 1;i <= m;i++){
arr[i].a = read();
arr[i].b = read();
arr[i].s = read();
arr[i].t = read();
st[arr[i].a].insert({arr[i].s,i});
}
for (re int i = 1;i <= m;i++) dp[i][0] = get(arr[i].b,arr[i].t);//维护 dp 数组
for (re int j = 0;j < 20;j++){
for (re int i = 1;i <= m;i++){
if (~dp[i][j - 1]) dp[i][j] = dp[dp[i][j - 1]][j - 1];
}
}
while (q--){
int x,y,z;
x = read();
y = read();
z = read();
int p = get(y,x);
if (!~p) printf("%d\n",y);//无法走动
else{
for (re int i = 19;~i;i--){
if (~dp[p][i] && arr[dp[p][i]].s < z) p = dp[p][i];//一直往后走
}
if (arr[p].s >= z) printf("%d\n",arr[p].a);
else if (arr[p].t < z) printf("%d\n",arr[p].b);
else printf("%d %d\n",arr[p].a,arr[p].b);
}
}
return 0;
}
作者:WaterSun
出处:https://www.cnblogs.com/WaterSun/p/18261945
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】