Flow Problem

Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 13315    Accepted Submission(s): 6372

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
#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>
#define maxn 16
#define inf 1<<29
using namespace std;
struct edge
{
int to,cap,rev;
};
int n,m,vis[maxn];
vector <edge> fuck[maxn];
void add_edge(int from,int to,int cap)//构图过程
{
fuck[from].push_back((edge){to,cap,fuck[to].size()});//fuck[to].size() 表示反边在反向邻接表的位置--即fuck[to]中的位置
fuck[to].push_back((edge){from,0,fuck[from].size()-1});//同上
}
void init()
{
for(int i=1;i<=n;i++) fuck[maxn].clear();
}
int dfs(int s,int e,int f)
{
if(s==e) return f;
vis[s]=1;
for(int i=0;i<fuck[s].size();i++)//每次的搜索 依次遍历s链接的点 更新cap 以及最大流量
{
edge &temp=fuck[s][i];//注意&这个东西 如果不用这个封装的话 改变的内容就只是形参的内容
if(!vis[temp.to]&&temp.cap>0)
{
int d=dfs(temp.to,e,min(f,temp.cap));
if(d>0)
{
temp.cap-=d;
fuck[temp.to][temp.rev].cap+=d;// 回溯处理容量问题 以及对反向边的处理
//一直回溯到流量最小的那个点上去 然后去走其他路
return d;//回溯过程肯定是要更新的
}
}
}
return 0;
}
int max_flow(int s,int e)
{
int flow=0;
while(1)//不断的从s走到e这个点 当所有可能的通道cap为0的时候 贪心完成(因为每次过程都会cap进行一次更新)
{
memset(vis,0,sizeof(vis));
int temp=dfs(s,e,inf);
if(temp==0) return flow;
else flow+=temp;
}
}
int main()
{
cin.sync_with_stdio(false);
int t,Case=0;
cin>>t;
while(t--)
{
cin>>n>>m;
init();
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
add_edge(a,b,c);//构图
}
cout<<"Case "<<++Case<<": "<<max_flow(1,n)<<endl; }
return 0;
}
 
 
 
 

hdu 3549 初试最大流问题的更多相关文章

  1. HDU 3549 Flow Problem(最大流)

    HDU 3549 Flow Problem(最大流) Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...

  2. Flow Problem HDU - 3549

    Flow Problem HDU - 3549 Network flow is a well-known difficult problem for ACMers. Given a graph, yo ...

  3. 网络流 HDU 3549 Flow Problem

    网络流 HDU 3549 Flow Problem 题目:pid=3549">http://acm.hdu.edu.cn/showproblem.php?pid=3549 用增广路算法 ...

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

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

  5. HDU 3549 网络最大流再试

    http://acm.hdu.edu.cn/showproblem.php?pid=3549 同样的网络最大流 T了好几次原因是用了cout,改成printf就A了 还有HDU oj的编译器也不支持以 ...

  6. hdu 3549 Flow Problem

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Description Network flow is a well- ...

  7. hdu 3549 Flow Problem 网络流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Network flow is a well-known difficult problem f ...

  8. hdu 3549 Flow Problem(增广路算法)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549 模板题,白书上的代码... #include <iostream> #include & ...

  9. HDU 3549 Flow Problem(最大流模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=3549 刚接触网络流,感觉有点难啊,只好先拿几道基础的模板题来练练手. 最大流的模板题. #include< ...

随机推荐

  1. 单词拼接(dfs/回溯/递归)

    单词拼接传送门 //单词拼接 #include<stdio.h> #include<string.h> #include<algorithm> using name ...

  2. 【转载】Maven安装配置+ GIt&SVN + Jenkins详细配置 软件项目管理 持续集成实验

    原文地址: https://www.cnblogs.com/clownice/p/5395933.html 以下是学习笔记: Jenkins是一款开源持续集成的软件,实现集成的自动化 自动构建工具:M ...

  3. Promise初尝试

    promise.ts export function showAlert() { console.log("开始调用showAlert"); return new Promise( ...

  4. vagrant系列三:vagrant搭建的php7环境

    原文:https://blog.csdn.net/hel12he/article/details/51107236 前面已经把vagrant的基础知识已经基本过了一遍 了,相信只要按着教程来,你已经搭 ...

  5. 开发软件-IntelliJ IDEA:百科

    ylbtech-开发软件-IntelliJ IDEA:百科 IDEA 全称 IntelliJ IDEA,是java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智 ...

  6. VLC播放器web插件接口(Part2)

    本文转自:http://www.educity.cn/wenda/124878.htmlVLC Activex控件(VideoLAN.VLCPlugin.1 VideoLAN.VLCPlugin.2) ...

  7. PAT 甲级 1016 Phone Bills (25 分) (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)

    1016 Phone Bills (25 分)   A long-distance telephone company charges its customers by the following r ...

  8. BAPI_GOODSMVT_CREATE物料凭证增强字段

    项目MSEG 的 BAPI 表增强结构  BAPI_TE_XMSEG 抬头MKPF 的 BAIP 表增强 BAPI_TE_XMKPF 1. 在结构BAPI_TE_XMSEG中appending str ...

  9. Delphi实现树型结构具体实例

    unit Unit1;interfaceuses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, ...

  10. laravel 加载指定版本的mongodb

    composer require jenssegers/mongodb:3.3 注意开启 php的mongodb的扩展 pecl install mongodb