zoukankan      html  css  js  c++  java
  • CDOJ Find the Fastest Server (SPFA)

    原题地址:http://acm.uestc.edu.cn/#/problem/show/702

    题意

    题解

    #include<bits/stdc++.h>
    
    #define clr(x) memset(x,0,sizeof(x))
    
    using namespace std;
    typedef long long LL;
    
    struct edge{int to,cost;};
    
    const int maxn=1e5;
    const int inf=1e9;
    
    int S[maxn+5];
    int dist[maxn+5];
    int query[maxn+5];
    int vis[maxn+5];
    
    queue <int> Q;
    vector <edge> G[maxn+5];
    
    int main(void)
    {
        #ifdef ex
        freopen ("in.txt","r",stdin);
        //freopen ("out.txt","w",stdout);
        #endif
    
        int T;
        int n,m,q,k;
        int u,v,w;
    
        scanf("%d",&T);
        while (T--)
        {
            fill(dist,dist+sizeof(dist)/sizeof(int),inf);
            clr(vis);
            for (int i=1;i<=maxn;++i) G[i].clear();
    
            scanf("%d%d",&n,&m);
            for (int i=1;i<=m;++i)
            {
                scanf("%d%d%d",&u,&v,&w);
                G[u].push_back((edge){v,w});
                G[v].push_back((edge){u,w});
            }
    
            scanf("%d",&k);
            for (int i=1;i<=k;++i)
            {
                scanf("%d",&S[i]);
            }
    
            scanf("%d",&q);
            for (int i=1;i<=q;++i)
            {
                scanf("%d",&query[i]);
            }
    
            for (int i=1;i<=k;++i)
            {
                Q.push(S[i]);
                dist[S[i]]=0;
                vis[S[i]]=1;
            }
    
            while (!Q.empty())
            {
                int x=Q.front();
                Q.pop();
                vis[x]=0;
    
                for (int i=0;i<G[x].size();++i)
                {
                    edge& e=G[x][i];
                    if (dist[x]<inf && dist[e.to]>e.cost+dist[x])
                    {
                        dist[e.to]=e.cost+dist[x];
                        if (!vis[e.to])
                        {
                            Q.push(e.to);
                            vis[e.to]=1;
                        }
                    }
                }
            }
    
            for (int i=1;i<=q;++i)
            {
                if (dist[query[i]]<inf)
                    printf("%d
    ",dist[query[i]]);
                else
                    printf("-1
    ");
                //printf("%d
    ",dist[i]);
            }
    
            printf("
    ");
        }
    }
  • 相关阅读:
    emacs jedi
    opencv 基本demo
    emacs列编辑
    observable operator example
    angular keydown 例子
    回调和匿名函数
    gin cors
    angular rxjs
    python dbus note
    视频截图
  • 原文地址:https://www.cnblogs.com/123-123/p/5585939.html
Copyright © 2011-2022 走看看