BZOJ1693Asteroids

1693: [Usaco2007 Demo]Asteroids
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 219 Solved: 163
Description
Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1 <= K <= 10,000), which are conveniently located at the lattice points of the grid. Fortunately, Bessie has a powerful weapon that can vaporize all the asteroids in any given row or column of the grid with a single shot. This weapon is quite expensive, so she wishes to use it sparingly. Given the location of all the asteroids in the field, find the minimum number of shots Bessie needs to fire to eliminate all of the asteroids.
Input
* Line 1: Two integers N and K, separated by a single space. * Lines 2..K+1: Each line contains two space-separated integers R and C (1 <= R, C <= N) denoting the row and column coordinates of an asteroid, respectively.
Output
* Line 1: The integer representing the minimum number of times Bessie must shoot.
Sample Input
3 4
1 1
1 3
2 2
3 2
INPUT DETAILS:
The following diagram represents the data, where “X” is an
asteroid and “.” is empty space:
X.X
.X.
.X.
Sample Output
2
OUTPUT DETAILS:
Bessie may fire across row 1 to destroy the asteroids at (1,1) and (1,3), and then she may fire down column 2 to destroy the asteroids at (2,2) and (3,2).
Source
Gold
源点为0,汇点为2*n+1。。
每一行与源点连边,流量为1
每一列与汇点连边,流量为1
输入的行与列连边,流量为1
然后跑dinic+当前弧即可

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
struct node
{
    int to,next,v;
};
node edge[250001];
int n,m,h[250001],head,tail,dis[250001],q[250001],ans,sum=0,cnt=1;
bool used[250001];

int read()
{
    int w=0,c=1;
    char ch=getchar();
    while (ch<'0' || ch>'9')
      {
        if (ch=='-')
          c=-1;
        ch=getchar();
      }
    while (ch>='0' && ch<='9')
      {
        w=w*10+ch-'0';
        ch=getchar();
      }
    return w*c;
}

void add(int u,int v,int w)
{
    cnt++;
    edge[cnt].next=h[u];
    h[u]=cnt;
    edge[cnt].to=v;
    edge[cnt].v=w;
}

bool bfs()
{
    int j,p;
    memset(dis,-1,sizeof(dis));
    q[0]=0;
    dis[0]=0;
    head=0;
    tail=1;
    while (head<tail)
      {
        head++;
        j=q[head];
        p=h[j];
        while (p)
          {
            if (dis[edge[p].to]<0 && edge[p].v>0)
              {
                dis[edge[p].to]=dis[j]+1;
                tail++;
                q[tail]=edge[p].to;
              }
            p=edge[p].next;
          }
      }
    if (dis[2*n+1]>0)
      return true;
    else
      return false;
}

int dfs(int x,int f)
{
    int w,used=0,i=h[x];
    if (x==2*n+1)
      return f;
    while (i)
      {
        if (edge[i].v && dis[edge[i].to]==dis[x]+1)
          {
            w=f-used;
            w=dfs(edge[i].to,min(w,edge[i].v));
            edge[i].v-=w;
            edge[i^1].v+=w;
            used+=w;
            if (used==f)
              return f;
          }
        i=edge[i].next;
      }
    if (!used)
      dis[x]=-1;
    return used;
}

int main()
{
    int i,x,y;
    n=read();
    m=read();
    for (i=1;i<=m;i++)
      {
        x=read();
        y=read();
        add(x,y+n,1);//连边的时候把行与列分离开
        add(y+n,x,0);
      }
    for (i=1;i<=n;i++)
      {
        add(0,i,1);
        add(i,0,0);
        add(i+n,2*n+1,1);
        add(2*n+1,i+n,0);
      }
    ans=0;
    while (bfs())
      while (sum=dfs(0,0x7fffffff))
        ans+=sum;
    printf("%d",ans);
    return 0;
}
  • 1
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

打赏
文章很值,打赏犒劳作者一下
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页

打赏

DCrusher

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者