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. uiautomator2 wifi连接手机

    [实施方法] 手机和电脑同时连接到同一个wifi上 1.开启远程adb #开启远端adb,这一步需要手机通过USB连接到电脑 adb tcpip 5555 #结果如下:restarting in TC ...

  2. 重新部署环境之后,总是提示表doesn't have a default value

    SQLSTATE[HY000]: General error: 1364 Field 'college' doesn't have a default value   数据库严格模式开启了,解决方法: ...

  3. embeddable persistent key-value store for fast storage

    A persistent key-value store for fast storage environmentsRocksDB is an embeddable persistent key-va ...

  4. linux内核中rtc框架选用什么接口来注册rtc设备呢?

    1. 有哪些接口? 1.1 devm_rtc_device_register 1.2  devm_rtc_allocate_device和 rtc_register_device 2. 1.1与1.2 ...

  5. docker下搭建owncloud

    在ubuntu下 搭建owncloud 用docker-compose启动,owncloud.yml文件内容 owncloud: image: owncloud: restart: always 开机 ...

  6. C之指针

    什么是指针 * 指针变量:用来存储某种数据在内存中的地址.* 世面上书籍一般把指针和指针变量的概念混在一起了.市面上的书籍说的指针指的就是指针变量 Ø *号的三种含义1. 两个数相乘int i =5; ...

  7. 关于在Vue中,只要单个列表显示模态框的做法。

    1.在后台返回的数组对象中,添加一个自定义属性,这个属性用于控制模态框的显示.2.在事件中传入该列表的索引参数,然后在事件方法中找到数组相对应的下标,更改自定义属性便可

  8. Qt编写自定义控件31-面板仪表盘控件

    一.前言 在Qt自定义控件中,仪表盘控件是数量最多的,写仪表盘都写到快要吐血,可能是因为各种工业控制领域用的比较多吧,而且仪表盘又是比较生动直观的,这次看到百度的echart中有这个控件,所以也来模仿 ...

  9. 标签 <i>

    <i>的使用 效果图

  10. swift 第二课 基础知识-2

    setter 和getter 的使用--> 适合计算使用 struct Point { var x = 0.0, y=0.0 } struct Size { var width = 0.0, h ...