【bzoj1930】吃豆豆

  • 本文为博主原创,未经许可不得转载

吐槽

先讲一个悲伤的故事

这是我从学OI至今提交次数最多的一道题,题目很水,但是很坑

首先这题卡空间,然后编译超时(excuse me?)

然后卡空间时各种RE

然后去膜大牛的代码,发现需要优化边数,然后又是各种wa

最后发现图建错了。。。。。

于是我的一下午就被这道水题坑走了

题解

问题可以转化为,吃豆人路径可以相交。因为如果路径相交,可以从相交位置开始交换两个吃豆人的后续路径。

考虑到所有豆之间组成了一张有向无环图,我们尝试网络流建模。

对于每个点,有经过次数限制和经过它的收益。

拆点费用流即可。

具体建模:

每个豆豆对应2个点ai,bi,另有S,SS,T,TT

SS->S流量2费用0

ai->bi要建两条边:一条流量为1费用1,一条流量为1费用为0;

S->ai流量INF费用0;bi->TT流量INF费用0。

TT->T流量2费用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
#include<bits/stdc++.h>
#define FILE "read"
#define INF 1000000000
using namespace std;
const int MAXN=(int)5010;
struct node{int y,next,v,w,rel;}e[200000];
int n,len,S,T,TT,ans,Link[MAXN],vis[MAXN],dis[MAXN],q[MAXN],lastnode[MAXN],lastedge[MAXN],flow[MAXN];
pair<int,int> a[MAXN];
namespace INIT{
char buf[1<<15],*fs,*ft;
inline char getc(){return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),fs==ft))?0:*fs++;}
inline int read(){
int x=0,f=1; char ch=getc();
while(!isdigit(ch)) {if(ch=='-') f=-1; ch=getc();}
while(isdigit(ch)) {x=x*10+ch-'0'; ch=getc();}
return x*f;
}
}using namespace INIT;
void insert(int x,int y,int v,int w){
e[++len].next=Link[x];Link[x]=len;e[len].y=y;e[len].v=v;e[len].w=-w;e[len].rel=len+1;
e[++len].next=Link[y];Link[y]=len;e[len].y=x;e[len].v=0;e[len].w=w;e[len].rel=len-1;
}
bool spfa(){
memset(vis,0,sizeof(vis));
memset(dis,10,sizeof(dis));
int head=0,tail=1,oo=dis[0];
q[1]=S; dis[S]=0; vis[S]=1; flow[S]=INF;
while(head!=tail){
int x=q[++head]; vis[x]=0;
if(head==5000) head=0;
for(int i=Link[x];i;i=e[i].next){
if(e[i].v&&e[i].w+dis[x]<dis[e[i].y]){
dis[e[i].y]=e[i].w+dis[x];
flow[e[i].y]=min(e[i].v,flow[x]);
if(!vis[e[i].y]){
vis[e[i].y]=1;
q[++tail]=e[i].y;
if(tail==5000) tail=0;
}
lastnode[e[i].y]=x; lastedge[e[i].y]=i;
}
}
}
if(dis[T]==oo) return 0;
ans+=flow[T]*(-dis[T]);
for(int i=T;i!=S;i=lastnode[i]){
e[lastedge[i]].v-=flow[T];
e[e[lastedge[i]].rel].v+=flow[T];
}
return 1;
}
void solve(){
while(spfa());
printf("%d\n",ans);
}
int main(){
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
n=read(); S=n*2+1; TT=S+1; T=TT+1;
for(int i=1;i<=n;++i){
a[i].first=read(); a[i].second=read();
insert(0,i,2,0);
insert(i,i+n,1,1);
insert(i,i+n,1,0);
insert(i+n,TT,2,0);
}
sort(a+1,a+n+1);
for(int i=1;i<=n;++i){
int maxx=INF;
for(int j=i+1;j<=n;++j)
if(a[j].second>=a[i].second&&a[j].second<maxx){
insert(i+n,j,2,0);
maxx=a[j].second;
}
}
insert(S,0,2,0); insert(TT,T,2,0);
solve();
return 0;
}

附makedata程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<bits/stdc++.h>
#define FILE "read"
using namespace std;
map<pair<int,int>,int>M;
int main(){
freopen(FILE".in","w",stdout);
srand(time(NULL));
int n=2000; printf("%d\n",n);
/*for(int i=1;i<=n/2;++i){
printf("%d %d\n",i,i);
}*///构造数据
for(int i=1;i<=n;++i){
int x=rand()%1000+1,y=rand()%1000+1;
while(M[make_pair(x,y)]) x=rand()%1000+1,y=rand()%1000+1;
M[make_pair(x,y)]=1;
printf("%d %d\n",x,y);
}
return 0;
}
文章目录
  1. 1. 吐槽
  2. 2. 题解
  3. 3. 参考代码
  4. 4. 附makedata程序
,