图论 网络流

最大流 INF(初始值)

路径上权值最小的边,决定流量大小。

流量网络的三个特性:

①流量控制

②反对称性

③流量守恒

残余网络:保留了c(e)容量<f(e)流量【可以继续流,因为还有f(e)-c(e)的流量】和 c(e)>0的反向边【可以回退】。

增广路定理:网络中达到最大流当且仅当残余网络中不存在增广路。

所以总结做题方法就是不断找增广路,有三种方法:

①Ford-Fvkerson(DFS)

伪代码:

不断地在残余网络中找增广路伪代码:

FF{

flow=0;

while(true)

{

}

DFS遍历传参伪代码过程:

dfs(int now,int t,int cnt)

{

if(now==t) return cnt;

for(i)

if(!vis[i] && cap>0)

vis[i]=true;

d=dfs(next,t,min(cnt,cap)

if(d>0)

else

return 0;

}

两个技巧:

①邻接表

②邻接矩阵 [i][j]=cap [j][i]=0;

F - Flow Problem

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.
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
 #include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
queue <int> q;
int n,m,gra[][],path[],flow[],st,ed;
int bfs()
{
int i,t;
while(!q.empty()) q.pop();
memset(path,-,sizeof(path));
path[st]=;
flow[st]=;
q.push(st);
while(!q.empty())
{
t=q.front();
q.pop();
if(t==ed) break;
for(i=;i<=n;i++)
{
if(i!=st && path[i]==- && gra[t][i])
{
flow[i]=flow[t]<gra[t][i]?flow[t]:gra[t][i];
q.push(i);
path[i]=t;
}
}
}
if(path[ed]==-) return -;
return flow[n];
} int ek()
{
int max_flow=,step,now,pre;
while((step=bfs())!=-)
{
max_flow+=step;
now=ed;
while(now!=st)
{
pre=path[now];
gra[pre][now]-=step;
gra[now][pre]+=step;
now=pre;
}
}
return max_flow;
} int main()
{
int i,u,v,cost,T,c,d,e,cas=;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
memset(gra,,sizeof(gra));
for(i=;i<=m;i++)
{
scanf("%d%d%d",&c,&d,&e);
gra[c][d]+=e;
}
st=;ed=n;
printf("Case %d: %d\n",++cas,ek());
}
return ;
}

H - Farm Tour

When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first of which contains his house and the Nth of which contains the big barn. A total M (1 <= M <= 10000) paths that connect the fields in various ways. Each path connects two different fields and has a nonzero length smaller than 35,000.

To show off his farm in the best way, he walks a tour that
starts at his house, potentially travels through some fields, and ends
at the barn. Later, he returns (potentially through some fields) back to
his house again.

He wants his tour to be as short as possible, however he
doesn't want to walk on any given path more than once. Calculate the
shortest tour possible. FJ is sure that some tour exists for any given
farm.

Input

* Line 1: Two space-separated integers: N and M.

* Lines 2..M+1: Three space-separated integers that define a path: The starting field, the end field, and the path's length.

Output

A single line containing the length of the shortest tour.

Sample Input

4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2

Sample Output

6
【题意】:给你N个农田、M条无向边以及每条边的长度。现在让你从1走到N再从N走回1,要求不能走重复的边。问你所走的路径总长的最小值。题目保证1到N至少会存在两条边不重复的路径。
【思路】:最小费用最大流问题:首先设置一个超级源点0,一个超级汇点N+1,因为是1到N的双源最短路,所以直接将0与1相连,cap为2,N与N+1相连,cap为2,因为是无向图,所以每条边要连两次。

建图:超级源点source,超级汇点sink

1,source连点1,容量为2,费用为0;

2,对题目给出的无向边<u, v>建双向边,容量为1(意味着该边只能走一次),费用为边的长度;

3,N到sink建边,容量为2,费用为0。

最后跑一次最小费用最大流就可以了。

 #include <cstdio>
#include <cstring>
#include <queue>
#define MAXN 1010
#define MAXM 50000+10
using namespace std;
struct Edge
{
int from, to, cap, flow, cost, next;
};
Edge edge[MAXM];
int head[MAXN], edgenum;
int dist[MAXN];
int pre[MAXN];
bool vis[MAXN];
int N, M;
int source, sink;
void init()
{
edgenum = ;
memset(head, -, sizeof(head));
}
void addEdge(int u, int v, int w, int c) //加边uv
{
Edge E1 = {u, v, w, , c, head[u]};
edge[edgenum] = E1;
head[u] = edgenum++;
Edge E2 = {v, u, , , -c, head[v]};
edge[edgenum] = E2;
head[v] = edgenum++;
}
void getMap()
{
int a, b, c;
source = , sink = N+;
while(M--)
{
scanf("%d%d%d", &a, &b, &c);
addEdge(a, b, , c);//建双向边
addEdge(b, a, , c);
}
addEdge(source, , , );//超级源点连起点
addEdge(N, sink, , );//终点连超级汇点
}
bool SPFA(int s, int t) //spfa算法求最小流
{
queue<int> Q;
memset(dist, 0x3f3f3f3f, sizeof(dist));
memset(vis, false, sizeof(vis));
memset(pre, -, sizeof(pre));
dist[s] = ;
vis[s] = true;
Q.push(s);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
vis[u] = false;
for(int i = head[u]; i != -; i = edge[i].next)
{
Edge E = edge[i];
if(dist[E.to] > dist[u] + E.cost && E.cap > E.flow)
{
dist[E.to] = dist[u] + E.cost;
pre[E.to] = i;
if(!vis[E.to])
{
vis[E.to] = true;
Q.push(E.to);
}
}
}
}
return pre[t] != -;
}
void MCMF(int s, int t, int &cost)//最小费用最大流算法
{
cost = ;
while(SPFA(s, t))
{
int Min = 0x3f3f3f3f;
for(int i = pre[t]; i != -; i = pre[edge[i^].to])
{
Edge E = edge[i];
Min = min(Min, E.cap - E.flow);
}
for(int i = pre[t]; i != -; i = pre[edge[i^].to])
{
edge[i].flow += Min;
edge[i^].flow -= Min;
cost += edge[i].cost * Min;
}
}
}
int main()
{
while(scanf("%d%d", &N, &M) != EOF)
{
init();
getMap();
int cost;//设置最小费用
MCMF(source, sink, cost);
printf("%d\n", cost);
}
return ;
}

ACM 第六天的更多相关文章

  1. ACM北大暑期课培训第六天

    今天讲了DFA,最小生成树以及最短路 DFA(接着昨天讲) 如何高效的构造前缀指针: 步骤为:根据深度一一求出每一个节点的前缀指针.对于当前节点,设他的父节点与他的边上的字符为Ch,如果他的父节点的前 ...

  2. SCNU ACM 2016新生赛决赛 解题报告

    新生初赛题目.解题思路.参考代码一览 A. 拒绝虐狗 Problem Description CZJ 去排队打饭的时候看到前面有几对情侣秀恩爱,作为单身狗的 CZJ 表示很难受. 现在给出一个字符串代 ...

  3. SCNU ACM 2016新生赛初赛 解题报告

    新生初赛题目.解题思路.参考代码一览 1001. 无聊的日常 Problem Description 两位小朋友小A和小B无聊时玩了个游戏,在限定时间内说出一排数字,那边说出的数大就赢,你的工作是帮他 ...

  4. acm结束了

    最后一场比赛打完了.之前为了记录一些题目,开了这个博客,现在结束了acm,这个博客之后也不再更新了. 大家继续加油!

  5. 关于ACM的总结

    看了不少大神的退役帖,今天终于要本弱装一波逼祭奠一下我关于ACM的回忆. 从大二上开始接触到大三下结束,接近两年的时间,对于大神们来说两年的确算不上时间,然而对于本弱来说就是大学的一半时光.大一的懵懂 ...

  6. 第一届山东省ACM——Phone Number(java)

    Description We know that if a phone number A is another phone number B’s prefix, B is not able to be ...

  7. 第一届山东省ACM——Balloons(java)

    Description Both Saya and Kudo like balloons. One day, they heard that in the central park, there wi ...

  8. ACM之鸡血篇

    一匹黑马的诞生 故事还要从南京现场赛讲起,话说这次现场赛,各路ACM英雄豪杰齐聚南京,为争取亚洲总舵南京分舵舵主之职位,都使出了看 家本领,其中有最有实力的有京城两大帮清华帮,北大帮,南郡三大派上交派 ...

  9. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

随机推荐

  1. python学习笔记:第6天 小数据池和编码转换

    目录 1. id 和 == 2. 小数据池 3. 编码和解码 1. id 和 == id:id是一个内置的函数,可以查看变量存放的内存地址(实际上不是真正的物理地址,这里暂时这样理解),用于判断是变量 ...

  2. mac安装ruby-oci8

    1.安装xcode 2.从oracle官网下载以下安装包 instantclient-basic-macos.x64-12.1.0.2.0.zip instantclient-sdk-macos.x6 ...

  3. golang 多维哈希(map,hashmap)实践随笔

    有些场景使用多维哈希来存储数据,时间复杂度恒定,简单粗暴好用.这里记录一下. 如下是三维哈希的简单示意图,建议层数不要太多,否则时间久了,自己写的代码都不认识. 下图是三维哈希在内存的存储形式,has ...

  4. Java中的IO

    引言:     对程序语言的设计者来说,创建一个好的输入/输出(I/O)系统是一项艰难的任务 < Thinking in Java >   本文的目录视图如下:   Java IO概要 a ...

  5. 20154327 Exp1 PC平台逆向破解

    一.实践目标 1.运行原本不可访问的代码片段 2.强行修改程序执行流 3.以及注入运行任意代码 二.基础知识 1.直接修改程序机器指令,改变程序执行流程 2.通过构造输入参数,造成BOF攻击,改变程序 ...

  6. 北京Uber优步司机奖励政策(12月19日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  7. 天津市人民优步Uber司机奖励政策(8.31-9.6)

    "*结算周期为周一凌晨4点至下周一凌晨4点 滴滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单 ...

  8. day 2 飞机大战原理

    1. 程序的图片的坐标 (左上角为顶点) 2.图片变成动态的 3.集成显卡 和独立显卡

  9. (AOSP)repo checkout指定版本

    aosp 怎么切换分支? To properly switch Android version, all you need to change is branch for your manifest ...

  10. Android Parcelable 源码解析

    大家都知道,要想在Intent里面传递一些非基本类型的数据,有两种方式,一种实现Parcelable,另一种是实现Serializable接口. 今天先不说Serializable 接口,只说Parc ...