POJ 1364 King 题解与分析

King
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 9040 Accepted: 3395

Description

对题目中给定的si,ni,ki,和一个给定的序列S[1....N],如果格式为(si,ni,gt,ki),意思就是新增一约束条件S[si]+S[si+1]+...S[si+ni]>ki,如果格式为(si,ni,lt,ki),意思就是新增一约束条件S[si]+S[si+1]+...S[si+ni]<ki,判断所给的约束条件有符合要求的序列S[1....N],有解就输出lamentable kingdom,无解就输出successful conspiracy。

Sample Input

4 2
1 2 gt 0
2 2 lt 2
1 2
1 0 gt 0
1 0 lt 0
0

Sample Output

lamentable kingdom
successful conspiracy
 
【分析】:
    设a[i]表示∑a[k],k∈[1,i]且k∈Z*,那么对于约束条件S[si]+S[si+1]+...S[si+ni]>ki可转变为a[si+ni]-a[si-1]>ki(因为要包含[si,si+ni],因此根据a数组的定义,下限为a[si-1]),而差分约束系统只能解形如Ax≤b的不等式组,因此转化给出的约束条件S[si]+S[si+1]+...S[si+ni]>ki→a[si+ni]-a[si-1]>ki→a[si-1]-a[si+ni]<=-ki-1,另一个同理:a[si+ni]-a[si-1]<=ki-1,然后跑一边差分约束即可<这里用的是spfa+邻接表存储>
【基础理论】:差分约束系统,用最短路算法实现
【代码】:
/*
   Problem: 1364  User: csyzcyj 
   Memory: 192K  Time: 0MS 
   Language: C++  Result: Accepted 
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
#define MAX 201
#define MAXP 1001
#define IMAX 21474836
struct EDGE{int to,next,v;};
EDGE a[MAXP];
int N,M,tot=0,last[MAX],dist[MAX],tot1[MAX];     
bool vis[MAX];
void add(int x,int y,int value)
{
      a[++tot].to=y;
      a[tot].next=last[x];
      a[tot].v=value;
      last[x]=tot;
}
bool check()
{
      queue<int> Q;
      memset(dist,0,sizeof(dist));
      for(int i=0;i<=N;i++)
            Q.push(i);
      while(!Q.empty())
      {
            int now=Q.front();
            Q.pop();
            vis[now]=false;
            for(int i=last[now];i!=-1;i=a[i].next)
            {
                  int now_to=a[i].to;
                  if(dist[now]+a[i].v<dist[now_to])
                  {
                        dist[now_to]=dist[now]+a[i].v;
                        if(!vis[now_to])
                        {
                              vis[now_to]=true;
                              tot1[now_to]++;
                              if(tot1[now_to]>N)   return false;   
                              Q.push(now_to);
                        }
                  } 
            }
      }
      return true;
}
int main()
{
      //freopen("input.in","r",stdin);
	  //freopen("output.out","w",stdout); 
	  while(scanf("%d%d",&N,&M)!=EOF && N!=0 && M!=0)
	  {
              memset(last,-1,sizeof(last));      
              memset(vis,true,sizeof(vis));
              memset(tot1,0,sizeof(tot1));
              memset(a,0,sizeof(a));
              tot=0;
              for(int i=1;i<=M;i++)
              {
                    int A,B,C;
                    char ch1,ch2;
                    scanf("%d%d",&A,&B);
                    scanf("%c%c%c",&ch1,&ch1,&ch2);
                    scanf("%d",&C);
                    if(ch1=='g')
                          add(A+B,A-1,-C-1);
                    if(ch1=='l')
                          add(A-1,A+B,C-1);
              }
              if(check())   printf("lamentable kingdom\n");
              else    printf("successful conspiracy\n");
      }
	  //system("pause");
      return 0;
}

 

转载注明出处:http://blog.csdn.net/u011400953

 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值