bzoj5037: [Jsoi2014]电信网络

题意

自己看。。

题解

一看就很有网络流的样子。。
代价最小,不就是最小割嘛
我们将节点黑白染色,正权为白,反之为黑
建图方法如下:
S—–白点
黑点——>T
然后能影响到的点连一下就好了。。
至于权值就是他们的S(或相反数)

CODE:

#include<cstdio>
#include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=505;
const int MAX=1<<30;
int X[N],Y[N],R[N],S[N];
int n;
struct qq{int x,y,z,last;}s[N*N*2];
int num,last[N];
int st,ed;
void init (int x,int y,int z)
{
    num++;
    s[num].x=x;s[num].y=y;s[num].z=z;
    s[num].last=last[x];
    last[x]=num;
    swap(x,y);z=0;
    num++;
    s[num].x=x;s[num].y=y;s[num].z=z;
    s[num].last=last[x];
    last[x]=num;
}
int ans=0;
int dis (int x,int y){return (X[x]-X[y])*(X[x]-X[y])+(Y[x]-Y[y])*(Y[x]-Y[y]);}
int h[N];
bool Bt ()
{
    memset(h,-1,sizeof(h));h[st]=1;
    queue<int> q;
    q.push(st);
    while (!q.empty())
    {
        int x=q.front();q.pop();
        for (int u=last[x];u!=-1;u=s[u].last)
        {
            int y=s[u].y;
            if (s[u].z>0&&h[y]==-1)
            {
                h[y]=h[x]+1;
                q.push(y);
            }
        }
    }
    return h[ed]!=-1;
}
int mymin (int x,int y){return x<y?x:y;}
int dfs (int x,int f)
{
    if (x==ed) return f;
    int s1=0;
    for (int u=last[x];u!=-1;u=s[u].last)
    {
        int y=s[u].y;
        if (s[u].z>0&&h[y]==h[x]+1&&s1<f)
        {
            int lalal=dfs(y,mymin(s[u].z,f-s1));
            s1+=lalal;
            s[u].z-=lalal;
            s[u^1].z+=lalal;
        }
    }
    if (s1==0) h[x]=-1;
    return s1;
}
int main()
{
    num=1;memset(last,-1,sizeof(last));
    scanf("%d",&n);st=n+1;ed=st+1;
    for (int u=1;u<=n;u++)  
    {
        scanf("%d%d%d%d",&X[u],&Y[u],&R[u],&S[u]);
        if (S[u]>0) {ans=ans+S[u];init(st,u,S[u]);}
        else init(u,ed,-S[u]);
    }
    for (int u=1;u<=n;u++)
        for (int i=1;i<=n;i++)
        {
            if (u==i) continue;
            if (dis(u,i)<=R[u]*R[u]) init(u,i,MAX);
        }
    /*for (int u=2;u<=num;u+=2)
        printf("%d %d %d\n",s[u].x,s[u].y,s[u].z);*/
    while (Bt()==true) ans=ans-dfs(st,MAX);
    printf("%d\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值