network flow based on BFS
2016-09-29 23:41
204 查看
Hello,everyone.Long time no meeting.Graph theory is a little difficult for me.Because the code is difficult to write.And i have not learned STL yet.So i spend so time to find my own data structure,and spend a lot of time to get my code passed.Luckily,i
find my ability is growing day by day.Now i can easily find the mistakes in my code.
Have fun coding,i_human.Have fun coding,everyone!
THE CODE:// 网络流算法 BFS.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include<iostream> #define a 100 #define m 1000 using namespace std; int n,e,s,t,l; int f=0; int flow[a][a],pre[2][a],rec[a][a],cap[a][a],relpre[a]; bool find(); void operate(); int main() { int u,v,w; cin>>s>>t; for(int i=0;i<a;i++) for(int j=0;j<a;j++) { flow[i][j]=0; cap[i][j]=0; rec[i][j]=0; } for(int i=0;i<a;i++) { pre[0][i]=0; pre[1][i]=0; relpre[i]=0; } pre[0][0]=s; pre[1][0]=0; relpre[s]=s; cin>>n>>e; for(int i=0;i<e;i++) { cin>>u>>v>>w; cap[u][v]=w; rec[u][0]++; rec[u][rec[u][0]]=v; } for(int i=1;i<=n;i++) { for(int j=0;j<=n;j++) cout<<rec[i][j]<<" "; cout<<endl; } while(find()) { operate(); for(int i=1;i<=n;i++) relpre[i]=0; relpre[s]=s; pre[0][0]=s; pre[1][0]=0; } cout<<f<<endl; system("pause"); return 0; } bool find() { int start=pre[1][0]; int end=pre[1][0]; bool flag=0; int num=0; l=pre[1][0]+1; while(flag==0 && l<=n) { num=0; for(int i=start;i<=end;i++) { for(int j=1;j<=rec[pre[0][i]][0];j++) { if(relpre[rec[pre[0][i]][j]]!=0) continue; if(relpre[rec[pre[0][i]][j]]==0) { pre[0][end+j]=rec[pre[0][i]][j]; pre[1][end+j]=l; relpre[rec[pre[0][i]][j]]=pre[0][i]; num++; } if(rec[pre[0][i]][j]==t) { flag=1; return flag; } } } l++; start=end+1; end=end+num; } return flag; } void operate() { int path[a]; int min=a; int r=t; int pot; path[l]=t; for(int i=l-1;i>=0;i--) { path[i]=relpre[r]; r=relpre[r]; } for(int i=0;i<l;i++) { if(cap[path[i]][path[i+1]]<min) min=cap[path[i]][path[i+1]]; } f+=min; for(int i=0;i<l;i++) { cap[path[i]][path[i+1]]-=min; flow[path[i]][path[i+1]]+=min; if(cap[path[i]][path[i+1]]==0) { for(int j=1;j<=rec[path[i]][0];j++) { if(rec[path[i]][j]==path[i+1]) { pot=j; break; } } if(pot!=rec[path[i]][0]) { for(int k=pot+1;k<=rec[path[i]][0];k++) rec[path[i]][k-1]=rec[path[i]][k]; } rec[path[i]][0]--; } } }
相关文章推荐
- Age and gender estimation based on Convolutional Neural Network and TensorFlow
- Some Improvements on Deep Convolutional Neural Network Based Image Classif ication
- Some Improvements on Deep Convolutional Neural Network Based Image Classification(精读)
- Paper Read: Robust Deep Multi-modal Learning Based on Gated Information Fusion Network
- Information centric network (icn) node based on switch and network process using the node
- 2010: Hardware Design Flow based on USA company
- 西瓜书习题3.3,3.4 Based on TensorFlow
- Tracing to the Network Attack Based on Large Data Environment
- Retrain a tensorflow model based on Inception v3
- On Pairwise Costs for Network Flow Multi-Object Tracking
- 目标跟踪之“Robust Visual Tracking with Deep Convolutional Neural Network based Object Proposals on PETS”
- Centos 6 VM on Hyper-v network lost (eth0 disapear)
- Android is Based on Linux, But What Does That Mean?
- 网络存储技术介绍(1) ( based on zt)
- Check bounds overflow bugs in C programs based on LLVM
- VMWare的The network bridge on device VMnet0 is not running故障解决
- Radar And Millimeter-Wave System Based On Photonic Technology
- A New Discrete-Time Iterative Adaptive Dynamic Programming Algorithm Based on Q-Learning
- 关于Scrollview嵌套ListView,使用setListViewHeightBasedOnChildren(ListView listView) ,在小米手机上崩溃问题
- 异常:android.os.NetworkOnMainThreadException