社区讨论
dijk 0pts求条
P6833[Cnoi2020] 雷雨参与者 2已保存回复 4
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 4 条
- 当前快照
- 1 份
- 快照标识符
- @mhjo14dq
- 此快照首次捕获于
- 2025/11/04 05:42 4 个月前
- 此快照最后确认于
- 2025/11/04 05:42 4 个月前
CPP
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=1e6+5;
int n,m,a,b,c,x,y,ans=1e9;
int dist1[1005][1005],dist2[1005][1005],dist3[1005][1005];
int t[1005][1005];
bool vis[1005][1005];
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
struct pts{
int x,y;
int dist;
bool operator<(const pts & x)const{
return x.dist<dist;
}
};
void dijk(int x,int y,int dist[][1005]){
// memset(dist,0x3f,sizeof(dist));
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
dist[i][j]=1e18;
}
}
memset(vis,0,sizeof(vis));
dist[x][y]=t[x][y];
priority_queue<pts> q;
q.push({x,y,t[x][y]});
while(!q.empty()){
x=q.top().x;
y=q.top().y;
q.pop();
if(vis[x][y]){
continue;
}
vis[x][y]=1;
for(int i=0;i<4;i++){
int nx=x+dx[i],ny=y+dy[i];
if(nx<1||ny<1||nx>n||ny>m){
continue;
}
if(dist[nx][ny]>dist[x][y]+t[nx][ny]){
dist[nx][ny]=dist[x][y]+t[nx][ny];
q.push({nx,ny,dist[nx][ny]});
}
}
}
return;
}
signed main(){
scanf("%d%d%d%d",&n,&m,&a,&b,&c);
for(int i=n;i>=1;i--){
for(int j=1;j<=m;j++){
scanf("%d",&t[i][j]);
// flag=t[i][j]!=1;
}
}
dijk(n,a,dist1);
dijk(1,b,dist2);
dijk(1,c,dist3);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
// cout<<dist1[i][j]<<' '<<dist2[i][j]<<' '<<d
ans=min(ans,dist1[i][j]+dist2[i][j]+dist3[i][j]-t[i][j]*2);
}
}
cout<<ans+1;
return 0;
}
/*
5 5 1 2 4
1 8 1 6 6
1 1 1 2 4
8 3 1 2 2
1 2 1 9 1
1 0 9 1 1
*/
回复
共 4 条回复,欢迎继续交流。
正在加载回复...