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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值