【图论】【单源最短路】【dijikstra】pascal+邻接矩阵/边集数组
来源:互联网 发布:微课录屏制作软件 编辑:程序博客网 时间:2024/05/19 14:19
1.邻接矩阵
var w:array[1..15000,1..15000] of -1..2; p:array[1..15000] of integer; i,n,m,t1,t2,t3,t:longint;procedure dijik;var i,j,minn,min:longint; d:array[1..15000] of longint; f:array[1..15000] of boolean;begin fillchar(f,sizeof(f),false); for i:=1 to n do if w[1,i]<>0 then d[i]:=w[1,i] else d[i]:=maxlongint; minn:=1; f[1]:=true; d[1]:=0; for i:=1 to n-2 do begin min:=maxlongint; for j:=1 to n do if (d[j]<min) and (d[j]>0) and (not f[j]) then begin minn:=j; min:=d[j]; end; f[minn]:=true; if min=maxlongint then exit; for j:=1 to n do if (w[minn,j]<>0) and (not f[j]) then if (min+w[minn,j]<d[j]) then begin d[j]:=min+w[minn,j]; //p[j]:=minn; end; end; writeln(d[n]);end;begin readln(n,m); for i:=1 to m do begin readln(t1,t2,t3); w[t1,t2]:=t3; end; dijik;end.
2.边集数组
ype edge = record a,b:longint; w:-1..2; end;var i,n,m:longint; num:array[0..300001] of longint; g:array[0..500001] of edge;procedure sort(l,r:longint);var i,j,x,y: longint;begin i:=l;j:=r;x:=g[(l+r) div 2].a; repeat while g[i].a<x do inc(i); while x<g[j].a do dec(j); if not(i>j) then begin y:=g[i].a; g[i].a:=g[j].a; g[j].a:=y; y:=g[i].b; g[i].b:=g[j].b; g[j].b:=y; y:=g[i].w; g[i].w:=g[j].w; g[j].w:=y; inc(i);dec(j); end; until i>j; if l<j then sort(l,j); if i<r then sort(i,r);end;procedure dijik;var i,j,min,minn:longint; d:array[0..300001] of int64; f:array[0..300001] of boolean; pre:array[0..300001] of longint;function find(u,v:longint):longint;var i:longint;begin find:=0; for i:=pre[u] to pre[u+1]-1 do with g[i] do if b=v then find:=w;end;begin for i:=1 to n do d[i]:=maxlongint; fillchar(f,sizeof(f),false); sort(1,m); pre[1]:=1; for i:=2 to n do pre[i]:=pre[i-1]+num[i-1]; for i:=1 to pre[2]-1 do with g[i] do if w<>0 then d[b]:=w; minn:=1;f[1]:=true;d[1]:=0; for i:=1 to n-2 do begin min:=maxlongint; for j:=1 to n do if (d[j]<min) and (not f[j]) then begin minn:=j; min:=d[j]; end; if min=maxlongint then exit; f[minn]:=true; for j:=1 to n do if not f[j] then begin t:=find(minn,j); if (t<>0) and (min+t<d[j]) then d[j]:=min+t; end; end; writeln(d[n]);end;begin readln(n,m); for i:=1 to m do with g[i] do readln(a,b,w); for i:=1 to m do with g[i] do inc(num[a]); dijik;end.
0 0
- 【图论】【单源最短路】【dijikstra】pascal+邻接矩阵/边集数组
- 数据结构之图的存储表示(邻接矩阵、邻接表和边集数组)
- 边集数组
- 【图论】【单源最短路】【SPFA】pascal+邻接表
- 【图论】【搜索】【DFS】邻接矩阵+pascal实现模板
- HDU 2544最短路 dijikstra算法
- 图的储存(边集数组)
- 图存储5-边集数组
- Dijkstra单源最短路,邻接矩阵形式(模板)
- 图的存储结构-邻接多重表、边集数组
- 十字链表 、邻接多重表 、边集数组
- 图论 邻接矩阵
- 关于图的存储方法 (静态邻接表、前向星、边集数组)
- 图的存储结构(十字链表、邻接多重表、边集数组)
- 图的存储结构-邻接表-边集数组-前向星
- 大话数据结构二十二:图的存储结构之边集数组
- 7.2图的存储结构(十字链表、邻接多重表、边集数组)
- 基于邻接矩阵的有回溯最小堆优化的单源最短路Dijkstra算法
- Caffe全环境配置备忘录
- 深度学习中的数据扩增方法
- 运算符、选择结构、循环结构
- 前台 JSON对象转换成字符串 相互转换 的几种方式
- 文件的简单操作
- 【图论】【单源最短路】【dijikstra】pascal+邻接矩阵/边集数组
- JSP——1
- codeforces - 508B - Anton and currency you all know(贪心)
- NodeJS基础API搭建服务器详细过程记录
- 【总结】软件工程视频(一)
- Android 加载动画(关于SpinKit的使用)
- leetcode_413. Arithmetic Slices 数组中连续的等差数列个数
- 第1章 ECS
- linux shell编程之——if语句