poj1364 King --- 差分約束

來源:互聯網
上載者:User

標籤:使用   os   for   io   amp   table   

這是我見過最扯淡的題面之一。

題讀了差不多一半我都覺得我這題肯定讀不懂了,到最後終於看到重點了靠!

就是個差分約束大水題!毫無新意!

扯些什麼皇后想生孩子!生了男孩是個弱智!父王很擔心!這些有的沒的有意思嗎!!


題目就是給一個序列,告訴你 a b gt/lt c 表示從a起的b+1個數之和大於/小於c

就根據這個列不等式,要把> 或 <關係換成>= <= 就減一就可以了

列出不等式:

S[a-1]-S[a+b]<=-c-1

S[a+b]-S[a-1]<=c-1


需要注意的是,不是每次添加附加結點0都是正確的,要保證添加的點是不會使用到的,比如n+1


#include <iostream>#include <cstring>#include <string>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <map>#define inf 0x3f3f3f3f#define eps 1e-6#define ll __int64using namespace std;struct node{    int v,w,next;}e[10010];int n,m,h,head[110],d[110],inq[110],outq[110];void init(){    memset(head,-1,sizeof head);    h=0;}void addedge(int a,int b,int c){    e[h].v=b;    e[h].w=c;    e[h].next=head[a];    head[a]=h++;}int spfa(int st){    memset(d,0x3f,sizeof d);    memset(inq,0,sizeof inq);    memset(outq,0,sizeof outq); //   d[st]=0;inq[st]=1;    queue<int> q;    q.push(st);    for(int i=1;i<=n;i++)    {        inq[i]=1;        q.push(i);    }    while(!q.empty())    {        int x=q.front();        q.pop();        inq[x]=0;        outq[x]++;        if(outq[x]>n+1) return 0;//存在負環//是在與源點連通的圖上有負環        for(int i=head[x];i!=-1;i=e[i].next)        {            if(d[e[i].v]>d[x]+e[i].w)            {                d[e[i].v]=d[x]+e[i].w;                if(!inq[e[i].v])                {                    inq[e[i].v]=1;                    q.push(e[i].v);                }            }        }    }    return 1;}int main(){    int a,b,c;    char s[10];    while(scanf("%d",&n)&&n)    {        init();        scanf("%d",&m);        while(m--)        {            scanf("%d%d%s%d",&a,&b,s,&c);            if(s[0]=='g')                addedge(b+a,a-1,-c-1);            else                addedge(a-1,b+a,c-1);        }      //  for(int i=0;i<=n;i++)      //      addedge(0,i,0);        int ans=spfa(0);        if(ans)            printf("lamentable kingdom\n");        else printf("successful conspiracy\n");    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.