POJ 1364 King【差分约束+SPFA/Bellman-Ford】


King

Time Limit: 1000MS

 

Memory Limit: 10000K

Total Submissions: 11846

 

Accepted: 4325

Description

Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen prayed: ``If my child was a son and if only he was a sound king.'' After nine months her child was born, and indeed, she gave birth to a nice son. 
Unfortunately, as it used to happen in royal families, the son was a little retarded. After many years of study he was able just to add integer numbers and to compare whether the result is greater or less than a given integer number. In addition, the numbers had to be written in a sequence and he was able to sum just continuous subsequences of the sequence. 

The old king was very unhappy of his son. But he was ready to make everything to enable his son to govern the kingdom after his death. With regards to his son's skills he decided that every problem the king had to decide about had to be presented in a form of a finite sequence of integer numbers and the decision about it would be done by stating an integer constraint (i.e. an upper or lower limit) for the sum of that sequence. In this way there was at least some hope that his son would be able to make some decisions. 

After the old king died, the young king began to reign. But very soon, a lot of people became very unsatisfied with his decisions and decided to dethrone him. They tried to do it by proving that his decisions were wrong. 

Therefore some conspirators presented to the young king a set of problems that he had to decide about. The set of problems was in the form of subsequences Si = {aSi, aSi+1, ..., aSi+ni} of a sequence S = {a1, a2, ..., an}. The king thought a minute and then decided, i.e. he set for the sum aSi + aSi+1 + ... + aSi+ni of each subsequence Si an integer constraint ki (i.e. aSi + aSi+1 + ... + aSi+ni < ki or aSi + aSi+1 + ... + aSi+ni > ki resp.) and declared these constraints as his decisions. 

After a while he realized that some of his decisions were wrong. He could not revoke the declared constraints but trying to save himself he decided to fake the sequence that he was given. He ordered to his advisors to find such a sequence S that would satisfy the constraints he set. Help the advisors of the king and write a program that decides whether such a sequence exists or not. 

Input

The input consists of blocks of lines. Each block except the last corresponds to one set of problems and king's decisions about them. In the first line of the block there are integers n, and m where 0 < n <= 100 is length of the sequence S and 0 < m <= 100 is the number of subsequences Si. Next m lines contain particular decisions coded in the form of quadruples si, ni, oi, ki, where oi represents operator > (coded as gt) or operator < (coded as lt) respectively. The symbols si, ni and ki have the meaning described above. The last block consists of just one line containing 0.

Output

The output contains the lines corresponding to the blocks in the input. A line contains text successful conspiracy when such a sequence does not exist. Otherwise it contains text lamentable kingdom. There is no line in the output corresponding to the last ``null'' block of the input.

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

 

题目大意:本题的一是一个国王通过一个序列来做决定,他会有这序列的一段连续子序列,并知道这个子序列的和是大于或者小于某个整数。这里一共有两个信息:


给你一堆这样的信息,求是否有解。

思路:

1、根据信息,看到了不等式的条件约束,我们不难想到差分约束系统转换为最短路求解。我们还知道,差分约束系统转化为最短路问题的前提是有这样的一个式子成立我们才能够构建边和图:t1-t2<=k

2、所以我们就尽量将已知条件转化为目标形式。我们这里不妨设b=S1+S2+S3+.....Sx+y,设a=S1+S2+S3+....+SX-1,那么我们对已知条件就可以转化为:

b-a>k

b-a<k

这个时候我们还差一个等号,但是因为题中保证了数据都是整数,所以我们再进行一次小小的变化就得到了目标式子:

b-a>=k+1

b-a<=k-1

最后规划式子都变成小于等于,最终得到推导式子:

a-b<=-k-1;

b-a<=k-1

3、既然我们得到了建边建图的条件,然后我们就剩下判断差分约束系统是否有解的问题了,有解,其实就是有向图不存在负权回路,没有解,其实就是有向图存在负权回路,这个问题转化到了最短路上边,我们也就非常好处理了。

4、因为图不一定是连通的,所以确定起点的问题比较尴尬,如果随便控制一个变量是起点,那么会有负权回路判断不到的地方,所以我们这里可以创立一个超级源点,使得一个图连通起来即可。

如果您选择SPFA算法来判断是否存在负权回路:

5、创立超级源点其实相当于第一次就直接将所有点都入队中,就相当于所有点全部连通起来了,这里注意,因为第一次所有点都入队列中了,所以判断是否存在负权回路的条件是这样的:如果某个点出队次数大于n+1,那么就存在负权回路。

如果您选择Bellman算法来判断是否存在负权回路:

6、创立超级源点,然后进行n次遍历,之后再进行一次遍历,如果这次遍历还可以松弛,那么说明存在负权回路。


这里提供两种做法的AC代码,我归到一份代码里边了,然后SPFA算法没有建立超级源点,直接将所有点入队了,Bellman算法建立了超级源点,读者喜欢哪一种方法,就可以对应参考一下。


AC代码:


#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int head[100000];
struct EdgeNode
{
    int to;
    int w;
    int next;
} e[100000];
int dis[100000];
int vis[100000];
int out[100000];
int cont;
int n,m;
void add(int from,int to,int w)
{
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont++;
}
int SPFA()
{
    queue<int >s;
    memset(dis,0,sizeof(dis));
    memset(out,0,sizeof(out));
    for(int i=0;i<=n;i++)
    {
        s.push(i);
    }
    while(!s.empty())
    {
        int u=s.front();
        s.pop();
        vis[u]=0;
        out[u]++;
        if(out[u]>n+1)return 0;
        for(int k=head[u];k!=-1;k=e[k].next)
        {
            int v=e[k].to;int w=e[k].w;
            if(dis[v]>dis[u]+w)
            {
                dis[v]=dis[u]+w;
                if(vis[v]==0)
                {
                    vis[v]=1;
                    s.push(v);
                }
            }
        }
    }
    return 1;
}
int Bellman()
{
    for(int i=0; i<=n; i++)add(n+1,i,0);
    for(int i=0; i<=n; i++)dis[i]=0x3f3f3f3f;
    dis[n+1]=0;
    for(int ci=0; ci<n; ci++)
    {
        for(int i=0; i<=n; i++)
        {
            for(int k=head[i]; k!=-1; k=e[k].next)
            {
                int to=e[k].to;
                int w=e[k].w;
                if(dis[to]>dis[i]+w)
                {
                    dis[to]=dis[i]+w;
                }
            }
        }
    }
    for(int i=0; i<=n; i++)
    {
        for(int k=head[i]; k!=-1; k=e[k].next)
        {
            int to=e[k].to;
            int w=e[k].w;
            if(dis[to]>dis[i]+w)
            {
                return 0;
                dis[to]=dis[i]+w;
            }
        }
    }
    return 1;
}
int main()
{
    while(~scanf("%d",&n))
    {
        if(n==0)break;
        cont=0;
        memset(head,-1,sizeof(head));
        scanf("%d",&m);
        for(int i=0; i<m; i++)
        {
            int x,y,k;
            char op[5];
            scanf("%d%d%s%d",&x,&y,&op,&k);
            int b=x+y;
            int a=x-1;
            if(op[0]=='g')add(b,a,-k-1);
            else add(a,b,k-1);
        }
        int tt=SPFA();
        //int tt=Bellman();
        if(tt==1)puts("lamentable kingdom");
        else puts("successful conspiracy");
    }
}







在使用Python来安装geopandas包时,由于geopandas依赖于几个其他的Python库(如GDAL, Fiona, Pyproj, Shapely等),因此安装过程可能需要一些额外的步骤。以下是一个基本的安装指南,适用于大多数用户: 使用pip安装 确保Python和pip已安装: 首先,确保你的计算机上已安装了Python和pip。pip是Python的包管理工具,用于安装和管理Python包。 安装依赖库: 由于geopandas依赖于GDAL, Fiona, Pyproj, Shapely等库,你可能需要先安装这些库。通常,你可以通过pip直接安装这些库,但有时候可能需要从其他源下载预编译的二进制包(wheel文件),特别是GDAL和Fiona,因为它们可能包含一些系统级的依赖。 bash pip install GDAL Fiona Pyproj Shapely 注意:在某些系统上,直接使用pip安装GDAL和Fiona可能会遇到问题,因为它们需要编译一些C/C++代码。如果遇到问题,你可以考虑使用conda(一个Python包、依赖和环境管理器)来安装这些库,或者从Unofficial Windows Binaries for Python Extension Packages这样的网站下载预编译的wheel文件。 安装geopandas: 在安装了所有依赖库之后,你可以使用pip来安装geopandas。 bash pip install geopandas 使用conda安装 如果你正在使用conda作为你的Python包管理器,那么安装geopandas和它的依赖可能会更简单一些。 创建一个新的conda环境(可选,但推荐): bash conda create -n geoenv python=3.x anaconda conda activate geoenv 其中3.x是你希望使用的Python版本。 安装geopandas: 使用conda-forge频道来安装geopandas,因为它提供了许多地理空间相关的包。 bash conda install -c conda-forge geopandas 这条命令会自动安装geopandas及其所有依赖。 注意事项 如果你在安装过程中遇到任何问题,比如编译错误或依赖问题,请检查你的Python版本和pip/conda的版本是否是最新的,或者尝试在不同的环境中安装。 某些库(如GDAL)可能需要额外的系统级依赖,如地理空间库(如PROJ和GEOS)。这些依赖可能需要单独安装,具体取决于你的操作系统。 如果你在Windows上遇到问题,并且pip安装失败,尝试从Unofficial Windows Binaries for Python Extension Packages网站下载相应的wheel文件,并使用pip进行安装。 脚本示例 虽然你的问题主要是关于如何安装geopandas,但如果你想要一个Python脚本来重命名文件夹下的文件,在原始名字前面加上字符串"geopandas",以下是一个简单的示例: python import os # 指定文件夹路径 folder_path = 'path/to/your/folder' # 遍历文件夹中的文件 for filename in os.listdir(folder_path): # 构造原始文件路径 old_file_path = os.path.join(folder_path, filename) # 构造新文件名 new_filename = 'geopandas_' + filename # 构造新文件路径 new_file_path = os.path.join(folder_path, new_filename) # 重命名文件 os.rename(old_file_path, new_file_path) print(f'Renamed "{filename}" to "{new_filename}"') 请确保将'path/to/your/folder'替换为你想要重命名文件的实际文件夹路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值