【codevs 2273】扬帆远洋大战牧师妹酱

spfa+slf

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
using namespace std;
int T,R,P,S;
const int maxn=200000+5;
deque<int>q;
bool used[maxn];
int first[maxn],next[maxn<<1],tot=0,dis[maxn];
struct edge
{
    int f,t,v;
}es[maxn<<1];
void build(int f,int t,int v)
{
    es[++tot]=(edge){f,t,v};
    next[tot]=first[f];
    first[f]=tot;
}

void read(int &a)
{
    a=0;
    int h=1;
    char c=getchar();
    while(c<'0'||c>'9')
    {
        if(c=='-')
            h=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9')
    {
        a*=10;
        a+=c-'0';
        c=getchar();
    }
    a*=h;
}

void spfa()
{
    while(!q.empty())
    {
        int x=q.front();
        q.pop_front();
        used[x]=0;
        for(int i=first[x];i;i=next[i])
        {
            int v=es[i].v,t=es[i].t;
            if(dis[t]>dis[x]+v)
            {
                dis[t]=dis[x]+v;
                if(!used[t])
                {
                    if(q.empty())  q.push_front(t);
                    else if(dis[t]>dis[q.front()] ) q.push_back(t);
                    else q.push_front(t);
                    used[t]=1;
                }
            }
        }
    }
}
int main()
{
    memset(used,0,sizeof(used));
    memset(dis,0x3f3f3f3f,sizeof(dis));
    int x,y,z;
    read(T),read(R),read(P),read(S);
    for(int i=1;i<=R;i++)
    {
        read(x),read(y),read(z);
        build(x,y,z);
        build(y,x,z);
    }
    for(int i=1;i<=P;i++)
    {
        read(x),read(y),read(z);
        build(x,y,z);
    }
    q.push_front(S);
    used[S]=1;
    dis[S]=0;
    spfa();
    int cnt=0;
    for(int i=1;i<=T;i++)
    {
        if(dis[i]==0x3f3f3f3f) printf("Need Teleport To Repair\n"),cnt++;
        else printf("%d\n",dis[i]);
    }
    printf("%d\n",cnt);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值