bzoj3955
首先,最短路不同的两辆车一定不会发生堵塞
对于最短路相同的点,我们把属于最短路径上的边拎出来建图跑最大流即可
然后我TLE了……
因为很明显建出来图很大,而真正流的流量很小
普通的初始标号都是0的sap在增广的时候编号会非常慢
运用fanhq博客里的做法,先用dfs计算图的标号O(m+n),然后再跑sap就跑得飞起了
- const inf=;
- type node=record
- po,next,flow:longint;
- end;
- point=record
- loc,num:longint;
- end;
- way=record
- po,next,num:longint;
- end;
- var w:array[..] of way;
- e:array[..] of node;
- h:array[..] of point;
- v:array[..] of boolean;
- wh,d,a,cur,hi,pre,p,q,numh:array[..] of longint;
- ans,j,i,len,n,m,t,c,x,y,z:longint;
- procedure swap(var a,b:point);
- var c:point;
- begin
- c:=a;
- a:=b;
- b:=c;
- end;
- procedure sift(i:longint);
- var j,x,y:longint;
- begin
- j:=i shl ;
- while j<=t do
- begin
- if (j<t) and (h[j].num>h[j+].num) then inc(j);
- if h[i].num>h[j].num then
- begin
- x:=h[i].loc;
- y:=h[j].loc;
- wh[x]:=j;
- wh[y]:=i;
- swap(h[i],h[j]);
- i:=j;
- j:=j shl ;
- end
- else break;
- end;
- end;
- procedure up(i:longint);
- var j,x,y:longint;
- begin
- j:=i shr ;
- while j> do
- begin
- if h[i].num<h[j].num then
- begin
- x:=h[i].loc;
- y:=h[j].loc;
- wh[x]:=j;
- wh[y]:=i;
- swap(h[i],h[j]);
- i:=j;
- j:=j shr ;
- end
- else break;
- end;
- end;
- procedure dij;
- var i,k,x,y:longint;
- begin
- t:=n;
- for i:= to n do
- begin
- if i= then d[i]:= else d[i]:=inf;
- wh[i]:=i;
- h[i].num:=d[i];
- h[i].loc:=i;
- end;
- for k:= to n- do
- begin
- x:=h[].loc;
- wh[h[t].loc]:=;
- swap(h[],h[t]);
- dec(t);
- sift();
- i:=q[x];
- while i<> do
- begin
- y:=w[i].po;
- if d[x]+w[i].num<d[y] then
- begin
- d[y]:=d[x]+w[i].num;
- h[wh[y]].num:=d[y];
- up(wh[y]);
- end;
- i:=w[i].next;
- end;
- end;
- end;
- function cmp(i,j:longint):boolean;
- begin
- if d[i]=d[j] then exit(i<j);
- exit(d[i]<d[j]);
- end;
- procedure sort(l,r:longint);
- var i,j,x,y:longint;
- begin
- i:=l;
- j:=r;
- x:=a[(l+r) shr ];
- repeat
- while cmp(a[i],x) do inc(i);
- while cmp(x,a[j]) do dec(j);
- if not(i>j) then
- begin
- y:=a[i]; a[i]:=a[j]; a[j]:=y;
- inc(i);
- dec(j);
- end;
- until i>j;
- if l<j then sort(l,j);
- if i<r then sort(i,r)
- end;
- procedure add(x,y,f:longint);
- begin
- inc(len);
- e[len].po:=y;
- e[len].flow:=f;
- e[len].next:=p[x];
- p[x]:=len;
- end;
- procedure build(x,y,f:longint);
- begin
- add(x,y,f);
- add(y,x,);
- end;
- procedure ins(x,y,z:longint);
- begin
- inc(len);
- w[len].po:=y;
- w[len].num:=z;
- w[len].next:=q[x];
- q[x]:=len;
- end;
- function sap(lim:longint):longint;
- var i,j,u,tmp,q:longint;
- begin
- u:=; sap:=;
- while hi[]<n+ do
- begin
- i:=cur[u];
- while i<>- do
- begin
- j:=e[i].po;
- if (e[i].flow>) and (hi[u]=hi[j]+) then
- begin
- pre[j]:=u;
- cur[u]:=i;
- u:=j;
- if u= then
- begin
- inc(sap);
- if sap=lim then exit;
- while u<> do
- begin
- u:=pre[u];
- j:=cur[u];
- dec(e[j].flow);
- inc(e[j xor ].flow);
- end;
- end;
- break;
- end;
- i:=e[i].next;
- end;
- if i=- then
- begin
- dec(numh[hi[u]]);
- if numh[hi[u]]= then break;
- tmp:=n;
- q:=-;
- i:=p[u];
- while i<>- do
- begin
- j:=e[i].po;
- if e[i].flow> then
- if hi[j]<tmp then
- begin
- q:=i;
- tmp:=hi[j];
- end;
- i:=e[i].next;
- end;
- cur[u]:=q;
- hi[u]:=tmp+;
- inc(numh[hi[u]]);
- if u<> then u:=pre[u];
- end;
- end;
- end;
- procedure dfs(x:longint);
- var tmp,i,y,q:longint;
- begin
- if x= then
- begin
- hi[]:=;
- inc(numh[]);
- exit;
- end;
- v[x]:=true;
- tmp:=n;
- q:=-;
- i:=p[x];
- while i<>- do
- begin
- y:=e[i].po;
- if e[i].flow> then
- begin
- if not v[y] then dfs(y);
- if hi[y]<tmp then
- begin
- tmp:=hi[y];
- q:=i;
- end;
- end;
- i:=e[i].next;
- end;
- cur[x]:=q;
- hi[x]:=tmp+;
- inc(numh[hi[x]]);
- end;
- procedure work(l,r:longint);
- var i,j:longint;
- begin
- len:=-;
- fillchar(p,sizeof(p),);
- for i:= to n do
- begin
- j:=q[i];
- while j<> do
- begin
- y:=w[j].po;
- if d[i]+w[j].num=d[y] then build(y,i,);
- j:=w[j].next;
- end;
- end;
- i:=l;
- while i<=r do
- begin
- j:=i+;
- while (j<=r) and (a[j]=a[i]) do inc(j);
- build(,a[i],j-i);
- i:=j;
- end;
- fillchar(numh,sizeof(numh),);
- fillchar(v,sizeof(v),false);
- dfs();
- if hi[]<n+ then
- ans:=ans+sap(r-l+);
- end;
- begin
- readln(n,m,c);
- for i:= to m do
- begin
- readln(x,y,z);
- ins(x,y,z);
- ins(y,x,z);
- end;
- dij;
- for i:= to c do
- read(a[i]);
- sort(,c);
- i:=;
- while i<=c do
- begin
- j:=i+;
- while (d[a[i]]=d[a[j]]) and (j<=c) do inc(j);
- if j=i+ then inc(ans)
- else if a[i]= then inc(ans,j-i)
- else work(i,j-);
- i:=j;
- end;
- writeln(ans);
- end.
bzoj3955的更多相关文章
- [bzoj3955] [WF2013]Surely You Congest
首先最短路长度不同的人肯定不会冲突. 对于最短路长度相同的人,跑个最大流就行了..当然只有一个人就不用跑了 看起来会T得很惨..但dinic在单位网络里是O(m*n^0.5)的... #include ...
随机推荐
- Linux课本第一二章
一.第一章:Linux内核简介 1.操作系统和内核:操作系统是指在整个系统中负责完成最基本功能和系统管理的那些部分,包括内核.设备驱动程序.启动引导程序.命令行shall等. 内核就是操作系统的核心, ...
- Windows下访问VMware中tomcat
很多人都可能和我一样,运行在虚拟机上,开发时在windows上进行. 在linux上运行tomcat,并且windows中能ping通虚拟机,但就不能通过虚拟机ip访问到8080端口上的tomcat, ...
- windows server 2008 下安装openmeetings 2.2.0
经过两天的痛苦经历,终于完成了openmeetings的安装部署.其实步骤都很简单,只是网上的资料都是英文的,而且很多教程都是针对openmeeting之前的版本,导致我在部署的时候走了很多弯路.网上 ...
- MVC3+AutoFac实现程序集级别的依赖注入
1.介绍 所谓程序集级别的依赖注入是指接口和实现的依赖不使用配置文件或硬代码实现(builder.RegisterType<UserInfoService>().As<IU ...
- 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。 n<=39
// test14.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- Entity Framework 基础
在忙碌中渡过了5,6,7 月份,现在些抽点时间对Entity Framework的使用做一些基础的回忆. Entity Framework 是什么? Entity Framework(EF)和我们所熟 ...
- C# mongodb [下]
概述 传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(collection).文档对象(d ...
- 【BZOJ】【3524】【POI2014】Couriers
可持久化线段树 裸可持久化线段树,把区间第K大的rank改成num即可……(往儿子走的时候不减少) 苦逼的我……MLE了一次(N*30),RE了一次(N*10)……数组大小不会开…… 最后开成N*20 ...
- jQuery1.9.1源码分析--Ajax模块
//Serialize an array of form elements or a set of //key/values into a query string // 将数组形式的表单元素或者哈希 ...
- C# 在vs2010中打开vs2012的项目(转)
在vs2010中打开vs2012的项目 今天在自己的电脑上装了vs2010然后要打开之前在vs2012上创建的sln文件 被提示-- 无法打开在新版本上创建的sln--解决方案--文件 其实vs201 ...