Flow Problem HDU - 3549
Flow Problem HDU - 3549
InputThe 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)OutputFor 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
Sample Output
Case 1: 1
Case 2: 2 题意:网络流模版题,1到n的结点的最大的流
思路:用dinic
#include<stdio.h>
#include<iostream>
#include<map>
#include<string.h>
#include<queue>
#include<vector>
#include<math.h>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int dis[];
int flow[][];
int n,m;
int bfs()
{
memset(dis,-,sizeof(dis));
queue<int>Q;
dis[]=;
Q.push();
while(!Q.empty())
{
int top=Q.front();
Q.pop();
for(int i=;i<=n;i++)
{
if(flow[top][i]>&&dis[i]<)
{
dis[i]=dis[top]+;
Q.push(i);
}
}
}
if(dis[n]>)return ;
return ;
}
int dinic(int x,int k)
{
if(x==n)
return k;
int y;
for(int i=;i<=n;i++)
{
if(flow[x][i]>&&dis[i]==dis[x]+&&(y=dinic(i,min(k,flow[x][i]))))
{
flow[x][i]-=y;
flow[i][x]+=y;
return y;
}
}
return ; }
int main()
{
int t;
scanf("%d",&t);
int cas=;
while(t--)
{
memset(flow,,sizeof(flow));
scanf("%d%d",&n,&m);
int a,b,c;
for(int i=;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
flow[a][b]+=c;
}
int ans=;
while(bfs())
{
//cout<<"++"<<endl;
int res;
while(res=dinic(,inf))ans+=res;
}
printf("Case %d: %d\n",cas++,ans);
}
}
用sap加邻接矩阵,maze那里是叠加,不是覆盖
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<cstdlib>
#include <vector>
#include<queue>
using namespace std; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
const int maxn = 1e3+;
const int mod = 1e9+; int maze[maxn][maxn];
int gap[maxn],dis[maxn],pre[maxn],cur[maxn]; int sap(int start,int end ,int nodenum)
{
memset(cur,,sizeof cur);
memset(dis,,sizeof dis);
memset(gap,,sizeof gap);
int u = pre[start] = start,maxflow = ,aug = -;
gap[] = nodenum;
while(dis[start] < nodenum)
{
loop:
for(int v = cur[u];v < nodenum; v++)
if(maze[u][v] && dis[u] == dis[v] + ){
if(aug == - || aug > maze[u][v])
aug = maze[u][v];
pre[v] = u;
u = cur[u] = v;
if(v == end)
{
maxflow += aug;
for(u = pre[u]; v!=start;v=u,u=pre[u])
{
maze[u][v] -= aug;
maze[v][u] += aug;
}
aug = -;
}
goto loop;
}
int mindis = nodenum - ;
for(int v = ;v<nodenum;v++)
if(maze[u][v] && mindis > dis[v])
{
cur[u] = v;
mindis = dis[v];
}
if((--gap[dis[u]]) == )
break;
gap[dis[u] = mindis + ]++;
u = pre[u]; }
return maxflow;
} int main()
{
int t;
scanf("%d",&t);
for(int ca = ;ca <= t; ca++)
{
memset(maze,,sizeof maze);
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<m;i++)
{
int a,b,l;
scanf("%d%d%d",&a,&b,&l);
maze[a-][b-] += l;
}
int res = sap(,n-,n);
printf("Case %d: %d\n",ca,res);
}
}
Flow Problem HDU - 3549的更多相关文章
- HDU 3549 Flow Problem(最大流)
HDU 3549 Flow Problem(最大流) Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...
- hdu 3549 Flow Problem
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Description Network flow is a well- ...
- hdu 3549 Flow Problem【最大流增广路入门模板题】
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Time Limit: 5000/5000 MS (Java/Others ...
- 网络流 HDU 3549 Flow Problem
网络流 HDU 3549 Flow Problem 题目:pid=3549">http://acm.hdu.edu.cn/showproblem.php?pid=3549 用增广路算法 ...
- hdu 3549 Flow Problem Edmonds_Karp算法求解最大流
Flow Problem 题意:N个顶点M条边,(2 <= N <= 15, 0 <= M <= 1000)问从1到N的最大流量为多少? 分析:直接使用Edmonds_Karp ...
- HDU 3549 Flow Problem 网络流(最大流) FF EK
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Tot ...
- hdu 3549 Flow Problem (网络最大流)
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tota ...
- HDU 3549 基础网络流EK算法 Flow Problem
欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit ...
- HDU 3549 Flow Problem (最大流ISAP)
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tota ...
随机推荐
- Primefaces dataTable设置某个cell的样式问题
设置primefaces dataTable的源网段列的Cell可以编辑,当回车键保存时,判断是否输入的网段合法,如果不合法就显示警告信息,并将这个不合法的数据用红色表示.问题是,怎么给这一个cell ...
- electron 开发记录
判断是否开发环境 安装 electron-is-dev npm install electron-is-dev // main.js const isDev = require('electron-i ...
- 《C#高效编程》读书笔记06-理解几个等同性判断之间的关系
当创建自定义类型时(无论是class还是struct),应为类型定义"等同性"的含义.C#提供了4种不同的函数来判断两个对象是否"相等": public sta ...
- 并发包阻塞队列之ArrayBlockingQueue
并发包阻塞队列之ArrayBlockingQueue jdk1.7.0_79 上一节中对并发包中的非阻塞队列ConcurrentLinkedQueue的入队.出队做了一个简要的分析,本文将对并发 ...
- 前端WEB编辑器-------webstrom
欲先善其事,必先利其器,如题.看到网上一篇介绍webstrom的文章,觉得功能确实强大,也知道为什么阿里巴巴的前端传到github上的文件为啥都有一个 .idea 文件,(传说淘宝内部推荐写js用we ...
- String与Date转换
public class TimeTraining { public static void changeStr(String str){ str = "137878"; } pu ...
- 设置mapcontrol的鼠标样式
http://blog.itpub.net/14999074/viewspace-586515/ mapcontrol的鼠标样式 this.axMapControl1.MousePointer=esr ...
- mybatis-映射器的CRUD
设计步骤:model.mapper.dao.service.junit单元测试.log4j日志 项目和之前的一样在此只是创建了test和修改了mapper 1.修改映射 1.1修改接口 package ...
- 流媒体 6——MPEG电视
1.电视图像的数据率 1.1 ITU-R BT.601标准数据率 按照奈奎斯特(Nyquist)采样理论,模拟电视信号经过采样(把连续的时间信号变成离散的时间信号)和量化 (把连续的幅度变成离散的幅度 ...
- shp格式数据发布服务:postGIS + postgresql + geoserver
主要流程: ①使用postgresql创建数据库 ②下载安装postgis插件 ③在创建的数据库中使用postgis插件,执行下列语句 CREATE EXTENSION postgis; CREATE ...