UVa11248 Frequency Hopping(最大流+最小割)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33206
【思路】
最大流最小割。
可以确定的是如果不可行需要修改的是流量已经达到上限的最小割中的边。可以考虑依次修改求最大流。
优化:1 在原最大流的基础上增广; 2 只增广到流量C为止。
【代码】
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std; const int maxn = +;
const int INF = 1e9*+; struct Edge{
int u,v,cap,flow;
bool operator<(const Edge& rhs) const{
return u<rhs.u || (u==rhs.u && v<rhs.v) ;
}
};
struct Dinic {
int n,m,s,t;
bool vis[maxn];
int d[maxn],cur[maxn];
vector<int> G[maxn];
vector<Edge> es; void init(int n) {
this->n=n;
es.clear();
for(int i=;i<n;i++) G[i].clear();
}
void clearflow() {
for(int i=;i<es.size();i++) es[i].flow=;
}
void AddEdge(int u,int v,int cap) {
es.push_back((Edge){u,v,cap,});
es.push_back((Edge){v,u,,});
m=es.size();
G[u].push_back(m-);
G[v].push_back(m-);
} bool BFS() {
queue<int> q;
memset(vis,,sizeof(vis));
q.push(s); vis[s]=; d[s]=;
while(!q.empty()) {
int u=q.front(); q.pop();
for(int i=;i<G[u].size();i++) {
Edge& e=es[G[u][i]];
int v=e.v;
if(!vis[v] && e.cap>e.flow) {
vis[v]=;
d[v]=d[u]+;
q.push(v);
}
}
}
return vis[t];
}
int DFS(int u,int a) {
if(u==t || a==) return a;
int flow=,f;
for(int& i=cur[u];i<G[u].size();i++){
Edge& e=es[G[u][i]];
int v=e.v;
if( d[v]==d[u]+ && (f=DFS(v,min(a,e.cap-e.flow)))> ) {
e.flow+=f;
es[G[u][i]^].flow-=f;
flow+=f,a-=f;
if(!a) break;
}
}
return flow;
}
int Maxflow(int s,int t,int limit) {
this->s=s , this->t=t;
int flow=;
while(BFS() && flow<limit) {
memset(cur,,sizeof(cur));
flow+=DFS(s,INF);
}
return flow;
}
vector<int> Mincut() {
vector<int> ans;
for(int i=;i<es.size();i++)
if(es[i].cap!= && es[i].cap==es[i].flow)
ans.push_back(i);
return ans;
}
void reduce() {
for(int i=;i<es.size();i++) es[i].cap-=es[i].flow;
}
}dc; int n,m,C; int main() {
int kase=;
while(scanf("%d%d%d",&n,&m,&C)== && n) {
dc.init(n);
int u,v,w;
for(int i=;i<m;i++) {
scanf("%d%d%d",&u,&v,&w);
u--,v--;
dc.AddEdge(u,v,w);
}
int flow=dc.Maxflow(,n-,INF);
printf("Case %d: ",++kase);
if(flow>=C) printf("possible\n");
else {
vector<int> cut;
vector<Edge> ans;
cut=dc.Mincut();
dc.reduce(); //考虑除去最大流之后的剩余网络//即在原流基础上增广
for(int i=;i<cut.size();i++) {
dc.clearflow();
Edge& e=dc.es[cut[i]];
int tmp=e.cap;
e.cap=C;
if(flow+dc.Maxflow(,n-,C-flow)>=C) ans.push_back(e);
e.cap=tmp;
}
if(!ans.size()) printf("not possible\n");
else {
sort(ans.begin(),ans.end());
printf("possible option:");
int d=ans.size();
for(int i=;i<d-;i++) printf("(%d,%d),",ans[i].u+,ans[i].v+);
printf("(%d,%d)\n",ans[d-].u+,ans[d-].v+);
}
}
}
return ;
}
UVa11248 Frequency Hopping(最大流+最小割)的更多相关文章
- Uvaoj 11248 Frequency Hopping(Dinic求最小割)
题意:1到n节点(节点之间有一定的容量),需要流过C的流量,问是否可以?如果可以输出possible, 否则如果可以扩大任意一条边的容量 可以达到目的,那么输出possible option:接着输出 ...
- 最大流-最小割 MAXFLOW-MINCUT ISAP
简单的叙述就不必了. 对于一个图,我们要找最大流,对于基于增广路径的算法,首先必须要建立反向边. 反向边的正确性: 我努力查找了许多资料,都没有找到理论上关于反向边正确性的证明. 但事实上,我们不难理 ...
- 最大流&最小割 - 专题练习
[例1][hdu5889] - 算法结合(BFS+Dinic) 题意 \(N\)个点\(M\)条路径,每条路径长度为\(1\),敌人从\(M\)节点点要进攻\(1\)节点,敌人总是选择最优路径即最短路 ...
- matlab练习程序(最大流/最小割)
学习这个算法是为学习图像处理中的图割算法做准备的. 基本概念: 1.最大流是一个有向图. 2.一个流是最大流,当且仅当它的残余网络中不包括增广路径. 3.最小割就是网络中所有割中值最小的那个割,最小割 ...
- 「网络流24题」「LuoguP2774」方格取数问题(最大流 最小割
Description 在一个有 m*n 个方格的棋盘中,每个方格中有一个正整数.现要从方格中取数,使任意 2 个数所在方格没有公共边,且取出的数的总和最大.试设计一个满足要求的取数算法.对于给定的方 ...
- HDU6582 Path【优先队列优化最短路 + dinic最大流 == 最小割】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6582 来源:2019 Multi-University Training Contest 1 题目大意 ...
- ISAP 最大流 最小割 模板
虽然这道题用最小割没有做出来,但是这个板子还是很棒: #include<stdio.h> #include<math.h> #include<string.h> # ...
- UVA-11248 Frequency Hopping (最大流+最小割)
题目大意:给一张网络,问是否存在一条恰为C的流.若不存在,那是否存在一条弧,使得改动这条弧的容量后能恰有为C的流? 题目分析:先找出最大流,如果最大流不比C小,那么一定存在一条恰为C的流.否则,找出最 ...
- Codeforces 965 枚举轮数贪心分糖果 青蛙跳石头最大流=最小割思想 trie启发式合并
A /*#include<cstring>#include<algorithm>#include<queue>#include<vector>#incl ...
随机推荐
- HTML5 文件域+FileReader 分段读取文件并上传(八)-WebSocket
一.同时上传多个文件处理 HTML: <div class="container"> <div class="panel panel-default&q ...
- Struts2 中拦截器和Action的调用关系(写的很好)
http://blog.csdn.net/hackerain/article/details/6991082
- VLC命令参数(转载)
转载自: http://blog.csdn.net/bytxl/article/details/6613449 http://www.cnblogs.com/MikeZhang/archive/201 ...
- 阿里云 centos vim 显示中文 乱码
开始以为是vim 设置编码的问题 :网上搜 改 .vimrc 无效!!! 后转战 是不是系统里面没有中文字体 1.先从你本机 C:\Windows\Fonts 拷贝或者网络上下载你想要安装的 ...
- PHP获取每月第一天与最后一天
<?phpfunction getthemonth($date){$firstday = date('Y-m-01', strtotime($date));$lastday = date('Y- ...
- CSS让图片垂直居中的几种技巧 三种方法介绍
在网页设计过程中,有时候会希望图片垂直居中的情况.而且,需要垂直居中的图片的高度也不确定,这就会给页面的布局带来一定的挑战.下面总结了一下,曾经使用过的几种方法来使图片垂直居中,除了第一种方法只限于标 ...
- Android Fragment基础及使用
同一个app内的界面切换 用Fragment比较合适,因为Activity比较重量级 Fragment 轻量级,切换灵活 --------------------------------------- ...
- python读写Excel文件的函数--使用xlrd/xlwt
python中读取Excel的模块或者说工具有很多,如以下几种: Packages 文档下载 说明 openpyxl Download | Documentation | Bitbucket The ...
- uboot总结:uboot配置和启动过程3(config.mk分析)
说明:文件位置:在uboot的目录下,文件名为:config.mk.是一个makefile文件,以后会被主Makefile调用. 它的主要作用的是: (1)具体的设置交叉编译工具链接(主Makefil ...
- Dao操作的抽取,BaseDao
Dao操作通用的步骤: 0. 写SQL语句 1. 获取连接 2. 创建stmt 3. 执行sql a) 更新 b) 查询 4. 关闭/异常 代码: BaseDao /** * 通用的dao,自己写的所 ...