procedure DFS (nowcity, nowdist, culture)
begin
if nowcity = t then //是否到达目标城市
do
if nowdist < ans then ans := nowdist; //比当前答案小就更新
exit //退出
end
if nowdist > ans then exit //剪枝1,比当前答案大就退出.
for each E(nowcity, nextcity) //枚举每条当前城市的出边
if nextcity ∉ cultrue then //如果到达的城市没有被登记过
do
Add nextcity to culture //登记到达的城市,并且将到达的城市所影响 Add cities influenced to culture 的城市也登记上)
DFS(nextcity, nowdist + dist(nowcity, nextcity), culture) //继续搜索
Clear culture //回溯操作.
end
end;
注:是pascal伪代码