2018南京I题:

dinic,链式前向星,数组队列,当前弧优化,不memset全部数组,抛弃满流点,bfs只找一条增广路,每次多路增广

#include <bits/stdc++.h>
#define ll long long
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rep(ii,a,b) for(int ii=a;ii<=b;++ii)
using namespace std;
const int maxn=2e3+10,maxm=2e6+10;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const double PI=acos(-1.0);
//head
int casn,n,m,k;
class mxf{public:
struct node{int to,next;ll cap;}e[maxm<<1];
int cur[maxn],head[maxn],que[maxn],dis[maxn],nume=1,s,t;
inline void adde(int a,int b,ll c){e[++nume]={b,head[a],c};head[a]=nume;}
inline void add(int a,int b,ll c){adde(a,b,c);adde(b,a,0);}
void init(int n=maxn-1){memset(head,0,(n+1)<<2);nume=1;}
bool bfs(){
memset(dis,-1,(t+1)<<2);
dis[t]=0,que[0]=t;
int tp=0,ed=1;
while(tp!=ed){
int now=que[tp++];tp%=maxn;
for(int i=head[now];i;i=e[i].next){
int to=e[i].to;
if(dis[to]==-1&&e[i^1].cap){
dis[to]=dis[now]+1;
if(to==s) return true;
que[ed++]=to;ed%=maxn;
}
}
}
return false;
}
ll dfs(int now,ll flow=0x3f3f3f3f){
if(now==t||flow==0) return flow;
ll use=0;
for(int &i=head[now];i&&use!=flow;i=e[i].next){
int to=e[i].to;
if(dis[to]+1!=dis[now])continue;
ll tmp=dfs(to,min(e[i].cap,flow-use));
e[i].cap-=tmp,e[i^1].cap+=tmp,use+=tmp;
}
if(!use) dis[now]=-1;
return use;
}
ll getflow(int ss,int tt){
s=ss,t=tt;ll ans=0;
memcpy(cur,head,(t+1)<<2);
while(bfs()){
ans+=dfs(s);
memcpy(head,cur,(t+1)<<2);
}
return ans;
}
}net;
int main() {IO;
cin>>n>>m>>k;
int s=n+m+1,t=n+m+10,ss=n+m+2;
net.init(t);net.add(s,ss,k);
rep(i,1,n){
net.add(s,i,1);net.add(ss,i,1);
int x;cin>>x;
while(x--){
int y;cin>>y;
net.add(i,n+y,1);
}
}
while(m--) net.add(n+m+1,t,1);
cout<<net.getflow(s,t);
return 0;
}

2016青岛G

原始对偶算法+dijkstra正权化

#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
#pragma GCC optimize("Ofast")
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rep(ii,a,b) for(int ii=a;ii<=b;++ii)
using namespace std;
const int maxn=2e3+10,maxm=2e6+10;
const double eps=1e-4;
int casn,n,m,k;
class mcf{public:
#define tpp double
struct node{int to;ll cap;tpp cost;int rev;};
int prev[maxn],pree[maxn];
tpp dis[maxn],cost,h[maxn];
ll f;
vector<node> g[maxn];
void init(int n=maxn-2){rep(i,0,n+1) g[i].clear();}
inline void add(int from,int to,ll cap,tpp cost){
g[from].push_back({to,cap,cost,(int)g[to].size()});
g[to].push_back({from,0,-cost,(int)g[from].size()});
}
tpp getcost(int s,int t){
f=0,cost=0;
fill(h,h+1+t,0);
while(1){
#define pdi pair<tpp,int>
priority_queue<pdi,vector<pdi>,greater<pdi> >que;
fill(dis,dis+t+1,1e10);
dis[s]=0;que.push(make_pair(0,s));
while(!que.empty()){
auto now=que.top();que.pop();
if(dis[now.second]<now.first)continue;
int x=now.second;
int cnt=0;
for(auto &i:g[x])
if(i.cap>0&&dis[i.to]>eps+dis[x]+h[x]-h[i.to]+i.cost){
dis[i.to]=dis[x]+i.cost+h[x]-h[i.to];
prev[i.to]=x;
pree[i.to]=cnt++;
que.push(make_pair(dis[i.to],i.to));
}else cnt++;
}
if(dis[t]>=1e9)break;
rep(i,0,t) h[i]+=dis[i];
ll d=1e9;
for(int now=t;now!=s;now=prev[now])
d=min(d,g[prev[now]][pree[now]].cap);
if(d==1e9)break;
f+=d;cost+=d*h[t];
for(int now=t;now!=s;now=prev[now]){
node &e=g[prev[now]][pree[now]];
e.cap-=d,g[now][e.rev].cap+=d;
}
}
return cost;
}
}net;
int a[maxn],b[maxn];
int main() {IO;
cout<<fixed<<setprecision(2);
cin>>casn;
while(casn--){
cin>>n>>m;
int s=n+1,t=n+2;
net.init(t);
rep(i,1,n){
cin>>a[i]>>b[i];
int k=min(a[i],b[i]);
a[i]-=k,b[i]-=k;
if(a[i])net.add(s,i,a[i],0);
if(b[i])net.add(i,t,b[i],0);
}
while(m--){
int x,y,z;double p;
cin>>x>>y>>z>>p;
p=-log(1-p);
if(z>0) net.add(x,y,1,0);
if(z>1) net.add(x,y,z-1,p);
}
double ans=1.0-exp(-net.getcost(s,t));
cout<<ans<<endl;
}
return 0;
}

EK+spfa

#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rep(ii,a,b) for(int ii=a;ii<=b;++ii)
using namespace std;
const int maxn=1e3+10,maxm=1e5+10;
const double eps=1e-8;
int casn,n,m,k;
class mcf{public:
#define tpp double
int nume=1,s,t,mflow;
int head[maxn],flow[maxn],pre[maxn];
tpp dis[maxn],mcost;
struct node{int to,next,cap;tpp cost;}e[maxm<<1];
void init(int n=maxn-10){
fill(head,head+n+2,0);
nume=1,mflow=mcost=0;
}
inline void add(int from,int to,int cap,tpp cost){
e[++nume]={to,head[from],cap,cost};head[from]=nume;
e[++nume]={from,head[to],0,-cost};head[to]=nume;
}
bool vis[maxn];
queue<int>q;
bool spfa(){
fill(dis,dis+2+t,1e9);
fill(vis,vis+2+t,false);
dis[s]=0;flow[s]=1e9;q.push(s);
while (!q.empty()){
int now=q.front();q.pop();
vis[now]=false;
for (int i=head[now];i;i=e[i].next){
int to=e[i].to;
tpp cost=e[i].cost;
if (e[i].cap&&dis[now]+cost+eps<dis[to]){
dis[to]=dis[now]+cost;
flow[to]=min(flow[now],e[i].cap);
pre[to]=i;
if (!vis[to]){
vis[to]=true;
q.push(to);
}
}
}
}
return dis[t]<1e9;
}
void update(){
int x=t;
while (x!=s){
int i=pre[x];
e[i].cap-=flow[t];
e[i^1].cap+=flow[t];
x=e[i^1].to;
}
mflow+=flow[t];
mcost+=(tpp)flow[t]*dis[t];
}
double getcost(int s,int t){
this->s=s;this->t=t;
while (spfa())update();
return mcost;
}
}net;
int a[maxn],b[maxn];
int main() {IO;
cout<<fixed<<setprecision(2);
while(cin>>casn){
while(casn--){
cin>>n>>m;
int s=n+1,t=n+2;
net.init(t);
rep(i,1,n){
cin>>a[i]>>b[i];
int k=min(a[i],b[i]);
a[i]-=k,b[i]-=k;
if(a[i])net.add(s,i,a[i],0);
if(b[i])net.add(i,t,b[i],0);
}
while(m--){
int x,y,z;double p;
cin>>x>>y>>z>>p;
p=-log(1-p);
if(z>0) net.add(x,y,1,0);
if(z>1) net.add(x,y,z-1,p);
}
double ans=net.getcost(s,t);
ans=exp(-ans);
ans=1.0-ans;
cout<<ans<<endl;
}
}
return 0;
}

网络流板子/费用流板子 2018南京I题+2016青岛G题的更多相关文章

  1. 【BZOJ2324】[ZJOI2011]营救皮卡丘(网络流,费用流)

    [BZOJ2324][ZJOI2011]营救皮卡丘(网络流,费用流) 题面 BZOJ 洛谷 题解 如果考虑每个人走的路径,就会很麻烦. 转过来考虑每个人破坏的点集,这样子每个人可以得到一个上升的序列. ...

  2. Libre 6012 「网络流 24 题」分配问题 (网络流,费用流)

    Libre 6012 「网络流 24 题」分配问题 (网络流,费用流) Description 有n件工作要分配给n个人做.第i个人做第j件工作产生的效益为\(c_{ij}\).试设计一个将n件工作分 ...

  3. 【BZOJ1449】[JSOI2009]球队收益(网络流,费用流)

    [BZOJ1449][JSOI2009]球队收益(网络流,费用流) 题面 BZOJ 洛谷 题解 首先对于一支队伍而言,总共进行多少场比赛显然是已知的,假设是\(n_i\)场,那么它的贡献是:\(C_i ...

  4. 洛谷P4003 无限之环(infinityloop)(网络流,费用流)

    洛谷题目传送门 题目 题目描述 曾经有一款流行的游戏,叫做 Infinity Loop,先来简单的介绍一下这个游戏: 游戏在一个 n ∗ m 的网格状棋盘上进行,其中有些小方格中会有水管,水管可能在格 ...

  5. HDU 3667 Transportation(网络流之费用流)

    题目地址:HDU 3667 这题的建图真是巧妙...为了保证流量正好达到k.须要让每一次增广到的流量都是1,这就须要把每一条边的流量都是1才行.可是每条边的流量并非1,该怎么办呢.这个时候能够拆边,反 ...

  6. Going Home POJ - 2195 费用流板子题

    On a grid map there are n little men and n houses. In each unit time, every little man can move one ...

  7. 网络流(费用流)CodeForces 321B:Ciel and Duel

    Fox Ciel is playing a card game with her friend Jiro. Jiro has n cards, each one has two attributes: ...

  8. 2018 ACM-ICPC徐州站网络赛 G题

    There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xxx , yy ...

  9. 洛谷P4003 无限之环(费用流)

    传送门 神仙题啊……不看题解我可能一年都不一定做得出来……FlashHu大佬太强啦 到底是得有怎样的脑回路才能一眼看去就是费用流啊…… 建好图之后套个板子就好了,那么我们着重来讨论一下怎么建图 首先, ...

随机推荐

  1. [Alpha阶段]项目展示博客

    目录 Alpha阶段项目展示 1.团队成员介绍 2.工程相关信息 (1)我们的用户 (2)产品表现 (3)团队分工 (4)项目管理 (5)测试 (6)文档 (7)用户调研 3.项目信息 (1)实际进展 ...

  2. vue keepalive 动态设置缓存

    场景:A首页.B列表页.C详情页B---->C 缓存‘列表1’详情的数据A---->C 读取‘列表1’详情的数据B---->C (希望清除‘列表1’的缓存,变成缓存‘列表2’详情的数 ...

  3. "=="和 equals 方法究竟有什么区别?

    "=="和 equals 方法究竟有什么区别? ==操作符专门用来比较两个变量的值是否相等,也就是用于比较变量所对应的内存中所存储的数值是否相同, 要比较两个基本类型的数据或两个引 ...

  4. LeetCode136.只出现一次的数字

    给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. 你可以不使用额外空间来实现吗? 示例 1: 输入: [ ...

  5. C# call webservice方法

    https://www.cnblogs.com/Fooo/p/5507153.html

  6. 使用ffmpeg将Mp4转gif

    视频转动图,是个强需求,家大业大的微博相册只可上传图片,进而基于微博相册的生态也是如此.目前,网络上有许多转换.压缩的网站,多数执行速度慢或者收费,体验较差. ffmpeg是一个开源的音频处理软件,支 ...

  7. Comet OJ - Contest #1

    A:随便怎么暴力. #include<bits/stdc++.h> using namespace std; #define ll long long #define N 25 char ...

  8. 为知笔记Linux版编译使用记录

    本文档长期不定时更新,根据使用情况进行反馈. 目录 编译 Error creating SSL context 无法输入中文 如何打包使用 桌面图标 Markdown Windows 版本差异 常用快 ...

  9. windows下提权基础

    拿到webshell很多时候代表渗透的开始,下面带来windows提权基础 环境:虚拟机 win7系统 首先:查看权限whoami 我们知道windows的高权限应该是administrator和sy ...

  10. 树莓派中QT实现串口通讯

    树莓派中QT实现串口通讯 开发平台为QT 此博客QT使用的为WiringPi驱动 我使用的串口调试助手为 cutecom 先简单说一些开发过程中需要注意的问题 Linux 下设备为 tty ,对应在 ...