博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
最小密度路径
阅读量:6958 次
发布时间:2019-06-27

本文共 3256 字,大约阅读时间需要 10 分钟。

给一个有向图,顶点数少于50,有Q条询问(Q<=100000),对于每条询问(x1 y1),输出他们之间的最小密度路径的密度(长度比边数)

两种实践方法,一个是FLOYD,再就是二维SPFA,注意数据有环,SPFA时要控制边数少于n才行,至于为什么,是困扰几代人的烦恼了。

d[i,j,k]表示从i到j走过k条路径的最小路径长度

floyd

View Code
1 program path(input,output);  2 var  3   i,j,k,l,m,n,w,p,x,y:longint;  4   max,tmp:real;  5   f:array[0..51,0..51,0..51]  of longint;  6 begin  7   assign(input,'path.in');  8   reset(input);  9   assign(output,'path.out'); 10   rewrite(output); 11   readln(n,m); 12   fillchar(f,sizeof(f),255); 13   for i:=1 to m do 14     begin 15       readln(x,y,w); 16       if (f[x,y,1]=-1) or (f[x,y,1]>w) then 17       f[x,y,1]:=w; 18     end; 19   for i:=1 to n do f[i,i,0]:=0; 20   for l:=2 to n do 21     for k:=1 to n do 22       for i:=1 to n do 23         for j:=1 to n do 24           if (f[i,k,l-1]>-1)  and (f[k,j,1]>-1) and 25             ((f[i,j,l]=-1) or (f[i,k,l-1]+f[k,j,1]
-1) and (f[x,y,i]
100110001 then writeln(max:0:3) 35 else writeln('OMG!'); 36 end; 37 close(input); 38 close(output); 39 end.

SPFA

View Code
1 program path(input,output);   2 type   3    node     = ^link;   4    link     = record   5           goal,w : longint;   6           next   : node;   7        end;           8    node2 = record   9           now  : integer;  10           step : longint;  11        end;        12 var  13    l      : array[0..100] of node;  14    v      : array[0..60,0..2010] of boolean;  15    d      : array[0..60,0..60,0..2010] of longint;  16    g      : array[0..60,0..60] of boolean;  17    q      : array[0..100010] of node2;  18    answer : array[0..60,0..60] of double;  19    n,m,qq : longint;  20 procedure add(xx,yy,ww: longint );  21 var  22    tt : node;  23 begin  24    new(tt);  25    tt^.w:=ww;  26    tt^.goal:=yy;  27    tt^.next:=l[xx];  28    l[xx]:=tt;  29 end; {
add } 30 procedure init; 31 var 32 i,j,k,xxx,yyy,www : longint; 33 begin 34 readln(n,m); 35 for i:=1 to n do 36 l[i]:=nil; 37 for i:=1 to n do 38 for j:=1 to n do 39 for k:=1 to m do 40 d[i,j,k]:=maxlongint>>2; 41 for i:=1 to n do 42 for j:=1 to n do 43 if i=j then 44 answer[i,j]:=0 45 else 46 answer[i,j]:=-1; 47 fillchar(g,sizeof(g),false); 48 for i:=1 to m do 49 begin 50 readln(xxx,yyy,www); 51 add(xxx,yyy,www); 52 g[xxx,yyy]:=true; 53 end; 54 for k:=1 to n do 55 for i:=1 to n do 56 for j:=1 to n do 57 g[i,j]:=g[i,j] or (g[i,k] and g[k,j]); 58 end; {
init } 59 procedure spfa(vo :longint ); 60 var 61 head,tail : longint; 62 t : node; 63 begin 64 fillchar(v,sizeof(v),false); 65 head:=0; 66 tail:=1; 67 d[vo,vo,0]:=0; 68 v[vo,0]:=true; 69 q[1].step:=0; 70 q[1].now:=vo; 71 while head
nil do 79 begin 80 if d[vo,q[head].now,q[head].step]+t^.w
=0 then 111 begin 112 writeln(answer[x1,y1]:0:3); 113 continue; 114 end; 115 answer[x1,y1]:=999999999; 116 for j:=1 to m do 117 if d[x1,y1,j]<(maxlongint>>2) then 118 if (d[x1,y1,j]/j)

转载于:https://www.cnblogs.com/neverforget/archive/2012/03/04/2379280.html

你可能感兴趣的文章
把手机当网卡使用
查看>>
Linux FTP服务器-VSFTPD虚拟用户配置
查看>>
GNU Guix 0.8 发布,软件包管理器
查看>>
CSS跨浏览器(转)
查看>>
nginx反向代理部署与演示(二)
查看>>
一个php多态性的小例子
查看>>
AIP(Azure 信息保护)之三:保护Office文件
查看>>
Java观察者模式(Observer模式)
查看>>
html5 流程条
查看>>
C#数据类型
查看>>
超150年老字号企业中国仅5家,日本上万
查看>>
永中Office-灵活的斜线表头
查看>>
hibernate映射插件
查看>>
Git 系列之二:Windows 下 Git 客户端的选择,及 msysGit 各种中文问题的解决
查看>>
oracle ORA-30926
查看>>
简析IMAP协议
查看>>
分享一个web应用程序池管理工具
查看>>
用shell实现简单的IDS检测系统
查看>>
Syngress.Nmap.in.the.Enterprise.Your.Guide.to.Network.Scanning
查看>>
GlusterFS 存储结构原理介绍
查看>>