SQL tree data struct(7): find the minimal cost route
2008-03-02 11:54
375 查看
-- find the minimal cost route
create table flights(departure char(20), destination char(20), cost int)
insert flights
select 'Chicago', 'New York', 10 union
select 'Chicago', 'Milwaukee', 20 union
select 'Denver', 'Chicago', 25 union
select 'Seattle', 'Chicago',30 union
select 'Seattle', 'Denver',40 union
select 'Seattle', 'San Francisco', 50 union
select 'Milwaukee', 'Seattle',11 union -- cycle 1: to the first source
select 'Milwaukee', 'Michigan', 36 union
select 'Milwaukee', 'Denver', 9 -- cycle 2: to the intermediate source
/*
insert into flights select destination, departure, cost from flights
select * from flights
*/
if object_id('mincostroute') is not null
drop procedure mincostroute
go
create procedure mincostroute(@from char(20), @to char(20))
with encryption
as
declare @depth int, @hasmore int
select @depth = 0, @hasmore = 1
/* can not use table variable, a table-var can not refer : table-var.field */
select top 0 src=replicate('.',20), dst=replicate('.',20), stops=0, route=replicate('.', 4000), ttlcost=0
into #map from sysfiles1
insert into #map select '', @from, @depth, @from, 0
while @hasmore > 0
begin
select @depth = @depth + 1
insert into #map
select flights.departure, flights.destination, @depth,
#map.route + '->' + destination, ttlcost+cost
from #map inner join flights on #map.dst = flights.departure
where stops = @depth-1 and flights.destination not in (select src from #map)
set @hasmore = @@rowcount
end
--select * from #map
--select * from #map where dst = @to
select top 1 route=rtrim(route), ttlcost from #map where dst = @to order by ttlcost asc
drop table #map
go
exec mincostroute 'Seattle', 'Michigan'
drop table flights
drop proc mincostroute
go
create table flights(departure char(20), destination char(20), cost int)
insert flights
select 'Chicago', 'New York', 10 union
select 'Chicago', 'Milwaukee', 20 union
select 'Denver', 'Chicago', 25 union
select 'Seattle', 'Chicago',30 union
select 'Seattle', 'Denver',40 union
select 'Seattle', 'San Francisco', 50 union
select 'Milwaukee', 'Seattle',11 union -- cycle 1: to the first source
select 'Milwaukee', 'Michigan', 36 union
select 'Milwaukee', 'Denver', 9 -- cycle 2: to the intermediate source
/*
insert into flights select destination, departure, cost from flights
select * from flights
*/
if object_id('mincostroute') is not null
drop procedure mincostroute
go
create procedure mincostroute(@from char(20), @to char(20))
with encryption
as
declare @depth int, @hasmore int
select @depth = 0, @hasmore = 1
/* can not use table variable, a table-var can not refer : table-var.field */
select top 0 src=replicate('.',20), dst=replicate('.',20), stops=0, route=replicate('.', 4000), ttlcost=0
into #map from sysfiles1
insert into #map select '', @from, @depth, @from, 0
while @hasmore > 0
begin
select @depth = @depth + 1
insert into #map
select flights.departure, flights.destination, @depth,
#map.route + '->' + destination, ttlcost+cost
from #map inner join flights on #map.dst = flights.departure
where stops = @depth-1 and flights.destination not in (select src from #map)
set @hasmore = @@rowcount
end
--select * from #map
--select * from #map where dst = @to
select top 1 route=rtrim(route), ttlcost from #map where dst = @to order by ttlcost asc
drop table #map
go
exec mincostroute 'Seattle', 'Michigan'
drop table flights
drop proc mincostroute
go
相关文章推荐
- Hdoj find the mincost route
- Floyd求最小环 hdu1599 find the mincost route
- HDU 1599 find the mincost route
- HD1599 find the mincost route(floyd + 最小环)
- hdu 1599 find the mincost route(无向图的最小环:求从一个点遍历所有节点以后回到原点的最短路径)
- hdu 1599 find the mincost route
- HDU1599 find the mincost route (floyd求最小环)
- hdu1599 find the mincost route
- hdu1599 flody求最小环 find the mincost route
- hd 1599 find the mincost route(floyd)
- HDU 1599 find the mincost route (无向图floyd最小环详解)
- HDU 1599 find the mincost route 无向图最小环
- 杭电1599 find the mincost route
- 杭电-1599 find the mincost route(最小环有向图)
- HDU 1599:find the mincost route(floyd最短路成环)
- find the mincost route hdoj1599
- poj 1734 Sightseeing trip hdu 1599 find the mincost route 最小环 解题报告
- 【HDU 1599】find the mincost route 【最小环】
- HDU 1599 find the mincost route 、 poj 1734 Sightseeing trip
- HDU1599:find the mincost route