【SCOI2011】糖果 差分约束

bzoj2330

差分约束裸题,我的代码总是T一个点,和黄学长的比了好多遍还看不出啥区别……【并不是那个一个链的测试点】

没有A掉这个题却恬不知耻的发代码的人的代码:

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
const int SIZE=100010;
int head[SIZE],nxt[SIZE*4],tot=0;
typedef long long LL;

int dist[SIZE];

struct edge{
    int t,d;
}l[SIZE*4];

inline void build(int f,int t,int d)
{
    l[++tot]=(edge){t,d};
    nxt[tot]=head[f];
    head[f]=tot; 
} 
int n,m;

int q[SIZE];
bool use[SIZE],t[SIZE];
inline bool spfa(int s)
{
    dist[s]=1;
    use[s]=1;
    int l=0,r=1;
    q[l]=s;
    while(l!=r)
    {
        int f=q[l]; l++; if(l==SIZE) l=0;
        use[f]=0;
        for(int i=head[f];i;i=nxt[i])
        {
            int v=::l[i].t;
            if(dist[v]<dist[f]+::l[i].d) 
            {
                dist[v]=dist[f]+::l[i].d;
                if(!use[v])
                {
                    use[v]=1;
                    q[r++]=v; if(r==SIZE) r=0;
                    t[v]++;
                    if(t[v]>n) return false;
                }
            }
        }
    }
    return true;
}
inline void scan(int &n)
{
    n=0;
    char a=getchar();
    while(a>'9'||a<'0') a=getchar();
    while(a<='9'&&a>='0') n*=10,n+=a-'0',a=getchar();
}

int main()
{
    scan(n);    scan(m);
    //scanf("%d%d",&n,&m);
    while(m--)
    {
        int x,a,b;
        scan(x);    scan(a);    scan(b);
        //scanf("%d%d%d",&x,&a,&b);
        switch(x)
        {
            case 1: build(a,b,0);build(b,a,0);  break;  //a==b
            case 2: if(a==b) {printf("-1");return 0;}
                build(a,b,1);                   break;                  //a<b b-a>0 b-a>=1

            case 3: build(b,a,0);               break;      //a>=b a-b>=0
            case 4: if(a==b) {printf("-1");return 0;}
                build(b,a,1);                   break;                  //a>b a-b>0 a-b>=1
            case 5: build(a,b,0);               break;                  //a<=b b-a>=0 
        }
    }

    for(int i=n;i>=1;i--)
    {
        build(0,i,0);
    }

    if(!spfa(0))
    {
        printf("-1");
        return 0;
    }
    LL ans=0; 
    for(int i=1;i<=n;i++) //printf("%lld ",dist[i]),
        ans+=dist[i];
    printf("%lld",ans);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值