【NOIP模拟赛】绿洲
程序员文章站
2024-03-20 13:30:28
...
题目翻译过来就是很水的迪杰斯特拉跑图啦,我天真以为直接跑到已有绿洲就继承会很快的没想到更慢了
#include<bits/stdc++.h>
#define inf 1000000000
using namespace std;
int n,m,k;
int head[200005];
int next[200005];
int ver[200005];
int d[100005];
int f[100005];
set<int>city[100005];
int v[100005];
bool gg[100005];
priority_queue<pair<int,int> >q;
struct oo {
int b;
bool vis;
int jl[100005];
} green[105];
int tot;
int read() {
int num=0,f=1;
char ch=getchar();
while(ch>'9'||ch<'0') {
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') {
num=(num<<1)+(num<<3)+ch-'0';
ch=getchar();
}
return num*f;
}
void add(int x,int y) {
ver[++tot]=y;
next[tot]=head[x];
head[x]=tot;
}
void dij(int u) {
for(int i=1; i<=n; i++) {
d[i]=inf;
v[i]=0;
}
d[u]=0;
while(q.size()) q.pop();
q.push(make_pair(0,u));
while(q.size()) {
int x=q.top().second;
q.pop();
if(v[x]) continue;
v[x]=1;
if(gg[x]&&green[f[x]].vis) {
for(int j=1; j<=n; j++) {
d[j]=min(d[j],d[x]+green[f[x]].jl[j]);
}
continue;
}
for(int i=head[x]; i; i=next[i]) {
if(d[ver[i]]>d[x]+1) {
d[ver[i]]=d[x]+1;
q.push(make_pair(-d[ver[i]],ver[i]));
}
}
}
}
int main() {
// freopen("oasis.in","r",stdin);
// freopen("oasis.out","w",stdout);
n=read(),m=read(),k=read();
for(int i=1; i<=k; i++) {
green[i].b=read();
gg[green[i].b]=1;
f[green[i].b]=i;
}
for(int i=1; i<=m; i++) {
int x=read(),y=read();
add(x,y);
add(y,x);
}
for(int i=1; i<=k; i++) {
dij(green[i].b);
for(int j=1; j<=n; j++) {
city[j].insert(d[j]);
green[i].jl[j]=d[j];
}
green[i].vis=1;
}
for(int i=1; i<n; i++) {
printf("%d ",*(--city[i].end()));
}
cout<<(*(--city[n].end()));
}
上一篇: Session会话_实现购物车功能
下一篇: Java实现MD5加密