HDU 2066 一个人的旅行(spfa)

 

假设 0 为虚拟起点,SPFA 求解即可 

const int N=1e4+5;
 
    int n,m,t;
    int i,j,k;
    int head[N],all=0;
    int d[N]; //点 i 到起点的距离
    int cnt[N]; //点 i 的入队次数
    bool vis[N];

struct node
{
    int to,next;
    int w;
}G[N];
void add(int u,int v,int w)
{
    G[all].w=w;
    G[all].to=v;
    G[all].next=head[u];
    head[u]=all++;
}
void SPFA(int s)
{
    ms(d,inf); ms(vis,0);
    d[s]=0; vis[s]=1;
    queue<int> q;
    q.push(s);
    while(!q.empty()){
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=G[i].next){
            int v=G[i].to;
            int w=G[i].w;
            if(d[v]>d[u]+w){
                d[v]=d[u]+w;
                if(!vis[v]){
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
}
int main()
{
    //IOS;
    while(~sddd(n,m,t)){
        all=0;
        ms(head,-1);
        int u,v,w;
        for(i=1;i<=n;i++){
            sddd(u,v,w);
            add(u,v,w);
            add(v,u,w);
        }
        for(i=1;i<=m;i++){
            sd(u);
            add(0,u,0);
            add(u,0,0);
        }
        SPFA(0);
        int ans=inf;
        for(i=1;i<=t;i++){
            sd(v);
            ans=min(ans,d[v]);
        }
        pd(ans);
    }
    //PAUSE;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/C_Dreamy/article/details/107861348