2012 If this is the end of the world how to do? I do not know how. But now scientists have found that some stars, who can live, but some people do not fit to live some of the planet. Now scientists want your help, is to determine
what all of people can live in these planets.
The last line has m digits, the ith digit ai indicates the ith planet can contain ai people most..
0 <= ai <= 100000
If you can output YES, otherwise output NO.
1 1 1 1 2 2 1 0 1 0 1 1
YES NO最大流
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod=609929123;
const int N=100220;
int frist[N];
int current[N];
int dis[N];
char mapp[100][100];
char mapp1[100][100];
int mp[100][100];
int number[100][100];
int R,S,T,t,n,d,m,cnt;
struct node
{
int v,cap,next;//领接表
}es[4*N];
int addedge(int u,int v,int cap)
{
node e1={v,cap,frist[u]};
es[R]=e1;
frist[u]=R++;
node e2={u,0,frist[v]};
es[R]=e2;
frist[v]=R++;
}
int bfs()
{
queue<int>q;
q.push(S);
memset(dis,-1,sizeof(dis));
dis[S]=0;
while(!q.empty())
{
int h=q.front();
if(h==T) return 1;
q.pop();
for(int i=frist[h];i!=-1;i=es[i].next)
{
int temp=es[i].v;
if(dis[temp]==-1&&es[i].cap)
{
dis[temp]=dis[h]+1;
q.push(temp);
}
}
}
return 0;
}
int dinic(int x,int maxflow)
{
if(x==T) return maxflow;
int flow,f=0;
for(int &i=current[x];i!=-1;i=es[i].next)
{
int temp=es[i].v;
if(dis[temp]==dis[x]+1&&es[i].cap)
{
flow=dinic(temp,min(maxflow-f,es[i].cap));
es[i].cap-=flow;
es[i^1].cap+=flow;
f+=flow;
if(f==maxflow) break;
}
}
return f;
}
int DINIC()
{
int ans=0;
while(bfs())
{
int flow;
memcpy(current,&frist,sizeof(frist));//复制
while(flow=dinic(S,inf))
ans+=flow;
}
return ans;
}
int main()
{
scanf("%d",&t);
int o=0;
while(t--)
{
memset(frist,-1,sizeof(frist));
memset(mapp,0,sizeof(mapp));
memset(mapp1,0,sizeof(mapp1));
memset(mp,0,sizeof(mp));
memset(number,0,sizeof(number));
R=0,S=0;
cnt=0;
scanf("%d%d",&n,&d);
for(int i=1;i<=n;i++)
{
scanf("%s",mapp[i]+1);
m=strlen(mapp[i]+1);
for(int j=1;j<=m;j++)
{
mp[i][j]=mapp[i][j]-'0';
number[i][j]=++cnt;
}
}
T=2*cnt+1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(mp[i][j])
{
if(i<=d||j<=d||i+d>n||j+d>m)
addedge(number[i][j]+cnt,T,inf);
addedge(number[i][j],number[i][j]+cnt,mp[i][j]);
for(int k=1;k<=n;k++)
{
for(int p=1;p<=m;p++)
{
int dx=abs(i-k);
int dy=abs(j-p);
double mm=sqrt(dx*dx*1.0+dy*dy*1.0);
if(mm>d) continue;
addedge(number[i][j]+cnt,number[k][p],inf);
}
}
}
}
}
int ll=0;
for(int i=1;i<=n;i++)
{
scanf("%s",mapp1[i]+1);
for(int j=1;j<=m;j++)
{
if(mapp1[i][j]=='L')
{
ll++;
addedge(0,number[i][j],1);
}
}
}
ll-=DINIC();
if(ll==0)
printf("Case #%d: no lizard was left behind.\n",++o);
else if(ll==1)
printf("Case #%d: 1 lizard was left behind.\n",++o);
else
printf("Case #%d: %d lizards were left behind.\n",++o,ll);
}
}
本文介绍了一种算法,用于解决特定条件下人类如何在不同星球上生存的问题。通过建立复杂的图模型并运用最大流算法,该算法能够判断所有人类是否都能找到合适的居住星球。涉及的数据结构包括图、队列等,算法应用了深度优先搜索和广度优先搜索。
4021

被折叠的 条评论
为什么被折叠?



