Hdu 4494 Teamwork(最小费用流)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4494

思路:每种属性人互不干扰,跑m次费用流,结果累加。超级源点0,超级汇点2*n-1。将每个点拆成两个点一个为自己,另一个表示可以提供给别人。源点向每个表示自己的点连一条容量为INF,费用为1的边,表示起点有无数人每选择一人需花费1。对于每个拆出来的点,源点向其连一条容量为kind[i](第i中需要人数),费用为0的边。表示当前这个点最多可以提供kind[i]的人,费用为0。对于第i个任务,若worker[i].b+worker[i].p+dist<=worker[j].b,则将i拆出来点向表示j自己的点连边,表示可以提供worker。

#include<cstdio>
#include<queue>
#include<cmath>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define debu
using namespace std;
const int maxn=500;
const int INF=0x3f3f3f3f;
typedef long long LL;
struct Edge
{
    int from,to,cap,flow,cost;
    Edge(int u,int v,int c,int f,int w):from(u),to(v),cap(c),flow(f),cost(w) {}
};
struct Node
{
    int kind[6];
    int b,p,x,y;
};
int s,t,stx,sty;
Node worker[maxn];
vector<Edge> edges;
vector<int> G[maxn];
int n,m,p[maxn],a[maxn];
int inq[maxn],d[maxn];
LL cost;
void init(int n)
{
    edges.clear();
    for(int i=0; i<=n; i++) G[i].clear();
}
void addedges(int from,int to,int cap,int cost)
{
    edges.push_back(Edge(from,to,cap,0,cost));
    edges.push_back(Edge(to,from,0,0,-cost));
    int tot=edges.size();
    G[from].push_back(tot-2);
    G[to].push_back(tot-1);
}
bool BF(int s,int t,int& flow,LL& cost)
{
    //cout<<n<<" "<<m<<endl;
    //cout<<"flag"<<endl;
    for(int i=0; i<=2*n; i++) d[i]=INF;
    memset(inq,0,sizeof(inq));
    d[s]=0,inq[s]=1,p[s]=0;
    queue<int> q;
    q.push(s),a[s]=INF;
    while(!q.empty())
    {
        int u=q.front();
        q.pop(),inq[u]=0;
        for(int i=0; i<G[u].size(); i++)
        {
            Edge& e=edges[G[u][i]];
            if(e.cap>e.flow&&d[e.to]>d[u]+e.cost)
            {
                d[e.to]=d[u]+e.cost;
                p[e.to]=G[u][i];
                a[e.to]=min(a[u],e.cap-e.flow);
                if(!inq[e.to])
                {
                    q.push(e.to);
                    inq[e.to]=1;
                }
            }
        }
    }
    if(d[t]==INF) return false;
    flow+=a[t];
    cost+=(LL)d[t]*(LL)a[t];
    for(int u=t; u!=s; u=edges[p[u]].from)
    {
        edges[p[u]].flow+=a[t];
        edges[p[u]^1].flow-=a[t];
    }
    return true;
}
int mincostmaxflow(int s,int t,LL& cost)
{
    int flow=0;
    cost=0;
    while(BF(s,t,flow,cost));
    return cost;
}
double dist(Node a,Node b)
{
    return sqrt(1.0*(a.x-b.x)*(a.x-b.x)+1.0*(a.y-b.y)*(a.y-b.y));
}
void make(int x)
{
    init(2*n);
    for(int i=1; i<=n-1; i++)
    {
        addedges(s,i,INF,1);
        addedges(i,t,worker[i].kind[x],0);
        addedges(s,i+n-1,worker[i].kind[x],0);
        //cout<<s<<" "<<i<<" "<<INF<<" "<<1<<endl;
        //cout<<i<<" "<<t<<" "<<worker[i].kind[x]<<" "<<0<<endl;
        //cout<<s<<" "<<i+n-1<<" "<<worker[i].kind[x]<<" "<<0<<endl;
        for(int j=1; j<=n-1; j++)
        {
            if(i==j) continue;
            double dis=dist(worker[i],worker[j]);
            if(worker[i].b+worker[i].p+dis<=worker[j].b)
            {
                //cout<<i+n-1<<" "<<j<<" "<<INF<<" "<<0<<endl;
                addedges(i+n-1,j,INF,0);
            }
        }
    }
    //cout<<"flag"<<endl;
}
void solve()
{
    int ans=0;
    for(int i=1; i<=m; i++)
    {
        make(i);
        //cout<<s<<" "<<t<<endl;
        ans+=mincostmaxflow(s,t,cost);
    }
    printf("%d\n",ans);
}
int main()
{
#ifdef debug
    freopen("in.in","r",stdin);
#endif // debug
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d%d",&n,&m);
        scanf("%d%d",&stx,&sty);
        s=0,t=2*(n-1)+1;
        //cout<<s<<" "<<t<<endl;
        for(int i=1; i<=n-1; i++)
        {
            scanf("%d%d%d%d",&worker[i].x,&worker[i].y,&worker[i].b,&worker[i].p);
            for(int j=1; j<=m; j++)
                scanf("%d",&worker[i].kind[j]);
        }
        solve();
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值