模板题:POJ1273

EK:

#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,m,jy,map[305][305],c[305][305],a[305],p[305];
bool vis[305];
int M(int e)
{
int f=0;
queue<int>q;
while(1){
memset(a,0,sizeof(a));
memset(vis,0,sizeof(vis));
a[1]=0x3fffffff;vis[1]=1;q.push(1);
while(!q.empty()){
jy=q.front();q.pop();
for(int i=1;i<=e;i++){
if(!vis[i]&&map[jy][i]-c[jy][i]>0)
{
vis[i]=1;
a[i]=min(a[jy],map[jy][i]-c[jy][i]);
p[i]=jy;
q.push(i);
}
}
}
if(a[e]==0)break;
for(int i=e;i!=1;i=p[i]){
c[i][p[i]]-=a[e];
c[p[i]][i]+=a[e];
}
f+=a[e];
}
return f;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(map,0,sizeof(map));
memset(c,0,sizeof(c));
memset(p,0,sizeof(p));
for(int i=1;i<=n;i++)
{
register int xx,yy,ee;
scanf("%d%d%d",&xx,&yy,&ee);
map[xx][yy]+=ee;
}
printf("%d\n",M(m));
}
}

Dinic:

#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 444
int tot,next[N],first[N],w[N],v[N],n,m,ch[N];
void add(int from,int to,int weight){
v[tot]=to;w[tot]=weight;
next[tot]=first[from];
first[from]=tot++;
}
bool tell(){
memset(ch,-1,sizeof(ch));
queue<int> q;
q.push(1);ch[1]=0;
while(!q.empty()){
int t=q.front();q.pop();
for(int i=first[t];~i;i=next[i])
if(w[i]&&ch[v[i]]==-1)
q.push(v[i]),ch[v[i]]=ch[t]+1;
}
return ch[n]!=-1;
}
int zeng(int a,int b){
if(a==n)return b;
int r=0;
for(int i=first[a];~i&&b>=r;i=next[i])
if(ch[a]+1==ch[v[i]]&&w[i]){
int t=zeng(v[i],min(b-r,w[i]));
w[i]-=t;w[i^1]+=t;r+=t;
}
if(!r)ch[a]=-1;
return r;
}
int dinic(){
int ans=0,jy;
while(tell())while(jy=zeng(1,0x3fffffff))ans+=jy;
return ans;
}
int main()
{
while(scanf("%d%d",&m,&n)!=EOF){
memset(first,-1,sizeof(first));
register int xx,yy,zz;
tot=0;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&xx,&yy,&zz);
add(xx,yy,zz);add(yy,xx,0);
}
printf("%d\n",dinic());
}
}

最新文章

  1. Ubuntu15.10下华南师大锐捷认证客户端的使用详解
  2. 为什么DOM操作很慢
  3. mysql的时间转化
  4. H608B无线路由破解方法
  5. Scala 深入浅出实战经典 第75讲:模式匹配下的For循环
  6. 如何使用Vue2做服务端渲染
  7. Mysql主从复制架构实战
  8. web服务器软件
  9. openstack之neutron配额调整
  10. [CXF REST标准实战系列] 二、Spring4.0 整合 CXF3.0,实现测试接口
  11. SQLALCHEMY_TRACK_MODIFICATIONS adds significant异常的解决方法
  12. MFC入门(一)-- 第一个简单的windows图形化界面小程序(打开计算器,记事本,查IP)
  13. C专家编程
  14. JSP (tomcat 内置对象)
  15. UI控件(ios)---UIImageView
  16. HashMap HashTable ConcurrentHashMap
  17. debug $mysqli-&gt;character_set_name();
  18. bootstrap-multiselect样式修改
  19. 连接数据库及出现System.AccessViolationException错误的解决方法
  20. 一站式学习Wireshark(一):Wireshark基本用法

热门文章

  1. HTML5轻松实现全屏视频背景
  2. python round()模块
  3. Django - ORM创建基本类
  4. SqlServer 【基 本 操 作】
  5. vue跨域问题解决(生产环境)
  6. 【[Offer收割]编程练习赛12 A】歌德巴赫猜想
  7. Spring+SpringMVC+MyBatis整合教程
  8. 对SPI、IIC、IIS、UART、CAN、SDIO、GPIO的解释
  9. 一个最简单的SPRINGMVC示例
  10. nyoj_38_布线问题_201403121753