bzoj2502 清理雪道

题意:有一个有向图,一次飞行可以走一条路径,走过的路径相当于清理过了,问清理完所有的路径最少要多少次

题解:模板题,每条边考虑以$1$为下界,无限为上界,由于可以放到任何一个点作为起点并且不限制你到这里多少次所以新建一个源点向所有点连$0$为下界,无限为上界的边,然后同理可以选择任意点结束飞行,所以所有点向汇点连$0$为下界,无限为上界的边,跑最小流即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
namespace io{
const int L=30000020;
int f;
char ibuf[L],*iS,*iT,c;
inline char Gc(){
if(iS==iT){
iT=(iS=ibuf)+fread(ibuf,1,L,stdin);
return iS==iT?EOF:*iS++;
}
return*iS++;
}
template<class I>void Gi(I&x){
for(f=1,c=Gc();c<'0'||c>'9';c=Gc())if(c=='-')f=-1;
for(x=0;c<='9'&&c>='0';c=Gc())x=x*10+(c&15);x*=f;
}
};
using io::Gi;
namespace mfIO {
inline int readInt()
{
char c;int tmp=0,x=1;c=getchar();
while(c>'9' || c<'0') {if(c=='-') x=-1;c=getchar();}
while(c>='0' && c<='9') {tmp=tmp*10+c-'0';c=getchar();}
return tmp*x;
}
inline ll readLL()
{
char c;ll tmp=0,x=1;c=getchar();
while(c>'9' || c<'0') {if(c=='-') x=-1;c=getchar();}
while(c>='0' && c<='9') {tmp=tmp*10+c-'0';c=getchar();}
return tmp*x;
}
inline void writeInt(int x)
{
if(x==0) {putchar('0');return;}
if(x<0) x*=-1;
char s[21];int tp=0;
while(x>0) s[++tp]=x%10+'0',x/=10;
while(tp) putchar(s[tp--]);
}
inline void writeLL(ll x)
{
if(x==0) {putchar('0');return;}
if(x<0) x*=-1;
char s[21];int tp=0;
while(x>0) s[++tp]=x%10+'0',x/=10;
while(tp) putchar(s[tp--]);
}
}
using mfIO::readInt;
using mfIO::readLL;
using mfIO::writeInt;
using mfIO::writeLL;
const int maxn=50005+5;
const int maxm=125005+5;
const int inf=0x3f3f3f3f;
struct edge{
int id,to,cap,rev;
edge(int id=0,int to=0,int cap=0,int rev=0): id(id),to(to),cap(cap),rev(rev) {}
};
vector<edge> g[maxn];
int tag=-1;
void addedge(int id,int u,int v,int cap,bool lab=false)
{
g[u].push_back(edge(id,v,cap,(int)g[v].size()));
if(lab) tag=(int)g[u].size()-1;
g[v].push_back(edge(0,u,0,(int)g[u].size()-1));
}
int up[maxm],A[maxn];
int dis[maxn],cur[maxn],q[maxn+5],n,m,S,T,N,ss,tt;
bool bfs(int st,int ed)
{
memset(dis,-1,sizeof(dis));
dis[st]=0;
int head=0,tail=0;
q[head]=st;
while(head<=tail) {
int u=q[head%N];head++;
for(int i=0;i<(int)g[u].size();i++) {
edge& e=g[u][i];
if(e.cap>0 && dis[e.to]==-1) {
dis[e.to]=dis[u]+1;
tail++;q[tail%N]=e.to;
}
}
}
return dis[ed]!=-1;
}
int dfs(int v,int ed,int mf)
{
if(v==ed) return mf;
for(int &i=cur[v];i<(int)g[v].size();i++) {
int u=g[v][i].to;
edge& e=g[v][i];
if(e.cap>0 && dis[u]==dis[v]+1) {
int F=dfs(u,ed,min(mf,e.cap));
if(F>0) {
g[v][i].cap-=F;
g[u][g[v][i].rev].cap+=F;
return F;
}
}
}
return 0;
}
int din(int st,int ed)
{
int ret=0,tmp=0;
while(bfs(st,ed)) {
memset(cur,0,sizeof(cur));
while((tmp=dfs(st,ed,inf))>0) ret+=tmp;
}
return ret;
}
int LOW=0;
void AddEdge(int i,int u,int v,int L,int R)
{
addedge(i,u,v,R-L);
A[u]-=L;A[v]+=L;
up[i]=R;
}
void NewEdge(int st,int ed)
{
for(int i=1;i<=n;i++) {
if(A[i]>0) addedge(0,S,i,A[i]),LOW+=A[i];
else addedge(0,i,T,-A[i]);
}
addedge(0,ed,st,inf,1);
}
int pan(int st,int ed)
{
int fl=din(S,T);
if(fl>=LOW) {
int ril=inf-g[ed][tag].cap;
edge & e=g[ed][tag];
e.cap=g[e.to][e.rev].cap=0;
ril-=din(ed,st);
return ril;
}
else return -1;
}
int main()
{
scanf("%d",&n);
int tot,x,id=0;
ss=0,tt=n+1,S=n+2,T=n+3,N=n+5;
for(int i=1;i<=n;i++) {
scanf("%d",&tot);
for(int j=1;j<=tot;j++) {
scanf("%d",&x);
AddEdge(++id,i,x,1,inf);
}
}
for(int i=1;i<=n;i++) {
AddEdge(++id,ss,i,0,inf);
AddEdge(++id,i,tt,0,inf);
}
NewEdge(ss,tt);
int ans=pan(ss,tt);
if(ans==-1) assert(0);
else printf("%d\n",ans);
return 0;
}