2502: 清理雪道
Time Limit: 10 Sec Memory Limit: 128 MB[ Submit][ Status][ Discuss]
Description
滑雪场坐落在
FJ
省西北部的若干座山上。
从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道),弧的方向代表斜坡下降的方向。
你的团队负责每周定时清理雪道。你们拥有一架直升飞机,每次飞行可以从总部带一个人降落到滑雪场的某个地点,然后再飞回总部。从降落的地点出发,这个人可以顺着斜坡向下滑行,并清理他所经过的雪道。
由于每次飞行的耗费是固定的,为了最小化耗费,你想知道如何用最少的飞行次数才能完成清理雪道的任务。
Input
Output
输出文件的第一行是一个整数
k
–
直升飞机的最少飞行次数。
Sample Input
8
1 3
1 7
2 4 5
1 8
1 8
0
2 6 5
0
1 3
1 7
2 4 5
1 8
1 8
0
2 6 5
0
Sample Output
4
HINT
Source
就加了个读优,就排了个第二名,玄学啊
辣鸡的我还想二分,结果这是最小流啊!二分个**啊!
#include<iostream>
#include<cstring>
#include<cstdio>
#define INF 100000000
using namespace std;
const int N = 115;
int n,S,T,SS,TT,si[N],last[N],cur[N],cnt=1,h[N],ans;
struct Edge{
int to,next,v;
}e[2000005];
void insert( int u, int v, int w ){
e[++cnt].to = v; e[cnt].next = last[u]; e[cnt].v = w; last[u] = cnt;
e[++cnt].to = u; e[cnt].next = last[v]; e[cnt].v = 0; last[v] = cnt;
}
bool bfs(){
static int q[N];
int head = 0, tail = 0;
memset(h,-1,sizeof(h));
h[S]=1;q[++tail]=S;
while( head != tail ){
int x = q[++head];
for( int i = last[x]; i; i = e[i].next )
if( e[i].v && h[e[i].to] == -1 ){
h[e[i].to] = h[x]+1;
q[++tail] = e[i].to;
if( e[i].to == T ) return true;
}
}
return false;
}
int dfs( int x, int f ){
int w,used=0;
if( x == T ) return f;
for( int i = last[x]; i; i = e[i].next )
if( h[e[i].to] == h[x] + 1 && e[i].v ){
w = dfs(e[i].to,min(e[i].v,f-used));
e[i].v -= w; e[i^1].v += w; used += w;
if( f == used ) return f;
}
if( !used ) h[x] = -1;
return used;
}
void rebuild(){
for( int i = last[S]; i; i = e[i].next ) e[i].v = e[i^1].v = 0;
for( int i = last[T]; i; i = e[i].next ) e[i].v = e[i^1].v = 0;
for( int i = last[TT]; i; i = e[i].next )
if( e[i].to == SS ){
ans += e[i^1].v; e[i].v = e[i^1].v = 0; break;
}
insert(S,TT,INF); insert(SS,T,INF);
}
int main(){
scanf("%d", &n ); S = n+1; T = n+2; SS = n+3; TT = n+4;
for( int i = 1,m; i <= n; i++ ){
scanf("%d", &m);
for( int j = 1,v; j <= m; j++ )
scanf("%d", &v), si[i]--, si[v]++, insert(i,v,INF);
}
for( int i = 1; i <= n; i++ ){
if( si[i] > 0 ) insert(S,i,si[i]);
if( si[i] < 0 ) insert(i,T,-si[i]);
insert(SS,i,INF); insert(i,TT,INF);
}
insert(TT,SS,INF);
while(bfs()) dfs(S,INF);
rebuild();
while(bfs()) ans -= dfs(S,INF);
printf("%d", ans);
return 0;
}