最后两点怎么搞都要30s+,但是我不会什么优化啊…暂时就这样吧。Dinic的时间复杂度是O(N^2*M)

这题和TDL的幼儿园模板是一样的。

这次写网络流给自己计时了,大约是40min左右,后来都跑去倒腾后面两组数据去了…

program profit;
type ptype=^node;
node=record
v,w,flow:longint;
next:ptype;
end;
const maxn=+;
inf=maxlongint div ;
var head:array[..maxn] of ptype;
visit:array[..maxn] of boolean;
d,q:array[..maxn] of longint;
n,m,i,j,sta,tar,v,a,b,c,cn:longint;
procedure insert(st,ed,r:longint);
var p,q,pre:ptype;
begin
new(p);
p^.v:=ed;p^.w:=r;p^.flow:=;p^.next:=nil;
q:=head[st];
if q=nil then head[st]:=p
else
begin
while q^.next<>nil do q:=q^.next;
q^.next:=p;
end;
end; function min(a,b:longint):longint;
begin
if a<b then exit(a) else exit(b);
end; function bfs:boolean;
var star,rear,x:longint;
y:ptype;
begin
fillchar(visit,sizeof(visit),false);
fillchar(q,sizeof(q),);
fillchar(d,sizeof(d),);
star:=;rear:=;q[star]:=sta;d[star]:=;visit[sta]:=true;
while star<=rear do
begin
x:=q[star];
y:=head[x];
while y<>nil do
begin
if (not visit[y^.v]) and (y^.w>y^.flow) then
begin
inc(rear);
q[rear]:=y^.v;
visit[y^.v]:=true;
d[y^.v]:=d[x]+;
end;
y:=y^.next;
end;
inc(star);
end;
bfs:=visit[tar];
end; procedure decflow(st,ed,delta:longint);
var y:ptype;
begin
y:=head[st];
while y^.v<>ed do y:=y^.next;
dec(y^.flow,delta);
end; function addflow(p,maxflow:longint):longint;
var y:ptype;
o:longint;
begin
if (p=tar) or (maxflow=) then exit(maxflow);
addflow:=;
y:=head[p];
while y<>nil do
begin
if (d[y^.v]=d[p]+) and (y^.w>y^.flow) then
begin
o:=addflow(y^.v,min(maxflow,y^.w-y^.flow));
if o> then
begin
inc(y^.flow,o);
decflow(y^.v,p,o);
inc(addflow,o);
dec(maxflow,o);
if maxflow= then break;
end;
end;
y:=y^.next;
end;
end; function network:longint;
begin
network:=;
while bfs do
inc(network,addflow(sta,inf));
end; begin
assign(input,'profit9.in');reset(input);
assign(output,'profit9.out');rewrite(output);
readln(n,m);
sta:=;tar:=m+n+;
for i:= to n do
begin
read(v);
insert(m+i,tar,v);
insert(tar,m+i,);
end;
readln;
for i:= to m do
begin
readln(a,b,c);
insert(i,m+a,inf);insert(m+a,i,);
insert(i,m+b,inf);insert(m+b,i,);
insert(sta,i,c);insert(i,sta,);
cn:=cn+c;
end;
writeln(cn-network);
close(input);close(output);
end.

profit

后来写了个优化是我之前自己发明的decflow,现在我在每条边加了一个域op,指向反向边。速度没有什么提升=w=

program profit2;
type ptype=^node;
node=record
v,w,flow:longint;
next,op:ptype;
end;
const maxn=+;
inf=maxlongint div ;
var head:array[..maxn] of ptype;
visit:array[..maxn] of boolean;
d,q:array[..maxn] of longint;
n,m,i,j,sta,tar,v,a,b,c,cn:longint;
procedure insert(st,ed,r1,r2:longint);
var p,q,pre,loc1,loc2:ptype;
begin
new(p);
p^.v:=ed;p^.w:=r1;p^.flow:=;p^.next:=nil;
q:=head[st];
if q=nil then head[st]:=p
else
begin
while q^.next<>nil do q:=q^.next;
q^.next:=p;
end;
loc1:=p;
new(p);
p^.v:=st;p^.w:=r2;p^.flow:=;p^.next:=nil;
q:=head[ed];
if q=nil then head[ed]:=p
else
begin
while q^.next<>nil do q:=q^.next;
q^.next:=p;
end;
loc2:=p;
loc1^.op:=loc2;
loc2^.op:=loc1;
end; function min(a,b:longint):longint;
begin
if a<b then exit(a) else exit(b);
end; function bfs:boolean;
var star,rear,x:longint;
y:ptype;
begin
fillchar(visit,sizeof(visit),false);
fillchar(q,sizeof(q),);
fillchar(d,sizeof(d),);
star:=;rear:=;q[star]:=sta;d[star]:=;visit[sta]:=true;
while star<=rear do
begin
x:=q[star];
y:=head[x];
while y<>nil do
begin
if (not visit[y^.v]) and (y^.w>y^.flow) then
begin
inc(rear);
q[rear]:=y^.v;
visit[y^.v]:=true;
d[y^.v]:=d[x]+;
end;
y:=y^.next;
end;
inc(star);
end;
bfs:=visit[tar];
end; function addflow(p,maxflow:longint):longint;
var y:ptype;
o:longint;
begin
if (p=tar) or (maxflow=) then exit(maxflow);
addflow:=;
y:=head[p];
while y<>nil do
begin
if (d[y^.v]=d[p]+) and (y^.w>y^.flow) then
begin
o:=addflow(y^.v,min(maxflow,y^.w-y^.flow));
if o> then
begin
inc(y^.flow,o);
dec(y^.op^.flow,o);
inc(addflow,o);
dec(maxflow,o);
if maxflow= then break;
end;
end;
y:=y^.next;
end;
end; function network:longint;
begin
network:=;
while bfs do
inc(network,addflow(sta,inf));
end; begin
assign(input,'profit10.in');reset(input);
assign(output,'profit10.out');rewrite(output);
readln(n,m);
sta:=;tar:=m+n+;
for i:= to n do
begin
read(v);
insert(m+i,tar,v,);
//insert(tar,m+i,);
end;
readln;
for i:= to m do
begin
readln(a,b,c);
insert(i,m+a,inf,);//insert(m+a,i,);
insert(i,m+b,inf,);//insert(m+b,i,);
insert(sta,i,c,);//insert(i,sta,);
cn:=cn+c;
end;
writeln(cn-network);
close(input);close(output);
end.

profit2

[NOI 2006] 最大获利 80分的更多相关文章

  1. [BZOJ 1497][NOI 2006]最大获利(最大权闭合子图)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1497 分析: 这是在有向图中的问题,且边依赖于点,有向图中存在点.边之间的依赖关系可以 ...

  2. NOIP 2016 天天爱跑步 80分暴力

    题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.«天天爱跑步»是一个养成类游戏,需要玩家每天按时上线,完成打卡任务. 这个游戏的地图可以看作一一棵包含 个结点 ...

  3. [关于SQL]查询成绩都大于80分的学生

    1.用一条SQL语句 查询出每门课都大于80分的学生姓名name kecheng fenshu张三 语文 81张三 数学 75李四 语文 76李四 数学 90王五 语文 81王五 数学 100王五 英 ...

  4. 用一条SQL语句查出每门课都大于80分的学生的姓名

    用一条SQL语句查出每门课都大于80分的学生的姓名,数据表结构如下: 建表SQL如下: ; -- ---------------------------- -- Table structure for ...

  5. 根据考试成绩输出对应的礼物,90分以上爸爸给买电脑,80分以上爸爸给买手机, 60分以上爸爸请吃一顿大餐,60分以下爸爸给买学习资料。 要求:该题使用多重if完成

    package com.Summer_0417.cn; import java.util.Scanner; /** * @author Summer * 根据考试成绩输出对应的礼物, * 90分以上爸 ...

  6. 案例2:用一条SQL查询出数学语文成绩都大于80分的学生姓名?

    方法1: 查出科目成绩有小于80分的学生姓名,再约束并去重学生不等于查出来的姓名 select distinct A.name from t_score A where A.name not in(s ...

  7. 第一个spring冲刺团队贡献分(80分满分)

    团队贡献分(80分满分): 李泳江 24 叶煜稳 26 谢洪跃 18 周伟雄 12

  8. 【mysql经典题目】科目成绩都大于80分\每个科目的第一名\总成绩排名

    参考:http://blog.csdn.net/lifushan123/article/details/44948135 1.查询出科目成绩都大于80分的学生的名字? drop table if EX ...

  9. SQL查询出每门课都大于80 分的学生姓名

    Course表如下: 查询出每门课都大于80 分的学生姓名有两种方法. 1.select  distinct name from Course where name not in (select di ...

随机推荐

  1. 微信小程序-WebSocket

    wx.connectSocket(OBJECT) 创建一个 WebSocket 连接:一个微信小程序同时只能有一个 WebSocket 连接,如果当前已存在一个 WebSocket 连接,会自动关闭该 ...

  2. dsp28377控制DM9000收发数据——第二版程序,能够实现手术功能,但是容易掉帧;使用读取中断寄存器的方式判断中断

    G:\controlSUITE\device_support\F2837xD\v180\F2837xD_examples_Cpu1\emif1_16bit_asram\cpu01\emif1_16bi ...

  3. php访问远程服务器上的文件

    test.php <?php $fp=fopen('http://www.baidu.com', 'r'); while (!feof($fp)) { $chunk=fgets($fp); ec ...

  4. TCP/IP协议学习(六) 链路层详解

    学习知识很简单,但坚持不懈却又是如此的困难,即使一直对自己说"努力,不能停下"的我也慢慢懈怠了... 闲话不多说,本篇将讲述TCP/IP协议栈的链路层.在本系列第一篇我讲到,TCP ...

  5. x265,帧内预测代码分析

    void Analysis::compressIntraCU(const CUData& parentCTU, const CUGeom& cuGeom, uint32_t& ...

  6. C#: DataBase

    using System.Data.SqlClient; namespace WindowsFormsApplication1{ class DB { private SqlConnection co ...

  7. tcpdump捕捉样例

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  8. nullable,nonnull, null_resettable以及_Null_unspecified的区别和使用

    1.关键字:可以用于属性 方法和返回值参数中 关键字作用:提示作用  告诉开发者属性信息 关键字的目的:迎合swift 强语言,swift必须要指定一个对象是否为空 关键字好处:提高代码规划,减少沟通 ...

  9. codeforces #369div2 B. Chris and Magic Square

    题目:在网格某一处填入一个正整数,使得网格每行,每列以及两条主对角线的和都相等 题目链接:http://codeforces.com/contest/711/problem/B 分析:题目不难,找到要 ...

  10. [转]IE8兼容Object.keys

    转自:http://blog.sina.com.cn/s/blog_6d63cf160102vbsg.html 只需要加入 var DONT_ENUM = "propertyIsEnumer ...