1273. Drainage Ditches

 

Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.

Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.

Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.

 

Input. The input includes several cases. For each case, the first line contains two space-separated integers, N (0 ≤ N ≤ 200) and M (2 ≤ M ≤ 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 ≤ Si, Ei ≤ M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 ≤ Ci ≤ 10,000,000) is the maximum rate at which water will flow through the ditch.

 

Output. For each case, output a single integer, the maximum rate at which water may emptied from the pond.

 

Sample Input

5 4

1 2 40

1 4 20

2 4 20

2 3 30

3 4 10

 

Sample Output

50

 

 

ÐÅØÅÍÈÅ

ãðàôû – ìàêñèìàëüíîå ïàðîñî÷åòàíèå

 

Àíàëèç àëãîðèòìà

Âõîäíûå äàííûå ñîñòîÿò èç íåñêîëüêèõ òåñòîâ. Âõîäíûå ãðàôû ÿâëÿþòñÿ îðèåíòèðîâàííûìè.  çàäà÷å òðåáóåòñÿ íàéòè ìàêñèìàëüíûé ïîòîê ìåæäó âåðøèíàìè 1 è n.

 

Ðåàëèçàöèÿ àëãîðèòìà ïðè ïîìîùè Ýäìîíäñà - Êàðïà

 

#include <cstdio>

#include <cstring>

#include <algorithm>

#define MAX 220

using namespace std;

 

int m[MAX][MAX], used[MAX];

int a, b, c, flow, MaxFlow, n, Edges;

 

int aug(int x,int t,int CurFlow)

 {

  if (x == t) return CurFlow;

  if (used[x]++) return 0;

 

  for (int Flow, y = 1; y <= n; y++)

  {

    if (m[x][y] > 0 && (Flow = aug(y,t,min(CurFlow,m[x][y]))))

    {

      m[x][y] -= Flow; m[y][x] += Flow;

      return Flow;

    }

  }

  return 0;

 }

 

int main(void)

{

  memset(m,0,sizeof(m));

  while(scanf("%d %d",&Edges,&n) == 2)

  {

    memset(m,0,sizeof(m));

    while (Edges--)

      scanf("%d %d %d",&a,&b,&c), m[a][b] += c;

    MaxFlow = 0;

    do

    {

      memset(used,0,sizeof(used));

    } while ((flow = aug(1,n,0x7FFFFFFF)) && (MaxFlow += flow));

    printf("%d\n",MaxFlow);

  }

  return 0;

}

 

Ðåàëèçàöèÿ àëãîðèòìà ïðè ïîìîùè Äèíèöà

 

#include <cstdio>

#include <cstring>

#define MAX 210

#define INF 0x3F3F3F3F

using namespace std;

 

long long Cap[MAX][MAX];

int ptr[MAX], d[MAX];

int a, b, c, n, Edges;

long long MaxFlow;

 

long long min(long long i, long long j)

{

  return (i < j) ? i : j;

}

 

long long bfs(int s)

{

  int q[MAX];

  int qh = 0, qt = 0;

  q[qt++] = s;

  memset (d, -1, sizeof(d));

  d[s] = 0;

  while (qh < qt)

  {

    int v = q[qh++];

    for (int to = 1; to <= n; to++)

     if ((d[to] == -1) && Cap[v][to])

     {

       q[qt++] = to;

       d[to] = d[v] + 1;

     }

  }

  return d[n] != -1;

}

 

long long dfs(int v, long long flow)

{

  if (!flow)  return 0;

  if (v == n)  return flow;

  for (int &to = ptr[v]; to <= n; to++)

  {

    if (d[to] != d[v] + 1)  continue;

    int pushed = dfs (to, min (flow, Cap[v][to]));

    if (pushed)

    {

      Cap[v][to] -= pushed;

      Cap[to][v] += pushed;

      return pushed;

    }

  }

  return 0;

}

 

long long Dinic(int s)

{

  long long flow = 0;

  for (;;)

  {

    if (!bfs(s))  break;

    for(int i = 1; i <= n; i++) ptr[i] = 1;

    while (long long pushed = dfs (s, INF))

      flow += pushed;

  }

  return flow;

}

 

int main(void)

{

  while(scanf("%d %d",&Edges,&n) == 2)

  {

    memset(Cap,0,sizeof(Cap));

    while (Edges--)

      scanf("%d %d %d",&a,&b,&c), Cap[a][b] += c;

    MaxFlow = Dinic(1);

    printf("%lld\n",MaxFlow);

  }

  return 0;

}