hdu 3549(最大流)

依旧是最大流板子

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
struct note
{
    int fr,to,c,f;
   note(int a,int b,int x,int y):fr(a),to(b),c(x),f(y) {}
};
const int maxn=1000+100;
const int maxm=30;
const int inf=0x3f3f3f3f;
struct Karp
{
    vector<note> aa;
    vector<int> g[maxm];
    int a[maxm],p[maxm];
    void init(int n)
    {
        for(int i=0;i<n;i++)
            g[i].clear();
        aa.clear();
    }
  void addadge(int x,int y,int z)
  {
      aa.push_back(note(x,y,z,0));
      aa.push_back(note(y,x,0,0));
      int mm=aa.size();
      g[x].push_back(mm-2);
      g[y].push_back(mm-1);
  }
  int maxflow(int s,int t)
  {
      int flow=0;
      while(1)
      {
          memset(a,0,sizeof(a));
          a[s]=inf;
          queue<int> q;
          q.push(s);
          while(q.size())
          {
              int x=q.front();q.pop();
              for(int i=0;i<g[x].size();i++)
              {
                   note &e=aa[g[x][i]];
                  if(!a[e.to]&&(e.c>e.f))
                  {
                      a[e.to]=min(a[x],e.c-e.f);
                      p[e.to]=g[x][i];
                      q.push(e.to);
                  }
              }
              if(a[t]) break;
          }
          if(!a[t]) break;
          for(int i=t;i!=s;i=aa[p[i]].fr)
          {
              aa[p[i]].f+=a[t];
              aa[p[i]^1].f-=a[t];
          }
          flow+=a[t];
      }
      return flow;
  }
};
Karp mmp;
int m,n,t;
int main()
{
    scanf("%d",&t);
     for(int zz=1;zz<=t;zz++)
     {
         scanf("%d%d",&n,&m);
         mmp.init(n);
          for(int i=1;i<=m;i++)
          {
              int x,y,z;
              scanf("%d%d%d",&x,&y,&z);
              mmp.addadge(x,y,z);
          }
          printf("Case %d: %d\n",zz,mmp.maxflow(1,n));
     }
    return 0;
}
时间: 2024-10-03 23:10:25

hdu 3549(最大流)的相关文章

hdu 3549 最大流入门题

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 [科普]什么是BestCoder?如何参加? Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 8862    Accepted Submission(s): 4168 Problem Description

HDU 3549 最大流入门

Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 8394    Accepted Submission(s): 3910 Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, yo

HDU 3549 Flow Problem ( 最大流 -EK 算法)

C++,G++的读取速度差距也太大了 Flow Problem 题意:n,m表示n个点m条有向带权边 问:从1-n最大流多少 裸最大流,拿来练手,挺不错的 #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> const int N = 210; #define

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 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

hdu 3549 Flow Problem (网络最大流)

Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 6674    Accepted Submission(s): 3112 Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, yo

HDU 3549 Flow Problem 网络最大流问题 Edmonds_Karp算法

题目链接:HDU 3549 Flow Problem Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 8218    Accepted Submission(s): 3824 Problem Description Network flow is a well-known difficult problem f

hdu 3549 Flow Problem (最大流入门题)

增广路: 1 /************************************************************* 2 题目: Flow Problem(HDU 3549) 3 链接: http://acm.hdu.edu.cn/showproblem.php?pid=3549 4 题意: 给一个单向图,求从1到n的最大流 5 算法: 最大流之增广路(入门) 6 算法思想: 不断用BFS找通路,没每找一条路,记录这条路的最小流, 7 再给这条路上的所有流量减去这个最小值.

hdu 4888 最大流给出行列和求矩阵

第一步,考虑如何求是否有解.使用网络流求解,每一行和每一列分别对应一个点,加上源点和汇点一共有N+M+2个点.有三类边: 1. 源点 -> 每一行对应的点,流量限制为该行的和 2. 每一行对应的点 -> 每一列对应的点,流量限制为 K 3. 每一列对应的点 -> 汇点,流量限制为该列的和 对上图做最大流,若源点出发的边和到达汇点的边全都满流,则有解,否则无解.若要求构造方案,则 (i,j) 对应的整数就是行 i–> 列 j 的流量. 第二步,考虑解是否唯一.显然,解唯一的充分必要条

hdu 4975最大流与4888类似但是有很吊的优化最大流

//来自潘神的优化 #include<stdio.h> #include<string.h> #include<queue> using namespace std; #define inf 0x3fffffff #define N 1100 struct node { int u,v,w,next; }bian[N*N*4]; int head[N],yong,dis[N],work[N]; void init(){ yong=0; memset(head,-1,si