la4255 Guess 差分约束

    这道题目我的做法是差分约束,具体地,设s[i]表示1..i的一个前缀和,那么对于输入的a[x][y],

    如果a[x][y]='+',说明s[y]比s[x-1]要大,s[y]>s[x-1] s[y]-s[x-1]>=1,所以从x-1到y连一条长度为1的边

    如果a[x][y]='-',说明s[y]比s[x-1]要大,s[y]<s[x-1] s[x-1]-s[y]>=1,所以从y到x-1连一条长度为1的边

    如果a[x][y]='0',说明s[y]和s[x-1]一样大,s[y]=s[x-1] s[y]-s[x-1]=0,所以从x-1到y连一条长度为0的边。虽然是等号而不是不等号,但是如果s[y]<>s[x-1],则会出现正权回路,和题设矛盾。

    这样建完图后,跑一遍最长路,求出s后,原始序列就显而易见了。

下附AC代码:

const
  mo=100;
var
  a:array[-1..15,-1..15] of longint;
  d:array[-1..100] of longint;
  h:array[0..100] of longint;
  bo:array[-1..100] of boolean;
  s:string;
  n,tt,i,j,k,head,tail,u:longint;
begin
  readln(tt);
  while tt>0 do
    begin
      tt:=tt-1;
      readln(n);
      readln(s);
      k:=0;
      fillchar(a,sizeof(a),200);
      for i:=1 to n do
        for j:=i to n do
          begin
            k:=k+1;
            case s[k] of
              '+':a[i-1,j]:=1;
              '-':a[j,i-1]:=1;
              '0':a[i-1,j]:=0;
            end;
          end;
      fillchar(d,sizeof(d),200); d[-1]:=0;
      fillchar(bo,sizeof(bo),true);
      for i:=0 to n do
        a[-1,i]:=0;
      head:=0; tail:=1; h[1]:=-1;
      while head<tail do
        begin
          head:=head mod mo+1;
          u:=h[head]; bo[u]:=true;
          for i:=0 to n do
            if a[u,i]>-1000000 then
              if d[u]+a[u,i]>d[i] then
                begin
                  d[i]:=d[u]+a[u,i];
                  if bo[i] then
                    begin
                      tail:=tail mod mo+1;
                      h[tail]:=i; bo[i]:=false;
                    end;
                end;
        end;
      for i:=1 to n-1 do write(d[i]-d[i-1],' '); writeln(d[n]-d[n-1]);
    end;
end.


2015.2.11

by lych

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值