qyy开始练习网络流啦 , 啊 ,蒟蒻只会套版 ,很裸的题 , 我连题都不想发了 ,可以参考我的代码(虽然我也是看的别人的

 #include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <queue>
const int N = + , M = + ;
using namespace std ;
int n , m ;
int tot , head[N] ;
long long ans;
struct id
{
int to , nxt ;
long long val ;
} edge[M<<] ;
queue< int > Q;
int dis[N] ,cur[N]; inline void Init( )
{
freopen( "NSOOJ10477.in" , "r" , stdin ) ;
freopen( "NSOOJ10477.out" , "w" , stdout ) ;
} int read( )
{
char ch = getchar( ) ; int k = , ret = ;
while( ch > '' || ch < '' ) { if( ch == '-' ) k = - ; ch = getchar( ) ; }
while( ch <= '' && ch >= '' ) ret = ret * + ch - '' , ch = getchar( ) ;
return ret * k ;
} void add( int u , int v , int c )
{
edge[++tot].to = v , edge[tot].nxt = head[u] ;
edge[tot].val = c , head[u] = tot ;
} void input( )
{
n = read( ) , m = read( ) ;
memset( head , - , sizeof( head ) ) ;
tot = ;
for( int x = ; x <= m ; ++x )
{
int a, b , c ;
a = read( ) , b = read( ) , c = read( ) ;
add( a, b , c ) ;
add( b , a , ) ;
}
} bool bfs( )
{
memset( dis , - , sizeof(dis) ) ;
Q.push( ) ; dis[] = ;
while( !Q.empty( ) )
{
int u = Q.front( ) ; Q.pop( ) ;
for( int i = head[u] ; ~i ; i = edge[i].nxt )
{ int v = edge[i].to ;
if( dis[v] < && edge[i].val > )
{
dis[v] = dis[u] + ; Q.push( v ) ;
} }
}
// cout<<endl;
if( dis[n] > ) return true ;
return false ;
} long long int dfs( int u , int inf )
{
long long int ans = 0ll , cost = 0ll;
if( u == n ) return inf ;
for( int i = cur[u] ; ~i ; i = edge[i].nxt )
{
int v = edge[i].to ;
if( dis[v] != dis[u] + ) continue ;
ans = dfs( v , min( inf - cost , edge[i].val ) ) ;
edge[i].val -= ans ;
edge[i^].val += ans ;
if( edge[i].val ) cur[u] = i ;
cost += ans ;
if( cost == inf ) return inf ;
}
if( !cost ) dis[u] = - ;
return cost ;
} void sov( )
{
ans = ;
while( bfs( ) )
{
int now ;
for( int i = ; i <= n ; ++i ) cur[i] = head[i] ;
ans += dfs( , << ) ;
}
printf( "%lld" , ans ) ;
} int main( )
{
// Init( ) ;
input( ) ;
sov( ) ;
// fclose( stdin ) ;
// fclose( stdout ) ;
return ;
}

重新贴板,和上面的程序几乎没什么区别,码风固定了,值得开心。

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
const int N = , M = 3e5 + , inf = << ;
using namespace std;
int n, m, head[N], cnt, dis[N],cur[N];
queue<int>Q;
struct edge
{
int nxt,to,vi;
} E[M<<]; void add(int u,int v,int vi)
{
E[++cnt] = (edge){head[u],v,vi}; head[u] = cnt;
E[++cnt] = (edge){head[v],u,}; head[v] = cnt;
} void Init()
{
scanf("%d%d",&n,&m); cnt = -;
int u,v,vi;
memset(head,-,sizeof(head));
for(int i = ; i <= m; ++i)
{
scanf("%d%d%d",&u,&v,&vi);
add(u,v,vi);
}
} bool bfs()
{
memset(dis,-,sizeof(dis));
Q.push(); dis[] = ;
while(!Q.empty())
{
int u = Q.front(); Q.pop();
for(int i = head[u]; ~i; i = E[i].nxt)
{
int v = E[i].to;
if(dis[v] == - && E[i].vi > ) dis[v] = dis[u] + , Q.push(v);
}
}
return (dis[n] > );
} int dfs(int u,int t,int ff)
{
int ret = , cost = ;
if(u == t) return ff;
for(int i = cur[u] ; ~i; i = E[i].nxt)
{
int v = E[i].to;
if(dis[v] != dis[u] + ) continue;
cost = dfs(v,t,min(ff-ret,E[i].vi));
E[i].vi -= cost, E[i^].vi += cost; ret += cost;
if(E[i].vi) cur[u] = i;
if(ret == ff) return ff;
}
if(!ret) dis[u] = -;
return ret;
} int max_flow(int s,int t)
{
int ret = ;
while(bfs())
{
for(int i = ; i <= n; ++i) cur[i] = head[i];
ret += dfs(s,t,inf);
}
return ret;
} void Solve()
{
int ans = max_flow(,n);
printf("%d\n",ans);
} int main()
{
Init();
Solve(); return ;
}

最新文章

  1. Android性能优化
  2. php安全编程: register_globals的安全性
  3. lua中得栈
  4. Wing IDE 5 的破解
  5. Windows套接字Socket函数
  6. pdf压缩之GSview
  7. 为什么报错说req未定义,createServer只接受匿名函数吗?
  8. React-Native基础教程
  9. 安装tomcat过程中出现问题小结
  10. Kubeadm 安装部署 Kubernetes 集群
  11. 转 Caffe学习系列(3):视觉层(Vision Layers)及参数
  12. git push 报错 &quot;Peer certificate cannot be authenticated with known CA certificates&quot;
  13. hive笔记
  14. 最近在学习Flask框架,那么就说下jinja2吧~~~
  15. 洛谷P1209修理牛棚题解
  16. Jessica&#39;s Reading Problem POJ - 3320(尺取法2)
  17. windows远程以及文件共享方法总结
  18. 使用Fidder从安卓模拟器获取APP内H5游戏网址
  19. C# DataTable Select用法
  20. Mac 10.12安装FTP工具FileZilla

热门文章

  1. leetcode中一些要用到动态规划的题目
  2. php返回相对时间(如:20分钟前,3天前)的方法
  3. 原生js判断是否有某个class,如果有就删掉,没有加上
  4. 互联网 免费的WebService接口
  5. js获取时间加多山天和时间戳转换成日期
  6. Jquery not选择器实现元素显示隐藏
  7. 实现Word的列表样式
  8. Android Learning:多线程与异步消息处理机制
  9. 大话设计模式之策略模式(strategy)
  10. 转:使用Tengine替代Nginx作为负载均衡服务器