题目链接

Problem Description

Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph.

Input

The first line of input contains an integer T, denoting the number of test cases.

For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 <= M <= 1000)

Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)

Output

For each test cases, you should output the maximum flow from source 1 to sink N.

Sample Input

2

3 2

1 2 1

2 3 1

3 3

1 2 1

2 3 1

1 3 1
Sample Output

Case 1: 1
Case 2: 2

模板题,用来测试模板,我的模板都能过。

#include<cstdio>
#include<cstring>
#include<queue>
#define INF 1e9
using namespace std;
const int maxn=200+5; struct Edge
{
int from,to,cap,flow;
Edge() {}
Edge(int f,int t,int c,int flow):from(f),to(t),cap(c),flow(flow) {}
}; struct Dinic
{
int n,m,s,t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int cur[maxn];
int d[maxn]; void init(int n,int s,int t)
{
this->n=n, this->s=s, this->t=t;
edges.clear();
for(int i=1; i<=n; i++)
G[i].clear();
} void AddEdge(int from,int to,int cap)
{
edges.push_back(Edge(from,to,cap,0));
edges.push_back(Edge(to,from,0,0));
m = edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
} bool BFS()
{
memset(vis,0,sizeof(vis));
queue<int> Q;
d[s]=0;
Q.push(s);
vis[s]=true;
while(!Q.empty())
{
int x=Q.front();
Q.pop();
for(int i=0; i<G[x].size(); i++)
{
Edge& e=edges[G[x][i]];
if(!vis[e.to] && e.cap>e.flow)
{
vis[e.to]=true;
Q.push(e.to);
d[e.to]= 1+d[x];
}
}
}
return vis[t];
} int DFS(int x,int a)
{
if(x==t || a==0)
return a;
int flow=0,f;
for(int& i=cur[x]; i<G[x].size(); i++)
{
Edge& e=edges[G[x][i]];
if(d[x]+1==d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow) ))>0 )
{
e.flow+=f;
edges[G[x][i]^1].flow -=f;
flow+=f;
a-=f;
if(a==0)
break;
}
}
return flow;
} int Maxflow()
{
int flow=0;
while(BFS())
{
memset(cur,0,sizeof(cur));
flow += DFS(s,INF);
}
return flow;
}
} DC; int main()
{
int n,m,t;
scanf("%d",&t);
for(int i=1;i<=t;++i)
{
scanf("%d%d",&n,&m);
DC.init(n,1,n);
while(m--)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
DC.AddEdge(u,v,w);
}
printf("Case %d: %d\n",i,DC.Maxflow());
}
return 0;
}

网络流--最大流--HDU 3549 Flow Problem的更多相关文章

  1. HDU 3549 Flow Problem(最大流)

    HDU 3549 Flow Problem(最大流) Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...

  2. 网络流 HDU 3549 Flow Problem

    网络流 HDU 3549 Flow Problem 题目:pid=3549">http://acm.hdu.edu.cn/showproblem.php?pid=3549 用增广路算法 ...

  3. hdu 3549 Flow Problem【最大流增广路入门模板题】

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Time Limit: 5000/5000 MS (Java/Others ...

  4. hdu 3549 Flow Problem

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Description Network flow is a well- ...

  5. HDU 3549 Flow Problem 网络流(最大流) FF EK

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Tot ...

  6. HDU 3549 Flow Problem (最大流ISAP)

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

  7. hdu 3549 Flow Problem Edmonds_Karp算法求解最大流

    Flow Problem 题意:N个顶点M条边,(2 <= N <= 15, 0 <= M <= 1000)问从1到N的最大流量为多少? 分析:直接使用Edmonds_Karp ...

  8. hdu 3549 Flow Problem (网络最大流)

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

  9. hdu 3549 Flow Problem 最大流问题 (模板题)

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

随机推荐

  1. NonRegisteringDriver造成的内存频繁FullGc

    某天上服务器看了下gc情况,发现状况不对,启动了才2天的服务器发生了360次fullgc,这个频率肯定高了 说明 S0C.S1C.S0U.S1U:Survivor 0/1区容量(Capacity)和使 ...

  2. js数组的遍历(API)

    1.for 循环 普通遍历方法,可优化,存下数组的length,避免每次都去获取数组的length,性能提升 for(var i=0;i<arr.length;i++){ console.log ...

  3. 【Java】用IDEA搭建源码阅读环境

    用IDEA搭建源码阅读环境 参考自CodeSheep的Mac源码环境搭建, https://www.bilibili.com/video/BV1V7411U78L 但是实际上在Windows搭建的差别 ...

  4. 【Java】FlowControl 流程控制

    FlowControl 流程控制 什么是流程控制? 控制流程(也称为流程控制)是计算机运算领域的用语,意指在程序运行时,个别的指令(或是陈述.子程序)运行或求值的顺序. 不论是在声明式编程语言或是函数 ...

  5. unity使用Animator做一个简单的动画

    1.在unity的物体上添加Animator组件 2.在Project下的Assets下添加Animator Controller 3.在Animator Controller添加动作 4.在动作之间 ...

  6. FormCloseQuery

    FormCloseQuery是一个事件回调函数,Close是一个功能函数.而FormCloseQuery与窗口的close有什么不同,通过跟踪Call stack发生在调用Close()函数关闭窗口时 ...

  7. stand up meeting 12/2/2015

    part 组员 今日工作 工作耗时/h 明日计划 工作耗时/h UI 冯晓云  将logic部分提供的delete接口接入,与logic,数据库部分沟通实现了add/delete按钮的复用:解决UI部 ...

  8. cool-yogurt小组采访感想

    “对于这个小组项目的选题,其实最初的那个版本我还是被“感动”到的,因为我自己以前确实有这样的类似体验和需求,以前非常喜欢一个球星,因此想知道关于他所有的事情,想知道他每一场比赛的数据,新闻有哪些报道, ...

  9. 漫谈LiteOS-Huawei_IoT_Link_SDK_OTA 开发指导

    1概述 在应用升级过程中,无线下载更新(OTA)是一种常用,且方便的升级方式.Liteos采用的OTA升级方案基于LwM2M协议,实现了固件升级(FOTA)和软件升级(SOTA)两种升级方案.用户可根 ...

  10. ROM定制开发教程-Android adb命令用法与实例解析

    一.什么是ADB Android Debug Bridge(adb)是一个命令行工具,可让您与模拟器或连接的Android设备进行通信.您可以在android sdk / platform-tools ...