BZOJ2330: [SCOI2011]糖果

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   io   資料   

2330: [SCOI2011]糖果Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 2220  Solved: 610
[Submit][Status]Description

 

幼兒園裡有N個小朋友,lxhgww老師現在想要給這些小朋友們分配糖果,要求每個小朋友都要分到糖果。但是小朋友們也有嫉妒心,總是會提出一些要求,比如小明不希望小紅分到的糖果比他的多,於是在分配糖果的時候,lxhgww需要滿足小朋友們的K個要求。幼兒園的糖果總是有限的,lxhgww想知道他至少需要準備多少個糖果,才能使得每個小朋友都能夠分到糖果,並且滿足小朋友們所有的要求。

 

Input

輸入的第一行是兩個整數N,K。

接下來K行,表示這些點需要滿足的關係,每行3個數字,X,A,B。

如果X=1, 表示第A個小朋友分到的糖果必須和第B個小朋友分到的糖果一樣多;

如果X=2, 表示第A個小朋友分到的糖果必須少於第B個小朋友分到的糖果;

如果X=3, 表示第A個小朋友分到的糖果必須不少於第B個小朋友分到的糖果;

如果X=4, 表示第A個小朋友分到的糖果必須多於第B個小朋友分到的糖果;

如果X=5, 表示第A個小朋友分到的糖果必須不多於第B個小朋友分到的糖果;

 

Output

輸出一行,表示lxhgww老師至少需要準備的糖果數,如果不能滿足小朋友們的所有要求,就輸出-1。

 

Sample Input5 7

1 1 2

2 3 2

4 4 1

3 4 5

5 4 5

2 3 5

4 5 1

Sample Output
11

HINT

 

【資料範圍】


    對於30%的資料,保證 N<=100


    對於100%的資料,保證 N<=100000


對於所有的資料,保證 K<=100000,1<=X<=5,1<=A, B<=N

 

Source

Day1

題解:本題是比較明顯的查分約束系統,但我由於沒有寫過,所以這次好好寫了些。首先求最長路或最短路應該搞清,不過這是可以互相轉化的,固定了寫一種也許更好?其次就是spfa寫的時候發現了模版一個錯誤,如果用迴圈隊列的話,終止條件是 l=r 而不是 l>=r最後就是資料有點兒SXBK,第五個點是全為2 or 4,而且資料用肉眼看出來是1<2<3<4<......100000,但它在描述的時候一會是前一個<後一個,一會是後一個>前一個,導致每次的spfa更新不了幾個點但如果最後連0的邊的時候 for i=n downtown 1 do 的或就可以過了,否則就被卡爆了還有就是spfa判環的方法,最短路判負環,最長路判正環,都是新增一個數組c表示0 - c 經過了幾個點,如果c>當前總點數,一定是有環了代碼:
 1 const maxn=1000000+1000;maxm=1000000+1000; 2 type node=record 3      go,next,w:longint; 4      end; 5 var head,q,c:array[0..maxn] of longint; 6     d:array[0..maxn] of int64; 7     v:array[0..maxn] of boolean; 8     e:array[0..maxm] of node; 9     i,n,m,ch,x,y,tot:longint;10     ans:int64;11 procedure insert(x,y,z:longint);12  begin13    inc(tot);14    e[tot].go:=y;e[tot].w:=z;e[tot].next:=head[x];head[x]:=tot;15  end;16 function spfa:boolean;17  var i,x,y,l,r:longint;18    begin19      fillchar(d,sizeof(d),0);20      fillchar(v,sizeof(v),false);21      fillchar(c,sizeof(c),0);22      l:=0;r:=1;q[1]:=0;d[0]:=0;v[0]:=true;23      while l<>r do24       begin25        l:=l mod maxn+1;26        x:=q[l];v[x]:=false;27        i:=head[x];28        while i<>0 do29          begin30           y:=e[i].go;31           if d[x]+e[i].w>d[y] then32             begin33               d[y]:=d[x]+e[i].w;34               c[y]:=c[x]+1;35               if c[y]>n+1 then exit(false);36               if not(v[y]) then37                 begin38                   v[y]:=true;39                   r:=r mod maxn+1;40                   q[r]:=y;41                 end;42             end;43           i:=e[i].next;44          end;45       end;46    exit(true);47    end;48 function init:boolean;49  begin50    readln(n,m);51    for i:=1 to m do52     begin53       readln(ch,x,y);54       if (x=y) and (ch and 1=0) then begin writeln(‘-1‘);exit(false);end;55       case ch of56       1:begin insert(x,y,0);insert(y,x,0);end;57       2:insert(x,y,1);58       3:insert(y,x,0);59       4:insert(y,x,1);60       5:insert(x,y,0);61       end;62     end;63    for i:=n downto 1 do insert(0,i,1);64    exit(true);65  end;66 procedure main;67  begin68    if not(spfa) then writeln(‘-1‘)69    else70      begin71        ans:=0;72       // for i:=1 to n do writeln(i,‘ ‘,d[i],‘ ‘,c[i]);73        for i:=1 to n do inc(ans,d[i]);74        writeln(ans);75      end;76  end;77 78 begin79  assign(input,‘input.txt‘);assign(output,‘output.txt‘);80  reset(input);rewrite(output);81  if init then main;82  close(input);close(output);83 end.   
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.