2502: 清理雪道

题意:任意点出发任意次每条边至少经过一次最小花费。


下界1,裸最小流....

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define fir first
#define sec second
typedef long long ll;
const int N=1005, M=4e5+5, INF=1e9;
inline ll read(){
char c=getchar();ll x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
} int n, m, x, s, t, tot, extra[N];
struct edge{int v, c, f, ne, lower;}e[M];
int cnt=1, h[N];
inline int ins(int u, int v, int c, int b=0) {
e[++cnt]=(edge){v, c, 0, h[u], b}; h[u]=cnt;
e[++cnt]=(edge){u, 0, 0, h[v], b}; h[v]=cnt;
return cnt-1;
}
int q[N], head, tail, vis[N], d[N], cur[N];
bool bfs(int s, int t) {
memset(vis, 0, sizeof(vis));
head=tail=1;
q[tail++]=s; d[s]=0; vis[s]=1;
while(head!=tail) {
int u=q[head++];
for(int i=h[u];i;i=e[i].ne)
if(!vis[e[i].v] && e[i].c>e[i].f) {
vis[e[i].v]=1; d[e[i].v]=d[u]+1;
q[tail++]=e[i].v;
if(e[i].v == t) return true;
}
}
return false;
}
int dfs(int u, int a, int t) {
if(u==t || a==0) return a;
int flow=0, f;
for(int &i=cur[u];i;i=e[i].ne)
if(d[e[i].v]==d[u]+1 && (f=dfs(e[i].v, min(a, e[i].c-e[i].f), t))>0) {
flow+=f;
e[i].f+=f;
e[i^1].f-=f;
a-=f;
if(a==0) break;
}
if(a) d[u]=-1;
return flow;
}
int dinic(int s, int t) {
int flow=0;
while(bfs(s, t)) {
for(int i=0; i<=tot; i++) cur[i]=h[i];
flow+=dfs(s, INF, t);
}
return flow;
} int main() {
freopen("in","r",stdin);
n=read(); s=0; t=n+1;
for(int i=1; i<=n; i++) {
m=read();
while(m--) x=read(), ins(i, x, INF, 1), extra[i]--, extra[x]++;
ins(s, i, INF); ins(i, t, INF);
}
int ss=t+1, tt=t+2, sum=0; tot=tt;
for(int i=s; i<=t; i++) {
if(extra[i]>0) ins(ss, i, extra[i]), sum+=extra[i];
if(extra[i]<0) ins(i, tt, -extra[i]);
}
ins(t, s, INF);
int flow=dinic(ss, tt); //printf("flow %d\n",flow);
int feas = e[cnt-1].f;
e[cnt-1].c = e[cnt].c = 0;
printf("%d",feas-dinic(t, s));
}

BZOJ 2502: 清理雪道 [最小流]的更多相关文章

  1. BZOJ 2502: 清理雪道

    BZOJ 2502: 清理雪道 标签(空格分隔): OI-BZOJ OI-最小流 OI-上下界网络流 Time Limit: 10 Sec Memory Limit: 128 MB Descripti ...

  2. bzoj 2502 清理雪道 (有源汇上下界最小流)

    2502: 清理雪道 Time Limit: 10 Sec  Memory Limit: 128 MB Description        滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场 ...

  3. BZOJ 2502 Luogu P4843 清理雪道 最小流

    题意: 滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道),弧的方向代表斜坡下降的方向. 你的团队负责每周定时清理雪道.你们拥有一架直升飞机 ...

  4. BZOJ 2502 清理雪道(有源汇上下界最小流)

    题面 滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道),弧的方向代表斜坡下降的方向. 你的团队负责每周定时清理雪道.你们拥有一架直升飞机, ...

  5. bzoj 2502 清理雪道(有源汇的上下界最小流)

    [题意] 有一个DAG,要求每条边必须经过一次,求最少经过次数. [思路] 有上下界的最小流.  边的下界为1,上界为无穷.构造可行流模型,先不加ts边跑一遍最大流,然后加上t->s的inf边跑 ...

  6. bzoj 2502: 清理雪道【有上下界有源汇最小流】

    对于原有边,流区间是(1,inf),按着原边连,然后再连(s,i,(0,inf)),(i,t,(0,inf))表示任意位置进出雪场 按着这个建出新图 然后最小流的方法是先跑可行流,设ans为(t,s, ...

  7. Bzoj 2502: 清理雪道 有上下界网络流_最小流

    好长时间没有写网络流了,感觉好手生.对于本题,设一个源点 $s$ 和 $t$.1.由 $s$ 向每个点连一条没有下界,容量为无限大的边,表示以该点为起点.2.由每个点向 $t$ 连一条没有下界,容量为 ...

  8. BZOJ 2502 清理雪道/ Luogu P4843 清理雪道 (有源汇上下界最小流)

    题意 有一个有向无环图,求最少的路径条数覆盖所有的边 分析 有源汇上下界最小流板题,直接放代码了,不会的看dalao博客:liu_runda 有点长,讲的很好,静心看一定能看懂 CODE #inclu ...

  9. BZOJ 2502: 清理雪道 | 有上下界最小流

    #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #defi ...

随机推荐

  1. FtpUtil.java测试 (淘淘商城第三课文件上传)

    首先在common-taotao中创建一个utils包,复制FtpUtil.java到其中.然后如下: @Test public void testFtpUtil() throws Exception ...

  2. mysql 手册关于修改列字符编码的一个bug

    项目因为历史原因使用了 GBK编码,遇到非GBK编码字符时出现乱码问题,情况比较严重,暂时先打算修改 列的字符编码为 utf8mb4. 查看 mysql 手册: 用 GBK 编码转 utf8 进行说明 ...

  3. POJ 3790 最短路径问题(Dijkstra变形——最短路径双重最小权值)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3790 Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你 ...

  4. mysql下优化表和修复表命令使用说明(REPAIR TABLE和OPTIMIZE TABLE)

    REPAIR TABLE `table_name` 修复表 OPTIMIZE TABLE `table_name` 优化表 show create table tablename   表结构 REPA ...

  5. mysql alter总结

    mysql alter总结(转载) 1:删除列 ALTER TABLE [表名字] DROP [列名称] 2:增加列 ALTER TABLE [表名字] ADD [列名称] INT NOT NULL  ...

  6. 【故障】MySQL主从同步故障-Slave_SQL_Running: No

    转自:http://www.linuxidc.com/Linux/2014-02/96945.htm 故障现象:进入slave服务器,运行:mysql> show slave status\G  ...

  7. Redis跟Spring整合,sentinel模式

    普通模式整合 一.在pom.xml中引入redis的依赖 <dependency> <groupId>org.springframework.data</groupId& ...

  8. Django_中国化

    需求: 要求Django显示中文,并使用北京时间 问题原因: Django具有相当的国际化,已经内置了多种语言,汉语当然也不落下,Django默认的时间是utc时间,也就是说相隔八个时区的中国,显示北 ...

  9. 1 let和const

    let命令 1)let声明的变量只在let命令所在的代码块内有效,如:   { let a = ; ;} a // ReferenceError: a is not defined. b 对于for循 ...

  10. mysql 列转行,合并字段

    数据表: 列转行:利用max(case when then) max---聚合函数 取最大值 (case course when '语文' then score else 0 end) ---判断   ...