CSUOJ 2005 nearest maintenance point


Description

A county consists of n cities (labeled 1, 2, …, n) connected by some bidirectional roads. Each road connects a pair of distinct cities. A robot company has built maintenance points in some cities. Now, they need your help to write a program to query the nearest maintenance point to a specific city.


Input

There will be at most 200 test cases. Each case begins with four integers n, m, s, q(2 ≤ n ≤ 104, 1 ≤ m ≤ 5 × 104, 1 ≤ s ≤ min{n, 1000},1 ≤ q ≤ min{n, 1000}), the number of cities, the number of roads, the number of cities which have maintenance points and the number of queries. Then m lines contain the descriptions of roads. Each of them contains three integers ui, vi, wi(1 ≤ ui, vi ≤ n, ui ≠ vi, 1 ≤ wi ≤ 1000), denoting there is a road of length wi which connects city ui and vi. The next line contains s integers, denoting the cities which have maintenance points. Then q lines contain the descriptions of queries. Each of them contains one integer denoting a specific city. The size of the whole input file does not exceed 6MB.


Output

For each query, print the nearest city which has a maintenance point. If there are multiple such cities, print all of them in increasing order separated by a single space. It is guaranteed that there will be at least one such city.


分析:多源最短路 + 路径信息维护 + Bitset优化。

            再加向大神学习:http://blog.csdn.net/u013534123/article/details/77927609

            确实学到了 一波。。。

#include<bitset>
#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<queue>
const int maxn = 20000;
const int maxe = 100000;
const int inf = 1e9;
typedef long long ll;
using namespace std;
struct Heap
{
    int d,u;
    bool operator < (const Heap &H) const
    {
        return d > H.d;
    }
};
struct Edge
{
    int fr,to,dist,nex;
} edges[maxe+5];
int n,m,tot;
int d[maxn+5],head[maxn+5],done[maxn+5],ans[maxn+5],key[maxn+5];
bitset<1005>bit[maxn+5];
priority_queue<Heap>Q;
void Init()
{
    tot = 0;
    for(int i=0; i<=n; i++) head[i]=-1,bit[i].reset();
}
void Addedge(int fr,int to,int dist)
{
    edges[++tot]=((Edge)
    {
        fr,to,dist,head[fr]
    });
    head[fr] = tot;
}
void dijkstra()
{
    memset(done,0,sizeof(done));
    while(!Q.empty())
    {
        Heap x = Q.top();
        Q.pop();
        int u = x.u;
        if(done[u]) continue;
        done[u] = 1;
        for(int i=head[u]; i!=-1; i=edges[i].nex)
        {
            Edge &e = edges[i];
            if(d[e.to] >= d[u]+e.dist)
            {
                if(d[e.to]>d[u]+e.dist)
                {
                    d[e.to] = d[u] + e.dist;
                    bit[e.to] = bit[u];
                    Q.push((Heap)
                    {
                        d[e.to],e.to
                    });
                }
                else
                {
                    bit[e.to]|=bit[u];
                }
            }
        }
    }
}
int main()
{
    int x,y,w,K,q;
    while(~scanf("%d %d %d %d",&n,&m,&K,&q))
    {
        Init();
        for(int i=1; i<=m; i++)
        {
            scanf("%d %d %d",&x,&y,&w);
            Addedge(x,y,w), Addedge(y,x,w);
        }
        for(int i=0; i<=n; i++) d[i] = inf;
        for(int i=1; i<=K; i++)
        {
            scanf("%d",&key[i]);
            d[key[i]] = 0;
            Q.push((Heap)
            {
                0,key[i]
            });
        }
        sort(key+1,key+K+1);
        for(int i=1;i<=K;i++) bit[key[i]][i] = 1;
        dijkstra();
        for(int i=1; i<=q; i++)
        {
            scanf("%d",&x);
            int l = 0;
            for(int j=1;j<=K;j++)
            {
                if(bit[x][j]) ans[++l] = key[j];
            }
            for(int j=1;j<l;j++) printf("%d ",ans[j]);
            printf("%d\n",ans[l]);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值