[NOI 2006] 最大获利 80分
最后两点怎么搞都要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分的更多相关文章
- [BZOJ 1497][NOI 2006]最大获利(最大权闭合子图)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1497 分析: 这是在有向图中的问题,且边依赖于点,有向图中存在点.边之间的依赖关系可以 ...
- NOIP 2016 天天爱跑步 80分暴力
题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.«天天爱跑步»是一个养成类游戏,需要玩家每天按时上线,完成打卡任务. 这个游戏的地图可以看作一一棵包含 个结点 ...
- [关于SQL]查询成绩都大于80分的学生
1.用一条SQL语句 查询出每门课都大于80分的学生姓名name kecheng fenshu张三 语文 81张三 数学 75李四 语文 76李四 数学 90王五 语文 81王五 数学 100王五 英 ...
- 用一条SQL语句查出每门课都大于80分的学生的姓名
用一条SQL语句查出每门课都大于80分的学生的姓名,数据表结构如下: 建表SQL如下: ; -- ---------------------------- -- Table structure for ...
- 根据考试成绩输出对应的礼物,90分以上爸爸给买电脑,80分以上爸爸给买手机, 60分以上爸爸请吃一顿大餐,60分以下爸爸给买学习资料。 要求:该题使用多重if完成
package com.Summer_0417.cn; import java.util.Scanner; /** * @author Summer * 根据考试成绩输出对应的礼物, * 90分以上爸 ...
- 案例2:用一条SQL查询出数学语文成绩都大于80分的学生姓名?
方法1: 查出科目成绩有小于80分的学生姓名,再约束并去重学生不等于查出来的姓名 select distinct A.name from t_score A where A.name not in(s ...
- 第一个spring冲刺团队贡献分(80分满分)
团队贡献分(80分满分): 李泳江 24 叶煜稳 26 谢洪跃 18 周伟雄 12
- 【mysql经典题目】科目成绩都大于80分\每个科目的第一名\总成绩排名
参考:http://blog.csdn.net/lifushan123/article/details/44948135 1.查询出科目成绩都大于80分的学生的名字? drop table if EX ...
- SQL查询出每门课都大于80 分的学生姓名
Course表如下: 查询出每门课都大于80 分的学生姓名有两种方法. 1.select distinct name from Course where name not in (select di ...
随机推荐
- 3d游戏模型及地形提取及导航
支持所有DirectX的游戏模型提取 有需要的可以直接联系我!QQ290387340
- Cheatsheet: 2016 05.01 ~ 05.31
Other Awesome Go - A curated list of awesome Go frameworks, libraries and software Visual Studio Cod ...
- A simple script to get all pictures
#-*- coding:utf-8 -*- import shutil import os from Tkinter import * import time import re def get_al ...
- for循环j = j++ 和 j = ++j
package com.test.forname; public class TestForName { public static void main(String[] args) throws E ...
- C#文件创建、修改、访问时间修改
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- C++ STL vector容器学习
STL(Standard Template Library)标准模板库是C++最重要的组成部分,它提供了一组表示容器.迭代器.函数对象和算法的模板.其中容器是存储类型相同的数据的结构(如vector, ...
- 转!!java事务的处理
java的事务处理,如果对数据库进行多次操作,每一次的执行或步骤都是一个事务.如果数据库操作在某一步没有执行或出现异常而导致事务失败,这样有的事务被执行有的就没有被执行,从而就有了事务的回滚,取消先前 ...
- L2TP协议
L2TP协议 L2TP(Layer 2 Tunneling Protocol) 第二层隧道协议.该协议是工业标准的Internet隧道协议. L2TP实现的两种方式 LAC (L2TP Access ...
- js 判断 是否位数字
isNaN(val) val为数字返回false 不为数字返回truedocument.write(isNaN(123)); document.write(isNaN(-1.23)); documen ...
- vscode 与 python 的约会
安装python 官网(https://www.python.org/downloads/)下载, 安装. (简单略过). 运行python代码 运行python代码的常见方式有三种: 运行pytho ...