HDU 3987 && HDU 6214(最小割的边数)

HDU 3987

HDU 6214

两个题目都差不多,都是求最小割的最少割边数量,HDU 3987分了边的方向,但是也一样

对于每一个边,将它的容量变为w=w*(maxm+1)+1;因为最小割的容量最后都是一样的,+1是为了算出边数,如果不加一,最后就是跑出一个最小割,因为最小割是相同的,所以不+1的话本质上是没有区别的,但是+1以后,边数少的会变成最小割,最后跑出来的答案对maxm+1取模以后就是最小割的边数了

HDU 3987

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const int maxn=2e5+7;
const ll maxm=1e5+7;
const ll inf=0x3f3f3f3f3f3f3f3f;
int source,sink;
struct Node
{
    int to;
    int next;
    ll capa;
}edge[maxn<<2];
int cnt;
int dep[maxn];
int head[maxn];
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
    return;
}
void add(int u,int v,ll capa)
{
    edge[cnt].to=v;
    edge[cnt].capa=capa;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    edge[cnt].to=u;
    edge[cnt].capa=0;
    edge[cnt].next=head[v];
    head[v]=cnt++;
    return;
}
bool bfs()
{
    queue<int> que;
    memset(dep,-1,sizeof(dep));
    dep[source]=0;
    que.push(source);
    while(!que.empty())
    {
        int node=que.front();
        que.pop();
        for(int i=head[node];~i;i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].capa&&dep[v]==-1)
            {
                dep[v]=dep[node]+1;
                if(v==sink) return true;
                que.push(v);
            }
        }
    }
    return false;
}
ll dfs(int node,ll minn)
{
    if(node==sink||minn==0)
    {
        return minn;
    }
    ll r=0;
    for(int i=head[node];~i;i=edge[i].next)
    {
        int v=edge[i].to;
        if(edge[i].capa>0&&dep[v]==dep[node]+1)
        {
            ll tmp=dfs(v,min(edge[i].capa,minn));
            if(tmp>0)
            {
                edge[i].capa-=tmp;
                edge[i^1].capa+=tmp;
                r+=tmp;
                minn-=tmp;
                if(!minn) break;
            }
        }
    }
    if(!r) dep[node]=-1;
    return r;
}
ll dinic()
{
    ll ans=0;
    while(bfs())
    {
        ans+=dfs(source,inf);
    }
    return ans;
}
int main()
{
    int test;
    scanf("%d",&test);
    for(int cas=1;cas<=test;cas++)
    {
        init();
        int n,m;
        scanf("%d%d",&n,&m);
        source=0;
        sink=n-1;
        for(int i=0;i<m;i++)
        {
            int u,v,d;
            ll c;
            scanf("%d%d%lld%d",&u,&v,&c,&d);
            add(u,v,c*(maxm+1)+1);
            if(d==1) add(v,u,c*(maxm+1)+1);
        }
        ll ans=dinic();
        printf("Case %d: %lld\n",cas,ans%(maxm+1));
    }
    return 0;
}

HDU 6214

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const int maxn=2e5+7;
const ll maxm=1e5+7;
const ll inf=0x3f3f3f3f3f3f3f3f;
int source,sink;
struct Node
{
    int to;
    int next;
    ll capa;
}edge[maxn<<2];
int cnt;
int dep[maxn];
int head[maxn];
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
    return;
}
void add(int u,int v,ll capa)
{
    edge[cnt].to=v;
    edge[cnt].capa=capa;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    edge[cnt].to=u;
    edge[cnt].capa=0;
    edge[cnt].next=head[v];
    head[v]=cnt++;
    return;
}
bool bfs()
{
    queue<int> que;
    memset(dep,-1,sizeof(dep));
    dep[source]=0;
    que.push(source);
    while(!que.empty())
    {
        int node=que.front();
        que.pop();
        for(int i=head[node];~i;i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].capa&&dep[v]==-1)
            {
                dep[v]=dep[node]+1;
                if(v==sink) return true;
                que.push(v);
            }
        }
    }
    return false;
}
ll dfs(int node,ll minn)
{
    if(node==sink||minn==0)
    {
        return minn;
    }
    ll r=0;
    for(int i=head[node];~i;i=edge[i].next)
    {
        int v=edge[i].to;
        if(edge[i].capa>0&&dep[v]==dep[node]+1)
        {
            ll tmp=dfs(v,min(edge[i].capa,minn));
            if(tmp>0)
            {
                edge[i].capa-=tmp;
                edge[i^1].capa+=tmp;
                r+=tmp;
                minn-=tmp;
                if(!minn) break;
            }
        }
    }
    if(!r) dep[node]=-1;
    return r;
}
ll dinic()
{
    ll ans=0;
    while(bfs())
    {
        ans+=dfs(source,inf);
    }
    return ans;
}
int main()
{
    int test;
    scanf("%d",&test);
    for(int cas=1;cas<=test;cas++)
    {
        init();
        int n,m;
        scanf("%d%d",&n,&m);
        scanf("%d%d",&source,&sink);
        for(int i=0;i<m;i++)
        {
            int u,v;
            ll c;
            scanf("%d%d%lld",&u,&v,&c);
            add(u,v,c*(maxm+1)+1);
        }
        ll ans=dinic();
        printf("%lld\n",ans%(maxm+1));
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值