洛谷P3356 火星探险问题
思路:
这题建图并不是很难,和洛谷P2045 方格取数加强版很相似。
将一个点
(
i
,
j
)
(i,j)
(i,j)拆成入点
(
i
,
j
)
(i,j)
(i,j)和出点
(
i
,
j
)
′
(i,j)^{'}
(i,j)′。
1、
S
S
S向
(
1
,
1
)
(1,1)
(1,1)建流量为
k
k
k,费用为
0
0
0的边。
2、
(
n
,
m
)
′
(n,m)^{'}
(n,m)′向
T
T
T建流量为
k
k
k,费用为
0
0
0的边。
3、每个点对向东或者向南可以走的点连流量为
i
n
f
inf
inf,费用为
0
0
0的边。
4、每个点
(
i
,
j
)
(i,j)
(i,j)向
(
i
,
j
)
′
(i,j)^{'}
(i,j)′连流量为
i
n
f
inf
inf,费用为
0
0
0的边。
5、对于石块的点,
(
i
,
j
)
(i,j)
(i,j)向
(
i
,
j
)
′
(i,j)^{'}
(i,j)′连流量为
1
1
1,费用为
−
1
-1
−1的边。
这样就可以用石块的数量作为费用,车辆通行的次数作为流量,通过对数量取反,可以用最小费用最大流求出满足要求的答案。
接下来就是输出
m
a
x
f
l
o
w
maxflow
maxflow辆车的路线,这里看了好多题解才理解一些。
对于
(
i
,
j
)
(i,j)
(i,j)和除了
S
,
T
,
(
i
,
j
)
′
S,T,(i,j)^{'}
S,T,(i,j)′之外的点的边
e
i
e_i
ei。遇到其反向边流量不
e
i
′
{e_i}^{'}
ei′为
0
0
0的情况,就表示
e
i
e_i
ei有流量,
e
i
′
{e_i}^{'}
ei′流量的值表示流过的次数。那么可以从
(
1
,
1
)
(1,1)
(1,1)开始遍历,遇到
e
i
′
{e_i}^{'}
ei′不等于
0
0
0,就判断前进的路线,并且把
e
i
′
{e_i}^{'}
ei′的值减一。
代码:
#include<bits/stdc++.h>
#define pii pair<int,int>
#define ll long long
#define cl(x,y) memset(x,y,sizeof(x))
#define loop(x,y,z) for(x=y;x<=z;x++)
#define reve(x,y,z) for(x=y;x>=z;x--)
#define ct cerr<<"Time elapsed:"<<1.0*clock()/CLOCKS_PER_SEC<<"s.\n";
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define lson x<<1,l,mid
#define rson x<<1|1,mid+1,r
#define INF 1e18
const int N=1e4+10;
const int M=1e6+10;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
const double eps=1e-8;
const double pi=acos(-1);
using namespace std;
struct edge
{
int u,v,f,w;
}e[M];
int head[N],len=1,dis[N],vis[N];
int maxflow,mincost;
int pre[N],path[N];
void add(int u,int v,int f,int w)
{
e[++len]={head[u],v,f,w};
head[u]=len;
}
void inc(int u,int v,int f,int w)
{
add(u,v,f,w);
add(v,u,0,-w);
}
int spfa(int s,int t)
{
cl(dis,inf);
cl(vis,0);
cl(pre,-1);
dis[s]=0;
vis[s]=1;
queue<int>q;
q.push(s);
while(!q.empty())
{
int u=q.front(),i;
q.pop();
vis[u]=0;
for(i=head[u];i;i=e[i].u)
{
int v=e[i].v,w=e[i].w;
if(e[i].f && dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
pre[v]=u;
path[v]=i;
if(!vis[v])
{
q.push(v);
vis[v]=1;
}
}
}
}
return pre[t]!=-1;
}
void ek(int s,int t)
{
while(spfa(s,t))
{
int mi=inf,i;
for(i=t;i!=s;i=pre[i])
mi=min(mi,e[path[i]].f);
for(i=t;i!=s;i=pre[i])
{
e[path[i]].f-=mi;
e[path[i]^1].f+=mi;
}
maxflow+=mi;
mincost+=dis[t]*mi;
}
}
#define id(x,y) (x-1)*m+y
#define ot(x,y) id(x,y)+n*m
int a[110][110],n,m,k,s,t;
void print(int x,int y,int u,int p)
{
int i,px,py,ans;
for(i=head[u];i;i=e[i].u)
{
int v=e[i].v;
if(v==s || v==t || v==u-n*m)
continue;
if(!e[i^1].f)
continue;
//cout<<v<<endl;
e[i^1].f--;
if(v>n*m)
{
print(x,y,v,p);
return;
}
if(v==id(x,y)+1)
{
px=x;
py=y+1;
ans=1;
}
else
{
px=x+1;
py=y;
ans=0;
}
cout<<p<<" "<<ans<<endl;
print(px,py,v+n*m,p);
return;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int i,j;
cin>>k>>m>>n;
s=0;
t=2*n*m+1;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
cin>>a[i][j];
if(a[1][1]!=1)
inc(s,id(1,1),k,0);
if(a[n][m]!=1)
inc(ot(n,m),t,k,0);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
if(a[i][j]==1)
continue;
inc(id(i,j),ot(i,j),inf,0);
if(a[i][j]==2)
inc(id(i,j),ot(i,j),1,-1);
if(i<n && a[i+1][j]!=1)
inc(ot(i,j),id(i+1,j),inf,0);
if(j<m && a[i][j+1]!=1)
inc(ot(i,j),id(i,j+1),inf,0);
}
ek(s,t);
//cout<<maxflow<<endl;
for(i=1;i<=maxflow;i++)
print(1,1,1,i);
return 0;
}