图割Graph-Cut的最大流实现
利用最大流标号法求解最大流,详见代码:
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的最大流实现的更多相关文章
- 图像分割之(二)Graph Cut(图割)
zouxy09@qq.com http://blog.csdn.net/zouxy09 上一文对主要的分割方法做了一个概述.那下面我们对其中几个比较感兴趣的算法做个学习.下面主要是Graph Cut, ...
- GrabCut in One Cut(基于图割算法grabcut的一次快速图像分割的OpenCV实现)----目前效果最好的图割
One cut in grabcut(grabcut算法的非迭代实现?) 本文针对交互式图像分割中的图割算法,主要想翻译一篇英文文献.不足之处请大家指正. 这是博主近期看到的效果最好,实现最简单 ...
- Graph Cut and Its Application in Computer Vision
Graph Cut and Its Application in Computer Vision 原文出处: http://lincccc.blogspot.tw/2011/04/graph-cut- ...
- Graph Cut
转自:http://blog.csdn.net/zouxy09/article/details/8532111 Graph Cut,下一个博文我们再学习下Grab Cut,两者都是基于图论的分割方法. ...
- 图像分割之(三)从Graph Cut到Grab Cut
zouxy09@qq.com http://blog.csdn.net/zouxy09 上一文对GraphCut做了一个了解,而现在我们聊到的GrabCut是对其的改进版,是迭代的Graph Cut. ...
- 基于GraphCuts图割算法的图像分割----OpenCV代码与实现
转载请注明出处:http://blog.csdn.net/wangyaninglm/article/details/44151213, 来自:shiter编写程序的艺术 1.绪论 图切割算法是组合图论 ...
- 看开源代码利器—用Graphviz + CodeViz生成C/C++函数调用图(call graph)
一.Graphviz + CodeViz简单介绍 CodeViz是<Understanding The Linux Virtual Memory Manager>的作者 Mel Gorma ...
- 图数据库(graph database)资料收集和解析 - daily
Motivation 图数据库中的高科技和高安全性中引用了一个关于图数据库(graph database)的应用前景的乐观估计: 预计到2017年,图数据库产业在数据库市场的份额将从2个百分点增长到2 ...
- 分析函数调用关系图(call graph)的几种方法
绘制函数调用关系图对理解大型程序大有帮助.我想大家都有过一边读源码(并在头脑中维护一个调用栈),一边在纸上画函数调用关系,然后整理成图的经历.如果运气好一点,借助调试器的单步跟踪功能和call sta ...
随机推荐
- 【BZOJ1503】 [NOI2004]郁闷的出纳员 splay
splay模板题,都快把我做忧郁了. 由于自己调两个坑点. 1.删除时及时updata 2.Kth 考虑k满足该点的条件即r->ch[1]->size+1<=k && ...
- BZOJ4500: 矩阵
Description 有一个n*m的矩阵,初始每个格子的权值都为0,可以对矩阵执行两种操作: 1. 选择一行, 该行每个格子的权值加1或减1. 2. 选择一列, 该列每个格子的权值加1或减1. 现在 ...
- iOS 项目中用到的一些开源库和第三方组件
iOS 项目中用到的一些 iOS 开源库和第三方组件 分享一下我目前所在公司 iOS 项目中用到的一些 iOS 开源库和第三方组件, 感谢开源, 减少了我们的劳动力, 节约了我们大量的时间, 让我们有 ...
- python--range()函数
1: >>> range(10, 0, -1) [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] >>> range(10) [0, 1, 2, 3, ...
- CSS3+HTML5实现块阴影与文字阴影
CSS 3 + HTML 5 是未来的 Web,它们都还没有正式到来,虽然不少浏览器已经开始对它们提供部分支持.本教程分5节介绍了 5 个 CSS3 技巧,可以帮你实现未来的 Web,不过,这些技术不 ...
- Hibernate的第一次测试解析
解析:此题目考查的是对Hibernate中交叉连接的理解.HQL支持SQL风格的交叉连接查询,交叉连接适用于两个类之间没有定义任何关联时.在where字句中,通过属性作为筛选条件,如统计报表数据.使用 ...
- thinkphp3.2设置session的过期时间
thinkPHP3.2中session的过期时间配置是不能使用的,我们需要修改一下它的配置文件thinkPHP>common>functions.php,找到这一行: if(isset($ ...
- 20个有用的jq弹窗
链接地址:http://www.ffpic.com/zixun/20-useful-jquery-modal-plugins.html 推荐:jQuery.bPopup.js
- js中常用的Tab切换
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 手动创建oem
[oracle@std bin]$ /u02/app/product//db_1/bin/emca -config dbcontrol db -repos create STARTED EMCA at ...