poj 1797
2013-09-08 09:48
最大生成树,输出生成树中最短的边儿即可
或者对边儿排序,二份答案+BFS判断是否1连通N
时间复杂度都是O(NlogN)的
附最大生成树pascal代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | / / By BLADEVIL var m, n, t :longint; pre, other, len :array[ 0. . 101000 ] of longint; father :array[ 0. . 100100 ] of longint; i :longint; procedure init; var i :longint; x, y, z :longint; begin read(n,m); for i: = 1 to m do begin read(x,y,z); pre[i]: = x; other[i]: = y; len [i]: = z; end; for i: = 1 to n do father[i]: = i; end; procedure qs(low,high:longint); var i, j, x, z :longint; begin i: = low; j: = high; x: = len [(i + j) div 2 ]; while i<j do begin while x> len [j] do dec(j); while x< len [i] do inc(i); if i< = j then begin z: = len [i]; len [i]: = len [j]; len [j]: = z; z: = pre[i]; pre[i]: = pre[j]; pre[j]: = z; z: = other[i]; other[i]: = other[j]; other[j]: = z; inc(i); dec(j); end; end; if i<high then qs(i,high); if j>low then qs(low,j); end; function getfather(x:longint):longint; begin if father[x] = x then exit(x); father[x]: = getfather(father[x]); exit(father[x]); end; procedure main; var j :longint; a, b :longint; begin init; qs( 1 ,m); for j: = 1 to m do begin a: = getfather(pre[j]); b: = getfather(other[j]); if a<>b then father[b]: = a; a: = getfather( 1 ); b: = getfather(n); if a = b then begin writeln( 'Scenario #' ,i, ':' ); writeln( len [j]); writeln; exit; end; end; end; begin read(t); for i: = 1 to t do main; end. |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步