标签:color pac print level empty 模版 nan clu div
最大流模版题
ek算法迷之tle一点,dinic秒过
#include<cstdio> #include<algorithm> #include<vector> #include<cstring> #include<queue> using namespace std; const int maxn=10010,inf=99999999; struct node{ int to,w,rev; }; int iter[maxn],level[maxn],n; vector<node>map[maxn]; void add_edge(int s,int t,int w){ map[s].push_back((node){t,w,map[t].size()}); map[t].push_back((node){s,0,map[s].size()-1}); } int dfs(int s,int t,int k){ if(s==t){ return k; } for(int &i=iter[s];i<map[s].size();++i){ if(level[map[s][i].to]>level[s]&&map[s][i].w>0){ int d=dfs(map[s][i].to,t,min(k,map[s][i].w)); if(d>0){ map[s][i].w-=d; map[map[s][i].to][map[s][i].rev].w+=d; return d; } } } return -1; } void bfs(int s){ memset(level,-1,sizeof(level)); queue<int>q; level[s]=0; q.push(s); while(!q.empty()){ int v=q.front(); q.pop(); for(int i=0;i<map[v].size();++i){ if(level[map[v][i].to]<0&&map[v][i].w>0){ level[map[v][i].to]=level[v]+1; q.push(map[v][i].to); } } } } int max_flow(int s,int t){ int ret=0; for(;;){ bfs(s); if(level[t]<0)break; memset(iter,0,sizeof(iter)); while(1){ int d=dfs(s,t,inf); if(d==-1)break; ret+=d; } } return ret; } int main(){ int m,s,x; scanf("%d%d%d",&n,&m,&x); for(int i=1;i<=m;++i){ int s,t,w; scanf("%d%d%d",&s,&t,&w); add_edge(s,t,w); } int t=max_flow(1,n); if(t>0){ printf("%d ",t); if(x%t==0)printf("%d",x/t); else printf("%d",x/t+1); } else printf("Orz Ni Jinan Saint Cow!"); return 0; }
标签:color pac print level empty 模版 nan clu div
原文地址:http://www.cnblogs.com/lqs6/p/7455432.html