[HDU] 3549 Flow Problem [最大流][Dinic][读取优化]
来源:互联网 发布:wow采集信息数据库 编辑:程序博客网 时间:2024/04/28 23:22
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
link
题解
使用Dinic的复杂度是 O(E×V^2)
发现对其进行读取优化,效率立马提升3倍,可见读取优化对竞赛的重要性
#include<cstdio>#include<cstring>#include<vector>#include<queue>#include<algorithm>#define INF 0x3f3f3f3f#define MAX_V 20using namespace std;typedef long long LL;const int MAXS = 5*1024*1024;char buf[MAXS],bufout[MAXS],*ch,*chout;void read(int &x){ for(++ch;*ch<=32;++ch); for(x=0;*ch>='0';++ch) x=x*10+*ch-'0';}void out(int x){ if(!x) *(++chout)='0'; else{ char *ch0=chout,*ch1=chout+1; while(x){ *(++ch0)=x%10+'0'; x/=10; } chout=ch0; while(ch1<=ch0) swap(*(ch1++),*(ch0--)); } *(++chout)='\n';}void std_init(){ ch=buf-1; chout=bufout-1; fread(buf,1,MAXS,stdin);}void std_out(){ fwrite(bufout,1,chout-bufout+1,stdout);}/*---------------------------------------------------------------*/struct edge{int to,cap,rev;};vector<edge> G[MAX_V];int lever[MAX_V];int iter[MAX_V];void add_edge(int x,int y,int cost){ G[x].push_back((edge){y,cost,G[y].size()}); G[y].push_back((edge){x,0,G[x].size()-1});}void del_graph(int N){for(int i=1;i<=N;i++) while(!G[i].empty()) G[i].pop_back();}void bfs(int s){ memset(lever,-1,sizeof(lever)); queue<int> que; lever[s]=0; que.push(s); while(!que.empty()){ int v=que.front();que.pop(); for(int i=0;i<G[v].size();i++){ edge &e=G[v][i]; if(lever[e.to]<0&&e.cap>0){ lever[e.to]=lever[v]+1; que.push(e.to); } } }}int dfs(int v,int t,int f){ if(v==t) return f; for(int &i=iter[v];i<G[v].size();i++){ edge &e=G[v][i]; if(e.cap>0&&lever[v]<lever[e.to]){ int d=dfs(e.to,t,min(f,e.cap)); if(d>0){ e.cap-=d; G[e.to][e.rev].cap+=d; return d; } } } return 0;}int max_flow(int s,int t){ int flow=0,f; while(true){ bfs(s); if(lever[t]<0) return flow; memset(iter,0,sizeof(iter)); while(f=dfs(s,t,INF)) flow+=f; }}int main(){ std_init(); int T,N,M; read(T); for(int t=1;t<=T;t++){ read(N);read(M); int from,to,cost; for(int i=0;i<M;i++){ read(from);read(to);read(cost); add_edge(from,to,cost); } printf("Case %d: %d\n",t,max_flow(1,N)); del_graph(N); } return 0;}
- [HDU] 3549 Flow Problem [最大流][Dinic][读取优化]
- HDU 3549--Flow Problem 【最大流 && dinic】
- hdu 3549 Flow Problem【Dinic最大流】
- hdu 3549 Flow Problem (最大流—EK—Dinic)
- HDU 3549 Flow Problem 最大流 最小增广路 DINIC算法 也是46MS
- hdu 3549 Flow Problem (第二篇) Dinic算法 最大流问题
- HDU 3549 Flow Problem(最大流+EK邻接表||邻接矩阵||dinic算法模板比较)
- Flow Problem 【最大流 dinic】模版
- 【网络流】HDU 1532 Drainage Ditches(最大流Ford-Fulkersson)+HDU 3549 Flow Problem(最大流Dinic算法)
- hdu 3549 Flow Problem(简单网络流Dinic)
- HDU 3549 Flow Problem (Ford-Fulkerson&Dinic)
- HDU-3549 Flow Problem【最大流】
- hdu 3549 Flow Problem【最大流】
- hdu 3549 Flow Problem 最大流
- hdu 3549 Flow Problem (最大流)
- hdu - 3549 - Flow Problem(最大流)
- Hdu 3549 Flow Problem - 最大流
- HDU 3549 Flow Problem 最大网络流
- 4.7 筛选法求素数
- Zynq-Linux移植学习笔记之十-u-boot网络配置
- bzoj3992
- Vue入门
- Oracle SQL 中DELETE、DROP、TRUNCATE删除命令的区别
- [HDU] 3549 Flow Problem [最大流][Dinic][读取优化]
- AJAX POST 后跳转页面Form submission canceled because the form is not connected的问题解决
- 1.2 Adding the main frame
- 自定义一个TopBar(完整流程)
- 指定出口IP,发起HTTP请求
- 模拟赛 循环小数
- angular表单
- 蓝桥杯C_C++/Java程序设计常用算法&技巧总结
- spring3接管struts1.X的请求