Since the end of the Noip 2014 after nearly one weeks without a problem, and now start to save the election, send a water of the bare network of the problems.
Title Link: http://poj.org/problem?id=1273
Bare network stream, template problem.
1. Edmond_karp algorithm
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include < algorithm> #include <queue> #define MAXV 220#define maxe 220#define INF 0x3f3f3f3fusing namespace Std;int n,m;int CAP[MAXV][MAXV],FLOW[MAXV][MAXV]; capacity, Flow int a[maxv];int last[maxv];int edmondskarp (int s,int t)//ek algorithm for s to T maximum stream {int sum=0; queue<int>q; while (1) {while (!q.empty ()) Q.pop (); Q.push (s); memset (A,0,sizeof (a)); A[s]=inf; while (!q.empty ())//Find augmented path {int u=q.front (); Q.pop (); for (int v=1;v<=m;v++) {if (!a[v]&&flow[u][v]<cap[u][v]) { Last[v]=u; Q.push (v); A[v]=min (A[u],cap[u][v]-flow[u][v]); }}} if (a[t]==0)//Cannot find the augmented path, the maximum stream break has been found; SUM+=A[T]; Add this augmentation to get the traffic for (int i=t;i!=s;i=last[i])///According to the linked list of the augmented road alongUpdate {flow[last[i]][i]+=a[t]; FLOW[I][LAST[I]]-=A[T]; }} return sum;} int main () {while (scanf ("%d%d", &n,&m)!=eof) {memset (cap,0,sizeof (CAP)); memset (flow,0,sizeof (flow)); for (int i=1;i<=n;i++) {int u,v,w; scanf ("%d%d%d", &u,&v,&w); Cap[u][v]+=w; } printf ("%d\n", Edmondskarp (1,m)); } return 0;}2. Dinic algorithm to quickly find the network flow
#include <iostream> #include <queue> #include <vector> #include <algorithm> #include <deque > #include <cstring> #define MEM (array,num) memset (array,num,sizeof (array)) #define Maxe 300#define INF 0x3f3f3f3fusing namespace Std;int g[maxe][maxe];bool visit[maxe];int layer[300]; layer[i]= point I layer int n,m;bool countlayer ()//bfs the graph is layered {int layer=0; deque<int>q; MEM (layer,-1); Initialization of all layers is 1 layer[1]=0; Point 1 layer is 0 q.push_back (1); Point 1 queue while (!q.empty ()) {int Now=q.front (); Q.pop_front (); for (int j=1;j<=m;j++) {if (g[now][j]>0&&layer[j]==-1)//now->j has the remaining capacity and J has not been accessed {layer[j]=layer[now]+1; if (j==m) return true; The hierarchy arrives at the meeting point and Else Q.push_back (j); }}} return false; Unable to reach meeting point, return False}int dinic () {int i,s,maxflow=0; deque<int>q; Stack, DFS with while (Countlayer ())//As long as can be layered {q.push_back (1);//Source Point inStack mem (visit,0); Visit[1]=true; while (!q.empty ()) {int top=q.back (); if (top==m)//top is the sink point {int minc=inf;//Find the smallest edge int sminc in the stack;//The beginning of the smallest edge of the capacity for (int i=1;i<q.size (); i++) {int vs=q[i-1]; int ve=q[i]; if (g[vs][ve]>0) {if (Minc>g[vs][ve])//update minimum capacity edge { Minc=g[vs][ve]; Sminc=vs; }}}//augmentation, change the image maxflow+=minc; for (int i=1;i<q.size (); i++) {int vs=q[i-1]; int ve=q[i]; G[vs][ve]-=minc; Reduce edge capacity g[ve][vs]+=minc; Increase the reverse side} while (!q.empty () &&q.back ()!=sminc)//rewind the stack until SmincFor the top of the stack, this facilitates DFS {visit[q.back ()]=false; Q.pop_back (); }} else//top is not a sink point {int i; for (i=1;i<=m;i++) {if (g[top][i]>0&&layer[i]==layer[top]+1&&!visit [i])//Only down a layer not traversed by the node walk {visit[i]=1; Q.push_back (i); Break }} if (I>M)//Cannot find the next point, then go back q.pop_back (); }}} return maxflow;} int main () {while (cin>>n>>m)//n edge m Point {mem (g,0); for (int i=0;i<n;i++) {int u,v,c; cin>>u>>v>>c; G[u][v]+=c; Prevent by pits, there may be multiple sides between two points, volume accumulation} cout<<dinic () <<endl; } return 0;}
[POJ 1273] Drainage ditches (edmond-krap algorithm and dinic algorithm for maximum flow)