hdu 3870

这些天做的很多题都没贴代码。

这是一道平面图最小割的题,贴下模板吧


#include<stdio.h>
#include<string.h>
const int MAXN= 610;
const __int64 maxint = 1000000000000000ll;
struct edge
{
    int x,ne;
    __int64 cost;
} e[8*MAXN*MAXN];
int st, ed, te, top, hd[4*MAXN*MAXN],rr[4*MAXN*MAXN];
__int64 heap[4*MAXN*MAXN],d[4*MAXN*MAXN],r[4*MAXN*MAXN],tmp;
void link(int x,int y,__int64 cost)
{
    te++;
    e[te].x=y;
    e[te].ne=0;
    e[te].cost=cost;
    if (hd[x])
    {
        e[rr[x]].ne=te;
        rr[x]=te;
    }
    else rr[x]=hd[x]=te;
    te++;
    e[te].x=x;
    e[te].ne=0;
    e[te].cost=cost;
    if (hd[y])
    {
        e[rr[y]].ne=te;
        rr[y]=te;
    }
    else rr[y]=hd[y]=te;
}
__int64 pop()
{
    __int64 t=heap[1];
    int i=1,flag=1;
    r[1]=r[heap[top]];
    heap[1]=heap[top--];
    //trail[pt++]=t;
    while (flag)
    {
        flag=0;
        if (top>=i*2+1 && d[heap[i]]>d[heap[i*2+1]] && d[heap[i*2]]>d[heap[i*2+1]])
        {
            flag=1;
            tmp=r[heap[i]];
            r[heap[i]]=r[heap[i*2+1]];
            r[heap[i*2+1]]=tmp;
            tmp=heap[i];
            heap[i]=heap[i*2+1];
            heap[i*2+1]=tmp;
            i=i*2+1;
        }
        else if (top>=i*2 && d[heap[i]]>d[heap[i*2]])
        {
            flag=1;
            tmp=r[heap[i]];
            r[heap[i]]=r[heap[i*2]];
            r[heap[i*2]]=tmp;
            tmp=heap[i];
            heap[i]=heap[i*2];
            heap[i*2]=tmp;
            i=i*2;
        }
    }
    return t;
}
void dij()
{
    int i,j;
    __int64 ii, k;
    top=ed+1;
    for(i=0; i<=top; i++)
    {
        heap[i+1]=i;
        d[i]=maxint;
        r[i]=i+1;
    }
    d[0]=0;
    j=hd[k=st];
    while (j)
    {
        if (d[e[j].x]>d[k]+e[j].cost)
        {
            d[e[j].x]=d[k]+e[j].cost;
            ii=r[e[j].x];
            while (ii!=1 && d[heap[ii]]<d[heap[ii/2]])
            {
                tmp=r[heap[ii]];
                r[heap[ii]]=r[heap[ii/2]];
                r[heap[ii/2]]=tmp;
                tmp=heap[ii];
                heap[ii]=heap[ii/2];
                heap[ii/2]=tmp;
                ii/=2;
            }
        }
        j=e[j].ne;
    }
    for (i=st; i<=ed; i++)
    {
        k=pop();
        j=hd[k];
        while (j)
        {
            if (d[e[j].x]>d[k]+e[j].cost)
            {
                d[e[j].x]=d[k]+e[j].cost;
                ii=r[e[j].x];
                while (ii!=1 && d[heap[ii]]<d[heap[ii/2]])
                {
                    tmp=r[heap[ii]];
                    r[heap[ii]]=r[heap[ii/2]];
                    r[heap[ii/2]]=tmp;
                    tmp=heap[ii];
                    heap[ii]=heap[ii/2];
                    heap[ii/2]=tmp;
                    ii/=2;
                }
            }
            j=e[j].ne;
        }
    }
    printf("%I64d\n",d[ed]);
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int n;
        scanf("%d", &n);
        te = 0;
        memset(hd, 0, sizeof(hd));
        st = 0;
        ed = (n - 1) * (n - 1) + 1;
        int a;
        for(int i = 0; i < n; i++)
          for(int j = 0; j < n; j++)
          {
              scanf("%d", &a);
              if(i == n - 1 && j == n - 1) continue;
              if(i != n - 1 && j != n - 1)
              {
                  int p = i * (n - 1) + j + 1;
                  if(i == 0) link(st, p, a);
                  else link(p - n + 1, p, a);
                  if(j == 0) link(p, ed, a);
                  else link(p - 1, p , a);
              }
              else
              {
                  if(i == n - 1) link((i - 1) * (n - 1) + j + 1, ed, a);
                  if(j == n - 1) link(st, i * (n - 1) + j, a);
              }
          }
        dij();
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值