ACM 第六天
图论 网络流
最大流 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
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
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
* Lines 2..M+1: Three space-separated integers that define a path: The starting field, the end field, and the path's length.
Output
Sample Input
4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2
Sample Output
6
建图:超级源点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 第六天的更多相关文章
- ACM北大暑期课培训第六天
今天讲了DFA,最小生成树以及最短路 DFA(接着昨天讲) 如何高效的构造前缀指针: 步骤为:根据深度一一求出每一个节点的前缀指针.对于当前节点,设他的父节点与他的边上的字符为Ch,如果他的父节点的前 ...
- SCNU ACM 2016新生赛决赛 解题报告
新生初赛题目.解题思路.参考代码一览 A. 拒绝虐狗 Problem Description CZJ 去排队打饭的时候看到前面有几对情侣秀恩爱,作为单身狗的 CZJ 表示很难受. 现在给出一个字符串代 ...
- SCNU ACM 2016新生赛初赛 解题报告
新生初赛题目.解题思路.参考代码一览 1001. 无聊的日常 Problem Description 两位小朋友小A和小B无聊时玩了个游戏,在限定时间内说出一排数字,那边说出的数大就赢,你的工作是帮他 ...
- acm结束了
最后一场比赛打完了.之前为了记录一些题目,开了这个博客,现在结束了acm,这个博客之后也不再更新了. 大家继续加油!
- 关于ACM的总结
看了不少大神的退役帖,今天终于要本弱装一波逼祭奠一下我关于ACM的回忆. 从大二上开始接触到大三下结束,接近两年的时间,对于大神们来说两年的确算不上时间,然而对于本弱来说就是大学的一半时光.大一的懵懂 ...
- 第一届山东省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 ...
- 第一届山东省ACM——Balloons(java)
Description Both Saya and Kudo like balloons. One day, they heard that in the central park, there wi ...
- ACM之鸡血篇
一匹黑马的诞生 故事还要从南京现场赛讲起,话说这次现场赛,各路ACM英雄豪杰齐聚南京,为争取亚洲总舵南京分舵舵主之职位,都使出了看 家本领,其中有最有实力的有京城两大帮清华帮,北大帮,南郡三大派上交派 ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
随机推荐
- vue的细节
1.如果使用路由跳转到别的界面的话,例如从文章list页面跳转到具体文章查看详情页,查看某一个具体就需要传递那个文章的id去后台查询, this.$router.push的params方法可以实现传递 ...
- 【 es搜索】
地图搜索实现: ①参数: 左下角经纬度和右上角经纬度 图层数(zoom) 关键字等各种数据库中的字段 排序方式 具体的坐标点+距离 ②实现 a.用es作为关系库,首先先mapping所有的字段,然后用 ...
- Flume:source和sink
Flume – 初识flume.source和sink 目录基本概念常用源 Source常用sink 基本概念 什么叫flume? 分布式,可靠的大量日志收集.聚合和移动工具. events ...
- PyPI - Datetime
PyPI for Python 3.7 import datetime https://docs.python.org/3.7/library/datetime.html timedelta Obje ...
- Qt——事件
1.常见事件 [1]鼠标事件 (1)坐标 x(),y(), 相对windows globalX() globalY() (2)获得点击 button() [2]键盘事件 [3]定时器事件 timerI ...
- Go 入门 - 控制流
主要内容来自中文版的官方教程Go语言之旅 目的为总结要点 循环 Go 只有 for循环 for 由三部分组成,用分号间隔开 初始化语句:在第一次迭代之前执行,通常为一句短变量声明(i:=0) 条件表达 ...
- 传说是小米家的一道面试题难倒了某Java程序员。扑克牌排序问题。
网上说的是有位网友在面试小米Java岗三次后,终于挺进了第三轮面试,结果还是败在了两道算法题上面. 1.写个读方法和写方法,实现读写锁 2.一副从1到n的牌,每次从牌堆顶取一张放桌子上,再取一张放牌堆 ...
- Java设计模式(23)——行为模式之访问者模式(Visitor)
一.概述 概念 作用于某个对象群中各个对象的操作.它可以使你在不改变这些对象本身的情况下,定义作用于这些对象的新操作. 引入 试想这样一个场景,在一个Collection中放入了一大堆的各种对象的引用 ...
- JavaScript---复选框全选的多种实现
<script language=javascript> //第一种方法 function selectall1() { var a = document.getElementsBy ...
- 金山WPS面试题
1.windows的handle 1)是一个宏定义#define void* HANDLE 2) HANDLE提供了一种统一的方式去获得系统资源,并对其进行操作. 3) HANDLE使得程序设计的细节 ...