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 ...
随机推荐
- VIM中文乱码(_vimrc配置文件备份)
_vimrc在用户目录下: set fileencodings=ucs-bom,utf-,cp936,gb18030,big5,euc-jp,euc-kr,latin1 set encoding=ut ...
- HDU-1114(背包DP)
Piggy-Bank Problem Description Before ACM can do anything, a budget must be prepared and the necessa ...
- 一个js 变量作用域问题
一个js 域问题,有一本书 叫 javasrcip pattert 好像是,写的很好,, <!DOCTYPE html> <html> <head lang=" ...
- 关于 ORA - 01861 文字与格式字符串不匹配问题(oracle存储过程)
一般问题(TO_DATE 和 TO_CHAR 两种格式互换)比如: 只要转化下格式就OK ,这里就不详细解释这两种格式的用法了! 今天把之前做好的模块拿到当地实习,不管怎么测 ,连续测试了好几个存储过 ...
- plsql中文乱码问题(显示问号)
问题:打开plsql,执行sql语句,中文显示乱码: 解决方案: 输入sql语句select userenv('language') from dual查看数据库字符集 输入sql语句select * ...
- A题笔记(4)
No. 1384 这题没啥 不过网考成绩出了,发现我的口语分数相较其他人还挺高的~~~哈哈哈 Code::Blocks 有时在程序运行结束后,.exe 并没有结束,因而之后无论怎么调试和修改代码,运行 ...
- iis7.5 应用程序池 经典模式和集成模式的区别
在 IIS 7.5 中,应用程序池有两种运行模式:集成模式和经典模式. 应用程序池模式会影响服务器处理托管代码请求的方式. 如果托管应用程序在采用集成模式的应用程序池中运行,服务器将使用 IIS 和 ...
- javascript 用函数实现“继承”
一.知识储备: 1.枚举属性名称的函数: (1)for...in:可以在循环体中遍历对象中所有可枚举的属性(包括自有属性和继承属性) (2)Object.keys():返回数组(可枚举的自有属性) ( ...
- hdu 1396 Counting Triangles(递推)
Counting Triangles Problem Description Given an equilateral triangle with n thelength of its side, p ...
- js隔行变色
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...