http://www.lydsy.com/JudgeOnline/problem.php?id=1001

 

解法1:直接最小割dinic || isap ,据说会卡掉某些人写的丑的dinic

 

/**************************************************************
    Problem: 1497
    User: 11101001
    Language: C++
    Result: Accepted
    Time:576 ms
    Memory:15356 kb
****************************************************************/
 
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn = 600001;
const int INF = 0x7fffffff;
int n,m,dis[maxn],head[maxn],que[maxn];
struct node{
    int v,w,next;
}edge[maxn];
int ans,tmp,S,T,num=1;
inline void Add_Edge(int u,int v,int w)
{
    edge[++num].v=v;edge[num].w=w;edge[num].next=head[u];head[u]=num;
    edge[++num].v=u;edge[num].next=head[v];head[v]=num;
}
bool bfs()
{
    memset(dis,-1,sizeof(dis));
    int h=0,t=1;
    que[h]=dis[0]=0;
    while(h<t)
    {
        int u=que[h++];
        if(h==maxn)h=0;
        for(int i=head[u];i;i=edge[i].next)
        {
            int v=edge[i].v;
            if(edge[i].w&&dis[v]<0)
            {
                que[t++]=v;
                dis[v]=dis[u]+1;
                if(t==maxn)t=0;
            }
        }
    }
    if(dis[T]==-1)return false;
    return true;
}
int dfs(int x,int f)
{
    if(x==T) return f;
    int mn=0,w;
    for(int i=head[x];i;i=edge[i].next)
    {
        if(edge[i].w&&dis[edge[i].v]==dis[x]+1)
        {
            w=f-mn;
            w=dfs(edge[i].v,min(w,edge[i].w));
            edge[i].w-=w;
            edge[i^1].w+=w;
            mn+=w;
            if(mn==f)return f;
        }   
    }
    if(!mn)dis[x]=-1;
    return mn;
}
void dinic()
{
    while(bfs())
        ans+=dfs(S,INF);
}
int main()
{
    S=0;
    scanf("%d%d",&n,&m);T=n+m+1;
    for(int a,i=1;i<=n;i++){
        scanf("%d",&a);
        Add_Edge(S,i,a);
    }
    for(int a,b,c,i=1;i<=m;i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        Add_Edge(a,n+i,INF);
        Add_Edge(b,n+i,INF);
        Add_Edge(n+i,T,c);
        tmp+=c;
    }
    dinic();
//  printf("%d %d\n",tmp,ans);
    printf("%d\n",tmp-ans);
    return 0;
}
Dinic

相关文章:

  • 2022-12-23
  • 2021-09-24
  • 2021-11-24
  • 2021-10-09
  • 2021-12-29
猜你喜欢
  • 2021-06-20
  • 2021-05-19
  • 2022-02-12
  • 2022-01-12
相关资源
相似解决方案