poj3519
凡是差分约束系统的题目
都是转化为d[j]-d[i]<=w[i,j]的形式
然后我们建立边i-->j 边权为w[i,j]
对于这道题,要求d[n]-d[1]尽可能的大
设d[i]为相对差,d[1]=0,我们只要跑1-->n的最短路即可(为什么是最短路呢?实在不明白简单举一个例子即可)
由于这道题边不存在负权,所以我们用dij+heap更合适
const inf=;
type link=^node;
node=record
po,len:longint;
next:link;
end;
point=record
num,loc:longint;
end; var w:array[..] of link;
heap:array[..] of point;
where,d:array[..] of longint;
z,t,s,i,j,n,m,x,y:longint;
p:link; function min(a,b:longint):longint;
begin
if a>b then exit(b) else exit(a);
end; procedure swap(var a,b:point);
var c:point;
begin
c:=a;
a:=b;
b:=c;
end; procedure add(x,y,z:longint);
var p:link;
begin
new(p);
p^.po:=y;
p^.len:=z;
p^.next:=w[x];
w[x]:=p;
end; procedure up(i:longint);
var j,x,y:longint;
begin
j:=i shr ;
while j> do
begin
if heap[i].num<heap[j].num then
begin
x:=heap[i].loc;
y:=heap[j].loc;
where[x]:=j;
where[y]:=i;
swap(heap[i],heap[j]);
i:=j;
j:=i shr ;
end
else break;
end;
end; procedure sift(i:longint);
var j,x,y:longint;
begin
j:=i shl ;
while j<=s do
begin
if (j+<=s) and (heap[j].num>heap[j+].num) then inc(j);
if heap[i].num>heap[j].num then
begin
x:=heap[i].loc;
y:=heap[j].loc;
where[x]:=j;
where[y]:=i;
swap(heap[i],heap[j]);
i:=j;
j:=i shl ;
end
else break;
end;
end; procedure dij;
var p:link;
mid,k,y:longint;
begin
p:=w[];
for i:= to t do
d[i]:=inf;
d[]:=;
while p<>nil do
begin
x:=p^.po;
d[x]:=min(d[x],p^.len);
p:=p^.next;
end;
s:=;
for i:= to t do
begin
inc(s);
heap[s].num:=d[i];
heap[s].loc:=i;
where[i]:=s;
up(s);
end; for k:= to t do
begin
mid:=heap[].num;
if s= then break;
if mid=inf then break;
x:=heap[].loc;
y:=heap[s].loc;
where[y]:=; swap(heap[],heap[s]);
dec(s); sift();
p:=w[x];
while p<>nil do
begin
y:=p^.po;
if d[y]>p^.len+mid then
begin
d[y]:=p^.len+mid;
heap[where[y]].num:=d[y];
up(where[y]);
end;
p:=p^.next;
end;
end;
end; begin
readln(t,m);
for i:= to m do
begin
readln(x,y,z);
add(x,y,z);
end;
dij;
writeln(d[t]);
end.
poj3519的更多相关文章
- poj3519 Lucky Coins Sequence矩阵快速幂
Lucky Coins Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
随机推荐
- python 面向对象深入理解
面向过程 函数式编程 面向对象编程:面向对象是由类和对象组成,只要用类和对象实现的,就是面向对象编程 def Bar(): print "This is Bar " ...
- SELECT [Code] ,[AlarmID] ,[ItemName] ,[isDeleted] ,[Remark] FROM [LjlData].[dbo].[T_BaseDetail] union select 0--
SELECT [id] ,[AlarmID] ,[ItemName] ,[isDeleted] ,[Remark] FROM [LjlData]. ...
- Android-关于屏幕适配的一些经验
刚开始,我开发时选取的模拟器是WVGA854,其分辨率为854*480.我开发完毕后装在800*480的手机上时感觉很OK,但是装到480*320.以及320*240分辨率上的手机时,很多界面都变形了 ...
- 标准web架构分层
标准Web系统的架构分层 转载:http://blog.csdn.net/yinwenjie http://blog.csdn.net/yinwenjie/article/details/464 ...
- Browser Link: Failed to deserialize JSON in Browser Link call
问题 VS2013中调试程序发现,在浏览器控制台输出如下截图代码:
- Wi-Fi无线网络下行速度超级慢 (5kb/s)之解决方案
转载:http://www.iplaysoft.com/wifi-slow-solution.html 作者:X-Force 转载原因:该文分类提出了多种解决方案,并详述其原因.简洁清晰,可作为参考方 ...
- angularjs中异常处理
1.TypeError: Cannot read property '$valid' of undefined a. Add ng-submit attribute to the form: < ...
- [转载]SQL字符串处理函数大全
[转载]http://www.cnblogs.com/andy2005/archive/2007/12/04/981864.html select语句中只能使用sql函数对字段进行操作(链接sql s ...
- OC - 14.NSOperation与NSOperationQueue
简介 通过NSOperation与NSOperationQueue的组合也能实现多线程 通常将任务封装成NSOperation对象,并将对象添加到NSOperationQueue中实现 NSOpera ...
- 说说http请求
为什么做web前端要了解http标准?因为浏览器要从服务端获取网页,网页也可能将信息再提交给服务器,这其中都有http的连接.web系统既然和http链接有瓜葛,你就必须去了解它.我将从一下几个方面讲 ...