hdu4289 最小割最大流 (拆点最大流)

最小割最大流定理:(参考刘汝佳p369)增广路算法结束时,令已标号结点(a[u]>0的结点)集合为S,其他结点集合为T=V-S,则(S,T)是图的s-t最小割。

 

Problem Description
  You, the head of Department of Security, recently received a top-secret information that a group of terrorists is planning to transport some WMD 1 from one city (the source) to another one (the destination). You know their date, source and destination, and they are using the highway network.   The highway network consists of bidirectional highways, connecting two distinct city. A vehicle can only enter/exit the highway network at cities only.   You may locate some SA (special agents) in some selected cities, so that when the terrorists enter a city under observation (that is, SA is in this city), they would be caught immediately.   It is possible to locate SA in all cities, but since controlling a city with SA may cost your department a certain amount of money, which might vary from city to city, and your budget might not be able to bear the full cost of controlling all cities, you must identify a set of cities, that:   * all traffic of the terrorists must pass at least one city of the set.   * sum of cost of controlling all cities in the set is minimal.   You may assume that it is always possible to get from source of the terrorists to their destination. ------------------------------------------------------------ 1 Weapon of Mass Destruction
 

 

Input
  There are several test cases.   The first line of a single test case contains two integer N and M ( 2 <= N <= 200; 1 <= M <= 20000), the number of cities and the number of highways. Cities are numbered from 1 to N.   The second line contains two integer S,D ( 1 <= S,D <= N), the number of the source and the number of the destination.   The following N lines contains costs. Of these lines the ith one contains exactly one integer, the cost of locating SA in the ith city to put it under observation. You may assume that the cost is positive and not exceeding 107.   The followingM lines tells you about highway network. Each of these lines contains two integers A and B, indicating a bidirectional highway between A and B.   Please process until EOF (End Of File).
 

 

Output
  For each test case you should output exactly one line, containing one integer, the sum of cost of your selected set.   See samples for detailed information.
 

 

Sample Input
5 6 5 3 5 2 3 4 12 1 5 5 4 2 3 2 4 4 3 2 1
 

 

Sample Output
3

 

大致题意:给出一个由n个点,m条组成的无向图,给出两个点是s,t。对于图中的每个点,去掉这个点都需要一定的花费,求至少多少花费才能使s和t之间不连通。     

思路:最基础的拆点最大流,把每个点拆作两个点i和i0,连接 I——>I0费用为去掉这个点的花费,如果原图中有一条边a和b,则连接a0和b0。(总之这四个点连完之后必须全部在环上)对图求最大流即可。

//这道题跨越了快一个月的时间,终于搞懂了,好开心—2016.9.9 ^_^。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
#define INF 0x7fffffff

struct Edge
{
    int st,ed;
    int c;
    int next;
} edge[255025];

int N,M,St,Ed;
int d[505],head[505];
int I;

void Addedge(int u,int v,int c)
{
    edge[I].st=u;
    edge[I].ed=v;
    edge[I].c=c;
    edge[I].next=head[u];
    head[u]=I++;

    edge[I].st=v;
    edge[I].ed=u;
    edge[I].c=0;
    edge[I].next=head[v];
    head[v]=I++;
}

bool bfs()
{
    memset(d,-1,sizeof(d));
    int cur;
    queue<int>q;
    d[St]=0;
    q.push(St);
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        if(cur==Ed+N) return true;
        for(int i=head[cur]; i!=-1; i=edge[i].next)
        {
            if(d[edge[i].ed]==-1 && edge[i].c>0)
            {
                d[edge[i].ed]=d[cur]+1;
                q.push(edge[i].ed);
            }
        }
    }
    return false;
}

int dinic(int n,int flow)
{
    if(n==Ed+N) return flow;
    int a,mflow=0;
    for(int i=head[n]; i!=-1; i=edge[i].next)
    {
        if(d[edge[i].ed]==d[n]+1 && edge[i].c)
        {
            a=dinic(edge[i].ed, min(flow-mflow,edge[i].c));
            edge[i].c -= a;
            edge[i^1].c+=a;
            mflow+=a;
            if(mflow==flow) break;
        }
    }
    if(mflow==0) d[n]=-1;
    return mflow;
}

int main()
{
    int a,b,x;
    while(scanf("%d%d",&N,&M)!=EOF)
    {
        scanf("%d%d",&St,&Ed);
        memset(head,-1,sizeof(head));
        I=0;
        for(int i=1; i<=N; i++)
        {
            scanf("%d",&x);
            Addedge(i,i+N,x);
        }
        for(int i=1; i<=M; i++)
        {
            scanf("%d%d",&a,&b);
            Addedge(N+a,b,INF);
            Addedge(N+b,a,INF);
        }
        int ans=0;
        while(bfs())
            ans+=dinic(St,INF);
        printf("%d\n",ans);
    }
    return 0;
}

对简单的dinic再进一步优化。

 

转载于:https://www.cnblogs.com/a-clown/p/5917705.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值