其他分享
首页 > 其他分享> > 逃离洞穴 题解

逃离洞穴 题解

作者:互联网

题意

跑两边spfa的水题,注意判断有人才取最大值

代码

#include<bits/stdc++.h>
using namespace std;
inline int read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return x*f;
}
const int N=510,M=100010;
int head[N],ver[M],edge[M],Next[M],d1[N],d2[N],d[N],op[N];
int n,m,k,T,tot,maxn;
queue<int> q;
bool v[N];
long long ans;
void add(int x,int y,int z){
    ver[++tot]=y;edge[tot]=z;Next[tot]=head[x],head[x]=tot;
}
void spfa(){
    memset(d1,0x3f,sizeof(d1));
    d1[1]=0,v[1]=1;
    q.push(1);
    while(q.size()){
        int x=q.front();q.pop();
        v[x]=0;
        for(int i=head[x];i;i=Next[i]){
            int y=ver[i],z=edge[i];
            if(d1[y]>d1[x]+z){
                d1[y]=d1[x]+z;
                if(!v[y]) q.push(y),v[y]=1;
            }
        }
    }
    memset(d2,0x3f,sizeof(d2));
    memset(v,0,sizeof(v));
    d2[n]=0,v[n]=1;
    q.push(n);
    while(q.size()){
        int x=q.front();q.pop();
        v[x]=0;
        for(int i=head[x];i;i=Next[i]){
            int y=ver[i],z=edge[i];
            if(d2[y]>d2[x]+z){
                d2[y]=d2[x]+z;
                if(!v[y]) q.push(y),v[y]=1;
            }
        }
    }
    for(int i=1;i<=n;++i){
        d[i]=min(d1[i],d2[i]);
    }
}
int main(){
    freopen("escape.in","r",stdin);
    freopen("escape.out","w",stdout);
    n=read();m=read();T=read();
    for(int i=1;i<=m;++i){
        int x,y,z;
        x=read();y=read();z=read();
        add(x,y,z);
        add(y,x,z);
    }
    spfa();
    k=read();
    int tmp;
    for(int i=1;i<=k;++i){
        tmp=read();
        op[tmp]++;
    }
    for(int i=1;i<=n;++i){
        if(d[i]<=T)  ans+=op[i];
        if(op[i]) maxn=max(maxn,min(d1[i],d2[i]));
    }
    printf("%lld\n%d",ans,maxn);
    return 0;
}
/*
4 4 3
1 2 5
2 4 3
1 3 4
3 4 6
4
1 2 3 4
*/

标签:ch,int,题解,洞穴,tot,逃离,while,d2,d1
来源: https://www.cnblogs.com/donkey2603089141/p/11416429.html