hotel.PAS 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. {$A+,B-,D+,E+,F-,G-,I+,L+,N-,O-,P-,Q-,R-,S+,T-,V+,X+,Y+}
  2. {$M 64000,0,655360}
  3. program Hotel;
  4. const
  5. fin = 'hotel.in';
  6. fon = 'hotel.out';
  7. maxN = 100;
  8. var
  9. N, M : integer;
  10. C : longint;
  11. a : array[1..maxn,1..maxn]of integer;
  12. f : array[1..maxn,1..maxn]of integer;
  13. f1 : array[1..maxn]of integer;
  14. d, p : array[1..maxn]of integer;
  15. i,j,k,l : integer;
  16. procedure Input;
  17. var fi:text;
  18. begin
  19. assign(fi,fin); reset(fi);
  20. readln(fi,n,m);
  21. fillchar(a,sizeof(a),0);
  22. for i:=1 to n-1 do
  23. begin
  24. for j:=i+1 to n do
  25. read(fi,a[i,j]);
  26. readln(fi);
  27. end;
  28. close(fi);
  29. end;
  30. procedure Out;
  31. var fo:text;
  32. begin
  33. assign(fo,fon); rewrite(fo);
  34. writeln(fo,C);
  35. for i:=1 to n-1 do
  36. begin
  37. for j:=i+1 to n-1 do
  38. write(fo,f[i,j],' ');
  39. writeln(fo,f[i,n]);
  40. end;
  41. close(fo);
  42. end;
  43. begin
  44. input;
  45. for k:=1 to m do
  46. begin
  47. for j:=2 to n do
  48. begin d[j]:=-1; p[j]:=0; end;
  49. p[1]:=1; d[1]:=0;
  50. repeat
  51. l:=0;
  52. for i:=1 to n do
  53. for j:=1 to n do
  54. if (p[i]>0) and (f[i,j]<a[i,j]) and (d[i]+(j-i)>d[j]) then
  55. begin
  56. d[j]:=d[i]+(j-i);
  57. p[j]:=i; l:=1;
  58. end;
  59. for i:=1 to n-1 do
  60. if (p[i]>0) and (d[i]>d[i+1]) then
  61. begin
  62. d[i+1]:=d[i];
  63. p[i+1]:=i; l:=1;
  64. end;
  65. for i:=1 to n-1 do
  66. if (p[i+1]>0) and (f1[i]>0) and (d[i+1]>d[i]) then
  67. begin
  68. d[i]:=d[i+1];
  69. p[i]:=i+1; l:=1;
  70. end;
  71. until (l=0);
  72. if p[n] > 0 then
  73. begin
  74. i:=p[n]; j:=n;
  75. c:=c+d[n];
  76. repeat
  77. if abs(d[i]-d[j])=0 then
  78. if i<j
  79. then f1[i]:=f1[i]+1
  80. else f1[j]:=f1[j]-1
  81. else
  82. begin
  83. f[i,j]:=f[i,j]+1;
  84. f[j,i]:=-f[i,j];
  85. end;
  86. j:=i; i:=p[i];
  87. until j=1;
  88. end;
  89. end;
  90. out;
  91. end.