BZOJ 1927 [Sdoi2010]星际竞速

费用流

我只是想考前复习一下费用流……

显然一个点只会进一次,出一次。那就考虑每一个点的出点抵达哪个点的入点。可以画成一个二分图的形式。一个特别操作的是可以直接跳到一个点上,那就建一个附加点使得所有出点连它,它连所有入点即可。

#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 1605
#define M 15005
#define cmin(u,v) ((u)>(v)?(u)=(v):0)
using namespace std;
namespace runzhe2000
{
    const int S = N-2, T = N-1, U = N-3, INF = 1<<29;
    int n, m, ecnt = 1, last[N], q[N], dis[N], from[N], a[N], ans, inq[N];
    struct edge{int next, to, flow, val;}e[M*2+N*8];
    void addedge(int a, int b, int flow, int val)
    {
        e[++ecnt] = (edge){last[a], b, flow, val};
        last[a] = ecnt;
        e[++ecnt] = (edge){last[b], a, 0, -val};
        last[b] = ecnt;
    }
    int EK()
    {
        memset(dis, 63, sizeof(dis));
        dis[S] = 0; from[T] = 0; q[0] = S;
        for(int head=0, tail=1;; head++)
        {
            if(head == N) head = 0; 
            if(head == tail) break;
            int x = q[head]; inq[x] = 0;
            for(int i = last[x]; i; i = e[i].next) if(e[i].flow)
            {
                int y = e[i].to;
                if(dis[x] + e[i].val < dis[y])
                {
                    dis[y] = dis[x] + e[i].val;
                    from[y] = i;
                    if(!inq[y])q[tail++] = y, inq[y] = 1;
                    if(tail == N) tail = 0;
                }
            }
        }
        if(!from[T]) return 0; int f = INF;
        for(int i = from[T]; i; i = from[e[i^1].to]) cmin(f, e[i].flow); ans += f * dis[T];
        for(int i = from[T]; i; i = from[e[i^1].to]) e[i].flow -= f, e[i^1].flow += f;
        return 1;
    }
    void main()
    {
        scanf("%d%d",&n,&m);
        for(int i = 1; i <= n; i++) 
        {
            scanf("%d",&a[i]);
            addedge(S, i, 1, 0);
            addedge(i+n, T, 1, 0);
            addedge(i, U, 1, 0);
            addedge(U,i+n, 1, a[i]);
        }
        for(int i = 1, a, b, c; i <= m; i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            if(a > b) swap(a, b);
            addedge(a, b+n, 1, c);
        }
        for(;EK(););
        printf("%d\n",ans);
    }
}
int main()
{
    runzhe2000::main();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值