图割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 ...
随机推荐
- tornado 学习笔记3 安装
3 安装 安装分为两种方式:自动安装和手动安装,推荐采用自动安装,提前是机器联网的情况下. 3.1 自动安装: 命令:pip install tornado 写一段代码测试一下安装是否成功 # -*- ...
- 预定义指令之debug
1)根据你必须知道的.NET一书, #define DEBUG class Program { static void Main(string[] args) { #if DEBUG Console. ...
- UIButton在Disabled状态下标题混乱的问题
最近开发中遇到的问题汇总 有段时间没有归纳开发中遇到的一些问题了,今天就写一下之前开发中遇到的几个问题.希望这 篇文章能让读者在以后的开发中少走弯路.本文将依次介绍<UIButton在Disab ...
- Emoji表情符号录入MySQL数据库报错
版本一: 1,查看tomcat后台日志,核心报错信息如下: Caused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\ ...
- Java教程-Java 程序员们值得一看的好书推荐
学习的最好途径就是看书“,这是我自己学习并且小有了一定的积累之后的第一体会.个人认为看书有两点好处: 能出版出来的书一定是经过反复的思考.雕琢和审核的,因此从专业性的角度来说,一本好书的价值远超其他资 ...
- GO语言练习:不定参数函数
1.代码 2.运行 1.代码 package main import "fmt" func MyPrintf(args ...interface{}){ for _, arg := ...
- C#常用方法二
public sealed class StringTool { /// <summary> /// 将txt文件读入字符串 /// </summary> /// <pa ...
- hdu 2102 BFS
原题链接 思路:bfs搜一发 AC代码: #include "map" #include "queue" #include "math.h" ...
- LeetCode 刷题顺序表
Id Question Difficulty Frequency Data Structures Algorithms 1 Two Sum 2 5 array + set sort + two poi ...
- windows信息
echo system('systeminfo'); echo "系统类型".php_uname("s")."<br>"; ec ...