P9026 [CCC2021 S4] Daily Commute
1.F. Chat Screenshots2.P1656 炸铁路3.P1137 旅行计划4.P2835 刻录光盘5.P1197 [JSOI2008] 星球大战6.P3388 【模板】割点(割顶)7.P8435 【模板】点双连通分量8.P8436 【模板】边双连通分量9.P2860 [USACO06JAN] Redundant Paths G10.P1653 [USACO04DEC] Cow Ski Area G11.P3047 [USACO12FEB] Nearby Cows G12.P1894 [USACO4.2] 完美的牛栏The Perfect Stall13.P1550 [USACO08OCT] Watering Hole G14.P2330 [SCOI2005] 繁忙的都市15.P1525 [NOIP2010 提高组] 关押罪犯16.P1379 八数码难题17.P2746 [USACO5.3] 校园网Network of Schools18.P6121 [USACO16OPEN] Closing the Farm G19.P2341 [USACO03FALL / HAOI2006] 受欢迎的牛 G20.P2055 [ZJOI2009] 假期的宿舍21.P5905 【模板】全源最短路(Johnson)22.F. Microcycle23.G. Path Prefixes24.G. Rudolf and Subway25.C. Ehab and Path-etic MEXs26.A. String Transformation 127.D. Secret Passwords28.F. Maximum White Subtree29.P3478 [POI2008] STA-Station30.P1347 排序31.P1960 郁闷的记者32.E1. Weights Division (easy version)33.P5007 DDOSvoid 的疑惑34.P2850 [USACO06DEC] Wormholes G35.P1265 公路修建36.P1354 房间最短路问题37.P2168 [NOI2015] 荷马史诗38.P8306 【模板】字典树39.P1481 魔族密码40.P3128 [USACO15DEC] Max Flow P41.P5536 【XR-3】核心城市42.P5836 [USACO19DEC] Milk Visits S43.P3384 【模板】重链剖分/树链剖分44.P5960 【模板】差分约束45.P7771 【模板】欧拉路径46.六度分离47.整数区间48.F. Alex's whims49.J. 上学50.Game on Tree51.E. We Need More Bosses52.B. Omkar and Heavenly Tree53.B. Mahmoud and Ehab and the bipartiteness54.P1668 [USACO04DEC] Cleaning Shifts S55.P6154 游走56.P8655 [蓝桥杯 2017 国 B] 发现环57.P10298 [CCC 2024 S4] Painting Roads58.P9650 [SNCPC2019] Escape Plan59.P9327 [CCC 2023 S4] Minimum Cost Roads
60.P9026 [CCC2021 S4] Daily Commute
61.P8724 [蓝桥杯 2020 省 AB3] 限高杆62.P4878 [USACO05DEC] Layout G63.P5663 [CSP-J2019] 加工零件64.P2731 [USACO3.3] 骑马修栅栏 Riding the Fences65.I. Disks66.P1351 [NOIP2014 提高组] 联合权值67.B. Time Travel68.F. Minimum Maximum Distance69.A. Book70.P1407 [国家集训队] 稳定婚姻71.P1991 无线通讯网72.P4047 [JSOI2010] 部落划分73.P3275 [SCOI2011] 糖果74.P1989 无向图三元环计数75.P1967 [NOIP2013 提高组] 货车运输76.D. Vitaly and Cycle77.P10838 『FLA - I』庭中有奇树78.P9751 [CSP-J 2023] 旅游巴士79.D. Colored Portals一步一步来
1.假设D为1,你要怎么求?
每个点乘地铁的时间是唯一的,也就是说,如果我一开始先走一段路到A点再坐地铁,等价于我直接坐地铁到A点,下地铁的瞬间再次上车。
所以最优路径一定可以是先从起点乘地铁到某个点,然后再一直走路到终点
因此我们可以遍历
由于求多条最短路,所以我们改变建边的方向,求单源最短路
时间复杂度
2.由于D的范围很大,我们没法每天遍历一遍S,发现交换只会影响两个点的下车的最短时间
所以我们可以用堆维护所有点下车的最短时间,只有堆顶元素处于它这一天应该在的位置上时有效
code
using namespace std;
vector<int> G[200005];
int dis[200005];
int s[200005];
struct node
{
int pos,val;
bool operator<(const node &b)const
{
return b.val<val;
}
};
struct unit
{
int id,pos;
bool operator<(const unit &b) const
{
return b.pos-1+dis[b.id]<pos-1+dis[id];
}
};
int main()
{
memset(dis,0x3f,sizeof dis);
int n,w,d;
cin>>n>>w>>d;
for(int i=1;i<=w;i++)
{
int x,y;
cin>>x>>y;
G[y].push_back(x);
}
priority_queue<node> q;
q.push({n,0});
while(q.size())
{
int now=q.top().pos,val=q.top().val;
q.pop();
if(dis[now]<=val) continue;
dis[now]=val;
for(auto next:G[now])
{
if(dis[next]>dis[now]+1)
{
q.push({next,dis[now]+1});
}
}
}
priority_queue<unit> ans;
for(int i=1;i<=n;i++)
{
cin>>s[i];
ans.push({s[i],i});
}
while(d--)
{
int x,y;
cin>>x>>y;
swap(s[x],s[y]);
ans.push({s[x],x});
ans.push({s[y],y});
while(s[ans.top().pos]!=ans.top().id) ans.pop();
cout<<ans.top().pos-1+dis[ans.top().id]<<"\n";
}
return 0;
}
合集:
图论
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)