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 ...
随机推荐
- python3 练习题100例 (一)
断断续续的学了很久的python,有很多又忘记了.从今天开始用实例再进行一次学习,并记录.本人小白一个,请大家多多指教. #!/usr/bin/env python3 # -*- coding: ut ...
- json传值给前端页面,出现堆栈溢出问题
用的com.alibaba.fastjson.JSONObject这个包 原因:JSONObject将对象转json字符串时候没有关闭循环引用导致的堆栈溢出. 解决办法是 使用这个 JSONObjec ...
- for循环练习题(1 ,判断任意一个数是91的多少倍 2,编写程序实现给定一个整数判断它从0到这个整数中间出现多少次9的次数)
1 //判断任意一个数是9的多少倍 #include <stdio.h> #include <stdlib.h> int main() { printf("请输入任意 ...
- Cmake3.6.1 下载
下载地址:https://github.com/Kitware/CMake/releases?after=v3.6.2
- spring源码-bean之加载-2
一.前面说了bean的容器初始化,后面当然是说bean的加载.这里还是不讲解ApplicationContext的bean的加载过程,还是通过最基础的XmlBeanFactory来进行讲解,主要是熟悉 ...
- Spring框架之Filter应用
在web.xml中进行配置,对所有的URL请求进行过滤,就像"击鼓传花"一样,链式处理. 配置分为两种A和B. 在web.xml中增加如下内容: <filter> &l ...
- 函数返回const,以便控制访问
#include <stdio.h> class const_out_parameter{ private: ]; public: int* const_out_parameter_tes ...
- unity share current game screen
using UnityEngine; using System.Collections; using UnityEngine.UI; using System.IO; public class Tak ...
- linux初学体会
第一篇随笔,其实是为了写作业,可是老师的要求是对的,其实自己在配环境和做作业的时候也会把遇到的问题的解决方法记录下来,以便以后查找方便.这次借此将那些内容放在这里,也跟大家一起分享下. 上周六算是第二 ...
- APP上下左右滑动屏幕的处理
#获得机器屏幕大小x,y driver = self.driver def getSize(): x = driver.get_window_size()['width'] y = driver.ge ...