凡是差分约束系统的题目
都是转化为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的更多相关文章

  1. poj3519 Lucky Coins Sequence矩阵快速幂

    Lucky Coins Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. PHP+jQuery+Ajax实现用户登录与退…

    用户登录与退出功能应用在很多地方,而在有些项目中,我们需要使用Ajax方式进行登录,登录成功后只刷新页面局部,从而提升了用户体验度.本文将使用PHP和jQuery来实现登录和退出功能. 查看演示DEM ...

  2. 【锋利的jQuery】学习笔记02

    第二章 jQuery选择器 一.jQuery选择器的优势 写法简洁 $("div") 支持css2和css3选择器(对于css3选择器支持这一项,我认为应该是jQuery首先创造并 ...

  3. sql存储过程通过ID删除两表中的数据。

    CREATE OR REPLACE PROCEDURE del_p --建立名为del_p 的过程 IS CURSOR get_abid --简历名为get_abid的cursor 用来存放a表的id ...

  4. google map 定位

    在map初始化的过程中,得到当前经纬度,完成初始化地图,通过HTML5中的Geolocation实现,具体参考:http://www.jb51.net/html5/71556.html 1.获取当前地 ...

  5. SQL语句一之建库

    USE master --转到系统表goIF EXISTS(SELECT *  FROM sysdatabases WHERE name ='Test') --查询是否存在Test数据库DROP DA ...

  6. ria service 单元测试

    https://blogs.msdn.microsoft.com/kylemc/2011/08/18/unit-testing-a-wcf-ria-domainservice-part-1-the-i ...

  7. winform中的chat

    百度一下 源代码下载:百度一下

  8. struts2 Action 接收参数的三种方法

    刚学Struts2 时 大家可能遇到过很多问题,这里我讲一下Action 接收参数的三种方法,我曾经在这上面摔过一回.所以要警醒一下自己..... 第一种:Action里声明属性,样例:account ...

  9. ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)

    Greatest Common Increasing Subsequence 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...

  10. thinkphp关联查询(多表查询)

    1.Table方法:定义要操作的数据表名称,可以动态改变当前操作的数据表名称,需要写数据表的全名,包含前缀,可以使用别名, 例如: $Model->Table('think_user user' ...