| 比赛 |
NOIP2025模拟赛4 |
评测结果 |
AAAAAAAAAAAAAAAAAAAA |
| 题目名称 |
Daily Commute |
最终得分 |
100 |
| 用户昵称 |
梦那边的美好TE |
运行时间 |
1.187 s |
| 代码语言 |
C++ |
内存使用 |
12.84 MiB |
| 提交时间 |
2025-11-27 09:55:43 |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+10;
const int inf=0x3f3f3f3f;
int n,m,k,dis[N],p[N],pos[N];
vector<int>G[N];
queue<int>q;
struct node{int v,x,y;};
priority_queue<node>pq;
bool operator < (const node &a,const node &b){
return a.v>b.v;
}
int main(){
freopen("Commute.in","r",stdin);
freopen("Commute.out","w",stdout);
scanf("%d %d %d",&n,&m,&k);
for(int i=1,u,v;i<=m;i++){
scanf("%d %d",&u,&v);
G[v].push_back(u);
}
memset(dis,0x3f,sizeof(dis));
dis[n]=0,q.push(n);
while(q.size()){
int x=q.front();q.pop();
for(auto y:G[x]){
if(dis[y]!=inf)continue;
dis[y]=dis[x]+1;
q.push(y);
}
}
for(int i=1;i<=n;i++){
scanf("%d",p+i);
pos[p[i]]=i;
}
for(int i=1;i<=n;i++){
pq.push(node{pos[i]+dis[i],i,pos[i]});
}
while(k--){
int x,y;scanf("%d %d",&x,&y);
pos[p[x]]=pos[p[y]]=0;
swap(p[x],p[y]);
pos[p[x]]=x,pos[p[y]]=y;
pq.push(node{pos[p[x]]+dis[p[x]],p[x],pos[p[x]]});
pq.push(node{pos[p[y]]+dis[p[y]],p[y],pos[p[y]]});
while(1){
node t=pq.top();
if(pos[t.x]!=t.y){
pq.pop();
}else{
printf("%d\n",t.v-1);
break;
}
}
}
return 0;
}