Don't like ads? PRO users don't see any ads ;-)

# Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 1.79 KB  |  hits: 16  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. :- use_module(library(chr)).
2. :- chr_option(optimize, full).
3. :- chr_type list(T) ---> []; [T|list(T)].
4. :- chr_type edge ---> edge(int,int,int).
5. :- chr_type heap ---> edge-list(heap).
6. :- chr_constraint
7.         heap(+heap), tail(+list(heap)), pop, top(+edge),
8.         matrix(+int,+list(list(any))),
9.         row(+int,+int,+list(any)),
10.         current_weight(+int),
11.         count(+int),
12.         group(+list(int),+int),
14.         problem107.
15.
16. problem107 <=> current_weight(0), data(X), matrix(1,X), count(0), pop.
17.
18. data(X) :- csv_read_file('network.txt',Rows), maplist(term_list,Rows,X).
19.
20. term_list(T,X) :- T =.. [_|X].
21.
22. heap(M-L), heap(N-L1) <=> comp(M,N) | heap(M-[N-L1|L]).
23.
24. tail([H|T]) <=> heap(H), tail(T).
25. tail([])    <=> true.
26.
27. pop, heap(A-L) <=> tail(L), top(A).
28.
29. comp(edge(_,_,M),edge(_,_,N)) :- M =< N.
30.
31. matrix(N,[H|T]) <=> row(N,1,H), N1 is N+1, matrix(N1,T).
32. matrix(_,[])    <=> true.
33.
34. row(M,N,[-|T]) <=> N1 is N+1, row(M,N1,T).
35. row(M,N,[_|T]) <=> M >= N | N1 is N+1, row(M,N1,T).
36. current_weight(X), row(M,N,[H|T])
37. <=> heap(edge(M,N,H)-[]), X1 is X+H, current_weight(X1), N1 is N+1, row(M,N1,T).
38. row(_,_,[])    <=> true.
39.
40. count(39) \ heap(_) <=> true.
41. count(39) \ current_weight(M), group(_,N) <=> X is M-N, answer(X).
42. pop, count(39) <=> true.
43.
44. group(L,_) \ top(edge(A,B,_))
45. <=> member(A,L), member(B,L) | pop.
46. group(L,X), group(L1,Y), count(N), top(edge(A,B,Z))
47. <=> (member(A,L), member(B,L1); member(B,L), member(A,L1))
48.   | ord_union(L,L1,L2), X1 is X+Y+Z, group(L2,X1), N1 is N+1, count(N1), pop.
49. group(L,X), count(N), top(edge(A,B,Y))
50. <=> member(A,L)
51.   | ord_add_element(L,B,L1), X1 is X+Y, group(L1,X1), N1 is N+1, count(N1), pop.
52. group(L,X), count(N), top(edge(A,B,Y))
53. <=> member(B,L)
54.   | ord_add_element(L,A,L1), X1 is X+Y, group(L1,X1), N1 is N+1, count(N1), pop.
55. count(N), top(edge(A,B,X))
56. <=> list_to_ord_set([A,B],L), group(L,X), N1 is N+1, count(N1), pop.