UVA10600

 1 var n,m,cost,i,j,p,t:longint;
 2     use:array[1..10000]of boolean;
 3     ok:array[1..100]of boolean;
 4     a,b,c:array[1..10000]of longint;
 5     d:array[1..100,1..100]of longint;
 6 procedure sort(l,r:longint);
 7 var i,j,x,y: longint;
 8 begin
 9   i:=l;
10   j:=r;
11   x:=c[(l+r) div 2];
12   repeat
13     while c[i]<x do
14       i:=i+1;
15     while x<c[j] do
16       j:=j-1;
17     if not(i>j) then
18     begin
19       y:=a[i];
20       a[i]:=a[j];
21       a[j]:=y;
22       y:=b[i];
23       b[i]:=b[j];
24       b[j]:=y;
25       y:=c[i];
26       c[i]:=c[j];
27       c[j]:=y;
28       i:=i+1;
29       j:=j-1;
30     end;
31   until i>j;
32   if l<j then
33     sort(l,j);
34   if i<r then
35     sort(i,r);
36 end;
37 function max(a,b:longint):longint;begin if a>b then exit(a)else exit(b);end;
38 function min(a,b:longint):longint;begin if a<b then exit(a)else exit(b);end;
39 begin
40   readln(t);
41   for t:=1 to t do
42   begin
43   readln(n,m);
44   for i:=1 to m do
45   begin
46     readln(a[i],b[i],c[i]);
47   end;
48   sort(1,m);
49   fillchar(use,sizeof(use),false);
50   fillchar(ok,sizeof(ok),false);
51   ok[1]:=true;cost:=0;
52   for p:=1 to n-1 do
53   for i:=1 to m do
54   if(ok[a[i]])xor(ok[b[i]])then
55   begin
56     cost:=cost+c[i];
57     d[a[i],b[i]]:=c[i];
58     d[b[i],a[i]]:=c[i];
59     if(ok[a[i]])then
60     begin
61       for j:=1 to n do
62       if ok[j] then
63       begin
64         d[j,b[i]]:=max(d[j,a[i]],c[i]);
65         d[b[i],j]:=d[j,b[i]];
66       end;
67       ok[b[i]]:=true;
68     end
69     else
70     begin
71       for j:=1 to n do
72       if ok[j] then
73       begin
74         d[j,a[i]]:=max(d[j,b[i]],c[i]);
75         d[a[i],j]:=d[j,a[i]];
76       end;
77       ok[a[i]]:=true;
78     end;
79     use[i]:=true;
80     break;
81   end;
82   write(cost,' ');
83   p:=maxlongint;
84   for i:=1 to m do
85   if not use[i] then
86   begin
87     p:=min(p,cost-d[a[i],b[i]]+c[i]);
88   end;
89   writeln(p);
90   end;
91 end.

图论*基本*题

posted @ 2015-07-08 13:00  汪立超  阅读(143)  评论(0)    收藏  举报