隐藏
「JLOI2014」镜面通道 - 最小割 | Bill Yang's Blog

路终会有尽头,但视野总能看到更远的地方。

0%

「JLOI2014」镜面通道 - 最小割

理论储备

要做此题首先要将“光线”这种玄学玩意儿转化掉。
似乎有个物理上的定理,放在这道题上就是:水能过去,光就能过去
换句话说,只要管道从左到右存在一个通路,那么必定存在一条通路使得光线可以通过。


模型建立

有了上述理论储备,我们就可以对此题建立模型了。
此题已经存在一些相交或不相交的物件,要求去掉一些物件使得存在一条通路。
转化一下目标:
要求去掉一些物件使得上顶与下顶不再连通。
不再连通,这不就是割吗?
而题目要求最少拿走多少个物件,不就是最小割吗?
当然这道题不是割边,而是割点,故按照最小点割集的方法建模:
每一个物件拆成两个点,一个入点,一个出点。
每一个入点到出点连接一条容量为1的边。
如果两个物件相交,则分别将它们的出点与入点相连,容量无穷大。
然后跑一次Dinic即可。


模型举例

如图,样例的实物图如下:

建图如下:

显然,最小割为2。


代码

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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
inline const int Get_Int() {
int num=0,bj=1;
char x=getchar();
while(x<'0'||x>'9') {
if(x=='-')bj=-1;
x=getchar();
}
while(x>='0'&&x<='9') {
num=num*10+x-'0';
x=getchar();
}
return num*bj;
}
//二倍+1出点 二倍入点
double eps=1e-8;
int dcmp(const double& x) {
if(fabs(x)<=eps)return 0;
if(x>eps)return 1;
return -1;
}
struct Point {
int x,y;
Point() {}
Point(int _x,int _y):x(_x),y(_y) {}
} C;
const int maxn=2005;
struct Thing { //矩形/圆
int bj; //1圆 2矩形
Point c;
int r;
Point LD,RU;
} a[maxn];
double Dist(const Point& a,const Point& b) {
return sqrt((double)(a.x-b.x)*(a.x-b.x)+(double)(a.y-b.y)*(a.y-b.y));
}
bool Check(Thing a,Thing b) { //a与b是否相交
int bj=a.bj+b.bj;
if(bj==2)return dcmp(Dist(a.c,b.c)-(a.r+b.r))<=0; //圆相交
if(bj==3) { //矩形a与圆b相交
if(a.bj==1)swap(a,b);
//与矩形四个点相交
if(dcmp(Dist(a.LD,b.c)-b.r)<=0)return 1;
if(dcmp(Dist(Point(a.LD.x,a.RU.y),b.c)-b.r)<=0)return 1;
if(dcmp(Dist(Point(a.RU.x,a.LD.y),b.c)-b.r)<=0)return 1;
if(dcmp(Dist(a.RU,b.c)-b.r)<=0)return 1;
//与矩形四条线相交
if(a.LD.x<=b.c.x&&a.RU.x>=b.c.x)
if(abs(b.c.y-a.LD.y)<=b.r||abs(b.c.y-a.RU.y)<=b.r)return 1;
if(a.LD.y<=b.c.y&&a.RU.y>=b.c.y)
if(abs(b.c.x-a.LD.x)<=b.r||abs(b.c.x-a.RU.x)<=b.r)return 1;
//在矩形中间
if(a.LD.x<=b.c.x&&a.RU.x>=b.c.x&&a.LD.y<=b.c.y&&a.RU.y>=b.c.y)return 1;
return 0;
}
if(bj==4) { //矩形a与矩形b相交
if(a.LD.x>b.RU.x||a.LD.y>b.RU.y||a.RU.x<b.LD.x||a.RU.y<b.LD.y)return 0;
return 1;
}
}
struct Edge {
int from,to,cap,flow;
};
struct Dinic {
int n,m,s,t;
vector<Edge>edges;
vector<int>G[maxn];
bool vst[maxn];
int dist[maxn],Current[maxn];
void init(int n) {
this->n=n;
edges.clear();
for(int i=1; i<=n; i++)G[i].clear();
}
void AddEdge(int from,int to,int cap) {
edges.push_back((Edge) {
from,to,cap,0
});
edges.push_back((Edge) {
to,from,0,0
});
m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool Bfs() {
memset(vst,0,sizeof(vst));
memset(dist,0,sizeof(dist));
queue<int>Q;
Q.push(s);
dist[s]=0;
vst[s]=1;
while(!Q.empty()) {
int Now=Q.front();
Q.pop();
for(int i=0; i<G[Now].size(); i++) {
Edge& e=edges[G[Now][i]];
int Next=e.to;
if(!vst[Next]&&e.cap>e.flow) {
vst[Next]=1;
dist[Next]=dist[Now]+1;
Q.push(Next);
}
}
}
return vst[t];
}
int Dfs(int Now,int a) {
if(Now==t||a==0)return a;
int flow=0;
for(int& i=Current[Now]; i<G[Now].size(); i++) {
Edge& e=edges[G[Now][i]];
int Next=e.to;
if(dist[Now]+1!=dist[Next])continue;
int Nextflow=Dfs(Next,min(a,e.cap-e.flow));
if(Nextflow>0) {
e.flow+=Nextflow;
edges[G[Now][i]^1].flow-=Nextflow;
flow+=Nextflow;
a-=Nextflow;
if(a==0)break;
}
}
return flow;
}
int MaxFlow(int s,int t) {
this->s=s;
this->t=t;
int flow=0;
while(Bfs()) {
memset(Current,0,sizeof(Current));
flow+=Dfs(s,0x7fffffff/2);
}
return flow;
}
} dinic;
int n;
void AddEdge(int x,int y) {
dinic.AddEdge(x<<1|1,y<<1,0x7fffffff/2);
dinic.AddEdge(y<<1|1,x<<1,0x7fffffff/2);
}
int main() {
C.x=Get_Int();
C.y=Get_Int();
n=Get_Int();
int Start=1,End=(n+1)<<1;
a[0].bj=a[n+1].bj=2;
a[0].LD=Point(0,C.y);
a[0].RU=C;
a[n+1].LD=Point(0,0);
a[n+1].RU=Point(C.x,0);
for(int i=1; i<=n; i++) {
a[i].bj=Get_Int();
if(a[i].bj==1) {
a[i].c.x=Get_Int();
a[i].c.y=Get_Int();
a[i].r=Get_Int();
} else {
a[i].LD.x=Get_Int();
a[i].LD.y=Get_Int();
a[i].RU.x=Get_Int();
a[i].RU.y=Get_Int();
}
}
for(int i=0; i<=n+1; i++)
for(int j=i+1; j<=n+1; j++)
if(Check(a[i],a[j]))AddEdge(i,j);
for(int i=1; i<=n; i++)dinic.AddEdge(i<<1,i<<1|1,1);
printf("%d\n",dinic.MaxFlow(Start,End));
return 0;
}
姥爷们赏瓶冰阔落吧~