BZOJ 4093: [Usaco2013 Dec]Vacation Planning

35 篇文章 0 订阅
12 篇文章 0 订阅

不难发现每条边都有交通中心 所以对每个交通中心跑一次spfa就好

求答案的时候就枚举是先到哪个交通中心,取个最优值。

#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define me(a,x) memset(a,x,sizeof a)
using namespace std;
typedef long long LL;
const int N=20010,K=205;
const int inf=1000000007;
inline LL read()
{
    LL x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}
struct node{int y,c,next;}a[N]; int first[N],len;
void ins(int x,int y,int c)
{
    a[++len].y=y,a[len].c=c,a[len].next=first[x],first[x]=len;
}
queue<int>q;
int f[K][N]; bool v[N];
int p[N];
void spfa(int st)
{
    q.push(st); me(v,0);
    v[st]=1,f[p[st]][st]=0;
    while(!q.empty())
    {
        int x=q.front();
        for(int k=first[x];k;k=a[k].next)
        {
            int y=a[k].y;
            if(f[p[st]][y]>f[p[st]][x]+a[k].c)
            {
                f[p[st]][y]=f[p[st]][x]+a[k].c;
                if(!v[y]){v[y]=1; q.push(y);}
            }
        }
        q.pop();
        v[x]=0;
    }
}
int main()
{
    int k=read(),m=read(),n=read(),q=read();
    int x,y,c,i;
    for(i=1;i<=m;i++)
    {
        x=read(),y=read(),c=read();
        ins(x,y,c);
    }
    me(f,63);
    for(i=1;i<=n;i++)
    {
        x=read();
        p[x]=i; spfa(x);
    }
    LL ans=0; int s=0;
    while(q--)
    {
        x=read(),y=read();
        LL g=inf;
        if(!p[x])
          for(i=first[x];i;i=a[i].next)
            g=min(g,(LL)a[i].c+f[p[a[i].y]][y]);
        else g=f[p[x]][y];
        if(g<inf)s++,ans+=g;
    }
    printf("%d\n%lld\n",s,ans);
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值