【图论最短路】【CQBZOJ 1634】【图论专项赛】外星人入侵

问题 F(1634): 【图论专项赛】外星人入侵
时间限制: 10 Sec 内存限制: 64 MB

题目描述
外星人入侵地球。可怕的吃人外星人正在全国各地依次序建立它们的基地。
全国共有N(1≤ N ≤10,000)座城市,城市编号1~N。城市之间有M(0≤ M ≤100,000)条双向道路相连。外星人计划建立A(0≤A≤N)个基地。
你只有在距离当前所有外星人基地至少K(1≤K≤100)单位长度的城市才能得到安全。
所以你必须赶快写一个程序决定走到哪里去。

输入
第1行:4个整数N, M, A, K
接下来M行,每行3个整数 T1,T2(1T1<T2N) D(1D100) ,表示城市T1与T2之间有一条长度为D的道路。两个城市之间最多有一条直连道路。
接下来A行,每行1个整数Bi(1≤Bi≤N),表示外星人依次序建的第i个基地所在的城市编号。

输出
共A行,第i行1个整数,表示当外星人建好第i个基地后,距离当前所有基地B1,B2,…,Bi至少K长度的城市的数量。

样例输入
7 6 3 3
1 2 1
1 3 1
2 5 1
3 6 1
1 4 1
4 7 2
2
1
4

样例输出
2
1
0

对于每一次出现的外星人基地,都做一次SPFA,更新一下每座城市距离基地的最小距离就行了,不过因为只有距离小于等于K才有危险,故但距离超过K的时候就终止,不然会TLE

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;

#define MAXN 100000
#define MAXM 100000
#define INF 0x3f3f3f3f
typedef long long int LL;

int getint()
{
    int rn=0;
    char c=getchar();

    while(c<'0'||'9'<c)
        c=getchar();

    while('0'<=c&&c<='9')
    {
        rn=rn*10+c-'0';
        c=getchar();
    }

    return rn;
}

struct node
{
    int v,w;
    node *next;
}*adj[MAXN+10],Edges[MAXM*2+10],*New=Edges;

void addedges(int u,int v,int w)
{
    node *p=++New;
    p->v=v;
    p->w=w;
    p->next=adj[u];
    adj[u]=p;

    p=++New;
    p->v=u;
    p->w=w;
    p->next=adj[v];
    adj[v]=p;
}

int Al[MAXN+10];
int N,M,A,K;
int dist[MAXN+10];
bool vis[MAXN+10];
queue<int>que;
void Spfa(int S)
{
    while(!que.empty())que.pop();

    dist[S]=0;
    que.push(S);

    int now;
    while(!que.empty())
    {
        now=que.front();que.pop();
        vis[now]=0;

        for(node *p=adj[now];p!=NULL;p=p->next)
        {
            if(dist[now]+p->w>=K)continue;

            if(dist[p->v]>dist[now]+p->w)
            {
                dist[p->v]=dist[now]+p->w;
                if(!vis[p->v])
                {
                    que.push(p->v);
                    vis[p->v]=1;
                }
            }
        }
    }
}

int main()
{
    scanf("%d%d%d%d",&N,&M,&A,&K);
    int i,j;
    int a,b,c;

    for(i=1;i<=M;++i)
    {
        a=getint(),b=getint(),c=getint();
        addedges(a,b,c);
    }

    memset(dist,0x3f,sizeof(dist));
    memset(vis,0,sizeof(vis));

    for(i=1;i<=A;++i)
    {
        Spfa(getint());

        int ans=0;
        for(j=1;j<=N;++j)
            if(dist[j]<K)++ans;
        printf("%d\n",N-ans);
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值