青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

差分約束系統詳解

 

差分約束系統

    在一個差分約束系統(system of difference constraints)中,線性規劃矩陣A的每一行包含一個1和一個-1A的其他所有元素都為0。因此,由Axb給出的約束條件是m個差分約束集合,其中包含n個未知量,對應的線性規劃矩陣Amn列。每個約束條件為如下形式的簡單線性不等式:xj-xibk。其中1i,jn1km

    例如,考慮這樣一個問題,尋找一個5維向量x=(xi)以滿足:

 

    這一問題等價于找出未知量xii=1,2,…,5,滿足下列8個差分約束條件:

x1-x20

x1-x5-1

x2-x51

x3-x15

x4-x14

x4-x3-1

x5-x3-3

x5-x4-3

    該問題的一個解為x=(-5,-3,0,-1,-4),另一個解y=(0,2,5,4,1),這2個解是有聯系的:y中的每個元素比x中相應的元素大5

 
引理:設x=(x1,x2,…,xn)是差分約束系統Axb的一個解,d為任意常數。則x+d=(x1+d,x2+d,…,xn+d)也是該系統Axb的一個解。

 

約束圖

   在一個差分約束系統Axb中,m X n的線性規劃矩陣A可被看做是n頂點,m條邊的圖的關聯矩陣。對于i=1,2,…,n,圖中的每一個頂點vi對應著n個未知量的一個xi。圖中的每個有向邊對應著關于兩個未知量的m個不等式中的一個。

   給定一個差分約束系統Axb,相應的約束圖是一個帶權有向圖G=(V,E),其中V={v0,v1,…,vn},而且E={ (vi,vj) : xj-xibk是一個約束}∪{ (v0,v1) , (v0,v2) , … , (v0,vn) }。引入附加頂點v0是為了保證其他每個頂點均從v0可達。因此,頂點集合V由對應于每個未知量xi的頂點vi和附加的頂點v0組成。邊的集合E由對應于每個差分約束條件的邊與對應于每個未知量xi的邊(v0,vi)構成。如果xj-xibk是一個差分約束,則邊(vi,vj)的權w(vi,vj)=bk(注意ij不能顛倒),v0出發的每條邊的權值均為0

  
定理:給定一差分約束系統Axb,設G=(V,E)為其相應的約束圖。如果G不包含負權回路,那么x=( d(v0,v1) , d(v0,v2) , … , d(v0,vn) )是此系統的一可行解,其中d(v0,vi)是約束圖中v0vi的最短路徑(i=1,2,…,n)。如果G包含負權回路,那么此系統不存在可行解。

 

差分約束問題的求解

   由上述定理可知,可以采用Bellman-Ford算法對差分約束問題求解。因為在約束圖中,從源點v0到其他所有頂點間均存在邊,因此約束圖中任何負權回路均從v0可達。如果Bellman-Ford算法返回TRUE,則最短路徑權給出了此系統的一個可行解;如果返回FALSE,則差分約束系統無可行解。

   關于n個未知量m個約束條件的一個差分約束系統產生出一個具有n+1個頂點和n+m條邊的約束圖。因此采用Bellman-Ford算法,可以再O((n+1)(n+m))=O(n^2+nm)時間內將系統解決。此外,可以用SPFA算法進行優化,復雜度為O(km),其中k 為常數。

http://acm.pku.edu.cn/JudgeOnline/problem?id=1364

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

Source


很典型的差分約束,關鍵在于怎么構圖。
這里我們設Sum(i) = A1 + A2 + … + Ai-1
那么輸入的si ni oi ki
就可以轉換成如下的約束式:Sum(si+ni+1) - Sum(si) oi ki

#include <iostream>
using namespace std;

const int MAXN = 120;
const int inf = 0x7f;
struct node{
    
int s,e,v;
}
edge[MAXN];
int n,m,d[MAXN];

bool bellman_ford(){
    
int i,j;
    
for(i=0;i<=n+1;i++) d[i]=inf;
    
for(d[0]=0,i=1;i<=n+1;i++)
        
for(j=0;j<=(n+m);j++)
            
if(d[edge[j].s]+edge[j].v<d[edge[j].e])
                d[edge[j].e]
=d[edge[j].s]+edge[j].v;
    
for(i=0;i<=(n+m);i++)
        
if(d[edge[i].s]+edge[i].v<d[edge[i].e])
            
return false;
    
return true;
}

int main(){
    
char oi[5];
    
int si,ni,k,i,j;
    
while(scanf("%d",&n),n){
        scanf(
"%d",&m);
        
for(i=0;i<m;i++){
            scanf(
"%d %d\n",&si,&ni);
            scanf(
"%s %d",oi,&k);
            
if(oi[0]=='g')
                edge[i].s
=si,edge[i].e=si+ni+1,edge[i].v=-k-1;
            
else
                edge[i].s
=si+ni+1,edge[i].e=si,edge[i].v=k-1;
        }

        
for(i=1,j=m;i<=n+1;i++,j++)
            edge[j].s
=0,edge[j].e=i,edge[j].v=0;
        puts(bellman_ford()
?"lamentable kingdom":"successful conspiracy");
    }

    
return 0;
}

posted on 2009-06-04 15:25 極限定律 閱讀(2212) 評論(0)  編輯 收藏 引用 所屬分類: ACM/ICPC

<2009年6月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

導航

統計

常用鏈接

留言簿(10)

隨筆分類

隨筆檔案

友情鏈接

搜索

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            中文在线一区| 亚洲一区二区在线| 久久五月激情| 欧美在线视频二区| 激情综合色丁香一区二区| 久久综合激情| 久久中文字幕导航| 日韩午夜电影av| 日韩一级黄色大片| 欧美日韩在线电影| 午夜精品国产| 欧美中文在线观看国产| 亚洲高清在线观看| 亚洲欧美国产高清va在线播| 一本色道久久88亚洲综合88| 国产精品日韩一区二区| 欧美专区中文字幕| 久热国产精品| 亚洲永久在线| 久久精品噜噜噜成人av农村| 亚洲精品中文字幕女同| 亚洲色图在线视频| 国内精品一区二区| 亚洲高清精品中出| 国产精品另类一区| 欧美插天视频在线播放| 欧美日韩免费观看一区=区三区 | 久久精品卡一| 亚洲精品国产精品乱码不99按摩| 在线亚洲欧美专区二区| 国内精品模特av私拍在线观看| 亚洲高清二区| 国产亚洲一区二区三区在线观看| 中文日韩电影网站| 香蕉久久一区二区不卡无毒影院| 亚洲国产精品视频| 亚洲欧美第一页| 亚洲精品字幕| 久久久久久噜噜噜久久久精品| 99精品福利视频| 久久久噜噜噜久久久| 亚洲摸下面视频| 蜜桃av噜噜一区| 久久国产精品久久久久久| 欧美国产精品v| 欧美aa在线视频| 国产精品美女主播在线观看纯欲| 亚洲福利视频网站| 好吊视频一区二区三区四区| 一区二区三区 在线观看视| 亚洲国产成人在线| 久久精品伊人| 欧美一级网站| 国产精品高潮呻吟久久| 亚洲国产精品v| 在线观看欧美日韩| 亚洲一区视频| 午夜精品久久久久久久99热浪潮 | 一本高清dvd不卡在线观看| 亚洲福利在线视频| 久久久www成人免费精品| 欧美一区二区三区在线播放| 国产精品白丝jk黑袜喷水| 91久久香蕉国产日韩欧美9色| 伊人久久婷婷| 久久精品国产欧美激情| 久久在线视频| 好看的日韩视频| 久久成人一区| 久久亚洲精品伦理| 精品成人在线| 久久伊人精品天天| 欧美激情国产高清| 99国产精品久久久久老师| 欧美欧美在线| 欧美中文字幕精品| 国产日韩欧美视频在线| 午夜亚洲性色福利视频| 欧美在线视频在线播放完整版免费观看 | 一本久久知道综合久久| 一区二区不卡在线视频 午夜欧美不卡在 | 在线观看一区二区精品视频| 久久精品国产清高在天天线 | 欧美一乱一性一交一视频| 国产乱码精品一区二区三区忘忧草 | 一区二区免费在线观看| 欧美日韩午夜视频在线观看| 亚洲午夜久久久久久久久电影院| 欧美在线视频a| 依依成人综合视频| 欧美成人资源网| 99在线热播精品免费| 性做久久久久久久免费看| 国产字幕视频一区二区| 乱码第一页成人| 亚洲免费精彩视频| 午夜精品福利电影| 亚洲成人在线| 欧美片第一页| 小嫩嫩精品导航| 欧美高清视频一区二区| 亚洲一区二区网站| 好吊妞这里只有精品| 欧美日韩八区| 久久精品二区三区| 一区二区久久久久久| 久久久久免费视频| 日韩一级免费观看| 国产午夜精品久久久久久久| 欧美风情在线| 欧美中文在线观看国产| 亚洲黄色小视频| 久久久www成人免费无遮挡大片| 亚洲黄色影院| 国产日韩欧美在线看| 欧美精品一区二区三区在线播放 | 国产乱码精品1区2区3区| 能在线观看的日韩av| 亚洲影视在线| 亚洲精品美女| 欧美国内亚洲| 久久精品国产第一区二区三区| 亚洲美女av黄| 亚洲成人影音| 国产夜色精品一区二区av| 欧美老女人xx| 裸体女人亚洲精品一区| 欧美一区二区久久久| 99精品久久久| 亚洲全部视频| 欧美高清在线视频| 久久精品国产亚洲高清剧情介绍| 一区二区电影免费观看| 亚洲国产精品黑人久久久| 国产亚洲永久域名| 国产精品私拍pans大尺度在线 | 欧美成人一区二区三区在线观看| 欧美专区第一页| 亚洲制服av| 这里只有精品视频在线| 亚洲乱码日产精品bd| 亚洲国产另类久久久精品极度| 久久综合九色99| 久久综合久久综合九色| 久久久综合精品| 久久久久国产精品午夜一区| 久久av资源网站| 欧美一区二区三区在线看| 欧美夜福利tv在线| 午夜精品久久久久久99热软件| 亚洲综合精品一区二区| 在线一区观看| 午夜精品国产更新| 欧美在线亚洲在线| 久久精品视频在线| 久久久国产91| 麻豆成人av| 亚洲电影有码| 亚洲欧洲三级电影| 在线视频一区观看| 校园激情久久| 狼狼综合久久久久综合网| 男女精品网站| 欧美精品一区二区三区在线播放| 欧美日韩国产综合视频在线观看中文 | 欧美日韩亚洲在线| 欧美三日本三级少妇三2023| 国产精品女主播| 国产精品香蕉在线观看| 国产一区二区三区av电影| 亚洲国产一区二区三区a毛片| 最新日韩中文字幕| 亚洲婷婷免费| 久久久久久久999精品视频| 欧美阿v一级看视频| 亚洲精品一二区| 欧美一区二区视频在线| 久久婷婷久久一区二区三区| 欧美极品影院| 国产亚洲精品久久久| 亚洲国产天堂久久国产91| 亚洲无玛一区| 久久久久网址| 日韩一本二本av| 久久久99免费视频| 欧美日韩一级黄| 国产综合欧美| 亚洲自拍偷拍麻豆| 蜜乳av另类精品一区二区| 一区二区三区产品免费精品久久75| 欧美一区二区三区免费看| 欧美国产精品久久| 国产亚洲视频在线| 日韩写真在线| 免费毛片一区二区三区久久久| 亚洲激情成人| 久久久国产一区二区三区| 欧美亚洲不卡| 一本一本久久a久久精品综合麻豆| 久久精品一级爱片|