Flow Problem

Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 8199    Accepted Submission(s): 3814

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
 
题意:给一个有向图,求源点为1汇点为n的最大流
 
思路:裸题,ISAP。。原来之前自己写的模版有点疏漏了。。
 
#include <vector>
#include <cstdio>
#include <cstring>
#include <queue>
#define FOR(i,n) for(i=1;i<=(n);i++)
using namespace std;
const int INF = 1e9;
const int N = ; struct Edge{
int from,to,cap,flow;
}; struct ISAP{
int n,m,s,t;
int p[N],num[N];
vector<Edge> edges;
vector<int> G[N];
bool vis[N];
int d[N],cur[N];
void init(int _n,int _m)
{
n=_n; m=_m;
int i;
edges.clear();
FOR(i,n)
{
G[i].clear();
d[i]=INF;
}
}
void AddEdge(int from,int to,int cap)
{
edges.push_back((Edge){from,to,cap,});
edges.push_back((Edge){to,from,,});
m = edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
}
bool BFS()
{
memset(vis,,sizeof(vis));
queue<int> Q;
Q.push(t);
d[t]=;
vis[t]=;
while(!Q.empty())
{
int x = Q.front(); Q.pop();
for(unsigned i=;i<G[x].size();i++)
{
Edge& e = edges[G[x][i]^];
if(!vis[e.from] && e.cap>e.flow)
{
vis[e.from]=;
d[e.from] = d[x]+;
Q.push(e.from);
}
}
}
return vis[s];
}
int Augment()
{
int x=t, a=INF;
while(x!=s)
{
Edge& e = edges[p[x]];
a = min(a,e.cap-e.flow);
x = edges[p[x]].from;
}
x = t;
while(x!=s)
{
edges[p[x]].flow+=a;
edges[p[x]^].flow-=a;
x=edges[p[x]].from;
}
return a;
}
int Maxflow(int _s,int _t)
{
s=_s; t=_t;
int flow = , i;
BFS();
if(d[s]>=n) return ;
memset(num,,sizeof(num));
memset(p,,sizeof(p));
FOR(i,n) if(d[i]<INF) num[d[i]]++;
int x=s;
memset(cur,,sizeof(cur));
while(d[s]<n)
{
if(x==t)
{
flow+=Augment();
x=s;
}
int ok=;
for(unsigned i=cur[x];i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if(e.cap>e.flow && d[x]==d[e.to]+)
{
ok=;
p[e.to]=G[x][i];
cur[x]=i;
x=e.to;
break;
}
}
if(!ok)
{
int m=n-;
for(unsigned i=;i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if(e.cap>e.flow) m=min(m,d[e.to]);
}
if(--num[d[x]]==) break;
num[d[x]=m+]++;
cur[x]=;
if(x!=s) x=edges[p[x]].from;
}
}
return flow;
}
}; ISAP isap; void run()
{
int n,m,u,v,c;
scanf("%d%d",&n,&m);
isap.init(n,m);
while(m--)
{
scanf("%d%d%d",&u,&v,&c);
isap.AddEdge(u,v,c);
//isap.AddEdge(v,u,c);
}
static int cas = ;
printf("Case %d: %d\n",cas++,isap.Maxflow(,n));
} int main()
{
freopen("case.txt","r",stdin);
int _;
scanf("%d",&_);
while(_--)
run();
return ;
}
 

HDU 3549 Flow Problem (最大流ISAP)的更多相关文章

  1. HDU 3549 Flow Problem (dinic模版 && isap模版)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 题意: 给你一个有向图,问你1到n的最大流. dinic模版 (n*n*m) #include ...

  2. hdu - 3549 Flow Problem (最大流模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=3549 Ford-Fulkerson算法. #include <iostream> #include ...

  3. hdu 3549 Flow Problem (最大流)

    裸最大流,做模板用 m条边,n个点,求最大流 #include <iostream> #include <cstdio> #include <cstring> #i ...

  4. hdu 3549 Flow Problem 最大流 Dinic

    题目链接 题意 裸的最大流. 学习参考 http://www.cnblogs.com/SYCstudio/p/7260613.html Code #include <bits/stdc++.h& ...

  5. HDU 3549 Flow Problem(最大流)

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

  6. 网络流 HDU 3549 Flow Problem

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

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

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

  8. hdu 3549 Flow Problem

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

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

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

随机推荐

  1. 开发及应用中 Linux与Window 取舍

    Linux是开源的,而Windows不是.这个也是Linux与Windows之间最大的差异.一般来说,开源似乎收到了更多系统管理员的亲睐,而开源的软件似乎更受个人电脑用户的欢迎.两种类型之间有很多不同 ...

  2. Hibernate性能优化

    1.性能是与具体的项目挂钩的,并不是对于A项目某种优化方法好就适用于B项目.性能需要不断的测试检验出来的.....(废话) 2.session.clear()方法的使用,通常session是有缓存的 ...

  3. 如何解决安装好的google浏览器打不开网页的问题?

    1.Google浏览器右上角,三个点,点击一下, 2.点击设置 3.在"搜索引擎"这一栏,选择'管理搜索引擎',右边的倒三角,进入选择界面 4.在其他搜索引擎中选择"百度 ...

  4. js版本的汉字转拼音

    var PinYin = {"a":"\u554a\u963f\u9515","ai":"\u57c3\u6328\u54ce\u ...

  5. 我的Android进阶之旅------>android视频播放只有声音无画面的解决办法

    今天调试公司用VideoView实现的播放器来播放视频的时候,只有声音输出而无画面输出.一开始以为是自己程序有问题,调试了半天无果.怀疑是真机本身的问题,于是下了几个第三方的播放器来进行视频播放,例如 ...

  6. T_CODE I18N

    关于T-CODE I18N 最近由于看到很多人遇到SMARTFORMS不能拖拽字段的问题,这个的解决方案 I18N:解决SMARTFORMS的不能从Field name 那边直接把变量拖入右边编辑框 ...

  7. High Performance Browser Networking

    Chapter 1. Primer on Latency and Bandwidth As a result, to improve performance of our applications, ...

  8. 用fiddler替换线上网页资源调试界面

    fiddler 是一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网之间的http通讯,设置断点,查看所有进出fiddler的数据(指cookie,html,js,css等文件,这些都 ...

  9. drawable animation基础

    动画配置<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android: ...

  10. 6 《锋利的jQuery》Ajax的应用(略。)

    Ajax的优势 1.不需要插件支持 2.优秀的用户体验 3.提高web程序的性能(传输数据的方式,按需发送) 4.减轻服务器和带宽的负担 Ajax的不足 1.浏览器对XMLHttpRequest对象支 ...