利用最大流标号法求解最大流,详见代码:

Version:未加头尾节点版;

缺点:havn't take nodes' pixels into consideration

/************************************************************************/
/* MaxFlow solve graph cut program */
/************************************************************************/
/*
File description:
This program for graph cut based on Ford - Fulkerson Algorithm.
Input:
M(edge number) N(node number)
then M lines input 3 parameters each line:
start_point end_point edge_capacity
e.g 5 4 1 4 40 1 2 20 2 4 10 4 3 30 3 2 10
output:
Line 1:maxflow value
Line2:nodes in Class 1
e.g
MAX Flow is 50
nodes in class S: 1 2
=========================================================================
CreateTime:2011-8-8
Author:@Zhang Ruiqing
*/
#include<iostream>
#include<queue>
using namespace std;
#define N 250//point
#define M 250*250//edge
#define INF 1000000000
#define min(a,b) a<b?a:b int pre[N],map[N][N];
int minlen[N];//minlen[i] represents min length from s to i
int pathmin[N];//min flow in this path from i to t
queue<int>Q; int n,m,s,t; void init(int s)
{
memset(pre,,sizeof(pre));
for(int i=;i<=n;i++)
minlen[i]=pathmin[i]=INF;
minlen[s]=;
} bool bfs(int s,int t)
{
init(s);
//push start node
Q.push(s);
while(!Q.empty())
{
int now=Q.front();
Q.pop();
for(int i=;i<=n;i++)
{
if(map[now][i]!=&&minlen[now]+map[now][i]<minlen[i])
{
minlen[i]=minlen[now]+map[now][i];
pre[i]=now;
pathmin[i]=min(pathmin[now],map[now][i]);
Q.push(i);
}
}
}
if(minlen[n]==INF)
return false;
return true;
} int max_flow(int s,int t)
{
int res=;
while(bfs(s,t))//if can find an augment road
{
int minflow=pathmin[n];//minimal flow in the path
int point=t;//calculate from end point t to start point s
while(point!=s)
{
int prep=pre[point];
map[prep][point]-=minflow;//positive road -= flow
map[point][prep]+=minflow;//nagative road+= flow
point=prep;
}
res+=minflow;
}
return res;
} int main()
{
int i,j;
int a,b,c;
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(map,,sizeof(map));
for(i=;i<m;i++)
{
cin>>a>>b>>c;
map[a][b]+=c;
}
s=;
t=n;
cout<<"MAX Flow is "<<max_flow(s,t)<<endl; cout<<"nodes in class S: 1 ";
for(i=;i<n;i++)
{
if(pathmin[i]!=INF)
cout<<i<<" ";
}
cout<<endl;
}
return ;
}

from: http://blog.csdn.net/abcjennifer/article/details/6668913

图割Graph-Cut的最大流实现的更多相关文章

  1. 图像分割之(二)Graph Cut(图割)

    zouxy09@qq.com http://blog.csdn.net/zouxy09 上一文对主要的分割方法做了一个概述.那下面我们对其中几个比较感兴趣的算法做个学习.下面主要是Graph Cut, ...

  2. GrabCut in One Cut(基于图割算法grabcut的一次快速图像分割的OpenCV实现)----目前效果最好的图割

     One cut in grabcut(grabcut算法的非迭代实现?) 本文针对交互式图像分割中的图割算法,主要想翻译一篇英文文献.不足之处请大家指正. 这是博主近期看到的效果最好,实现最简单 ...

  3. Graph Cut and Its Application in Computer Vision

    Graph Cut and Its Application in Computer Vision 原文出处: http://lincccc.blogspot.tw/2011/04/graph-cut- ...

  4. Graph Cut

    转自:http://blog.csdn.net/zouxy09/article/details/8532111 Graph Cut,下一个博文我们再学习下Grab Cut,两者都是基于图论的分割方法. ...

  5. 图像分割之(三)从Graph Cut到Grab Cut

    zouxy09@qq.com http://blog.csdn.net/zouxy09 上一文对GraphCut做了一个了解,而现在我们聊到的GrabCut是对其的改进版,是迭代的Graph Cut. ...

  6. 基于GraphCuts图割算法的图像分割----OpenCV代码与实现

    转载请注明出处:http://blog.csdn.net/wangyaninglm/article/details/44151213, 来自:shiter编写程序的艺术 1.绪论 图切割算法是组合图论 ...

  7. 看开源代码利器—用Graphviz + CodeViz生成C/C++函数调用图(call graph)

    一.Graphviz + CodeViz简单介绍 CodeViz是<Understanding The Linux Virtual Memory Manager>的作者 Mel Gorma ...

  8. 图数据库(graph database)资料收集和解析 - daily

    Motivation 图数据库中的高科技和高安全性中引用了一个关于图数据库(graph database)的应用前景的乐观估计: 预计到2017年,图数据库产业在数据库市场的份额将从2个百分点增长到2 ...

  9. 分析函数调用关系图(call graph)的几种方法

    绘制函数调用关系图对理解大型程序大有帮助.我想大家都有过一边读源码(并在头脑中维护一个调用栈),一边在纸上画函数调用关系,然后整理成图的经历.如果运气好一点,借助调试器的单步跟踪功能和call sta ...

随机推荐

  1. 【POJ】1160 Post Office

    http://poj.org/problem?id=1160 题意:直线上有n个城市,其中有p个城市有邮局,问如何建p个邮局使得每个城市到最近的邮局和最小.(n<=300, p<=30&a ...

  2. Codeforces Round #157 (Div. 2) D. Little Elephant and Elections(数位DP+枚举)

    数位DP部分,不是很难.DP[i][j]前i位j个幸运数的个数.枚举写的有点搓... #include <cstdio> #include <cstring> using na ...

  3. InterBase数据库迁移到MySQL(数据导出)

    这篇我将记叙我的第二个脚本程序,这篇我使用InterBase数据库提供的“isql”命令来导出我所要的数据,但是由于“isql”命令没有直接导出数据的语句,说以我采用的是导入一个配置文件,在这个文件中 ...

  4. android中利用Socket实现手机客户端与PC端进行通信

    1. 项目截图

  5. 显式激活数据库( ACTIVATE DATABASE)

    某天值班员联系我说,我负责的一套报送系统没有按时生成报文,因为此报警提前量比较大,加上系统经常发生未按时生成报文的事件,也就是没在意,然后不急不慢的到公司,打开系统页面,发现其中一个存储过程跑了将近8 ...

  6. jquery.autocomplete在火狐下的BUG解决

    该插件在火狐下使用拼音输入法输入,回车后无法即使响应事件,需要再输入别的字母才会响应事件     主要原因:   该插件响应主要监听了keydown事件   其他浏览器在打开中文输入法的情况下输入的拼 ...

  7. 华清远见金牌讲师名家大讲堂Android开发篇成功举办

    2014年3月5日.12日华清远见金牌讲师名家大讲堂(以下简称名家大讲堂)在线讲座全新升级开讲,至此拉开了新一年名家大讲堂的序幕! 华清远见名家大讲堂作为业内颇具影响力的公益免 费线上课程,自2009 ...

  8. FLEX SharedObject介绍及应用

    ShareObject介绍: 1 ShareObject,顾名思义共享对象,而通常意义上的共享,从B/S结构上来讲,无非是客户端(浏览器端)的共享和服务器端的共享了,不错,ShareObject刚好份 ...

  9. ZeroMQ实例-使用ZeroMQ进行windows与linux之间的通信

    1.本文包括 1)在windows下使用ZMQ 2)在windows环境下与Linux环境下进行网络通信 2.在Linux下使用ZMQ 之前写过一篇如何在Linux环境下使用ZMQ的文章 <Ze ...

  10. Jquery Ajax方法传值到action

    假设cshtml文件中是这样的: <script type="text/javascript"> $(document).ready(function(){ $(&qu ...