题目链接: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(最大流+最小割)的更多相关文章

  1. Uvaoj 11248 Frequency Hopping(Dinic求最小割)

    题意:1到n节点(节点之间有一定的容量),需要流过C的流量,问是否可以?如果可以输出possible, 否则如果可以扩大任意一条边的容量 可以达到目的,那么输出possible option:接着输出 ...

  2. 最大流-最小割 MAXFLOW-MINCUT ISAP

    简单的叙述就不必了. 对于一个图,我们要找最大流,对于基于增广路径的算法,首先必须要建立反向边. 反向边的正确性: 我努力查找了许多资料,都没有找到理论上关于反向边正确性的证明. 但事实上,我们不难理 ...

  3. 最大流&最小割 - 专题练习

    [例1][hdu5889] - 算法结合(BFS+Dinic) 题意 \(N\)个点\(M\)条路径,每条路径长度为\(1\),敌人从\(M\)节点点要进攻\(1\)节点,敌人总是选择最优路径即最短路 ...

  4. matlab练习程序(最大流/最小割)

    学习这个算法是为学习图像处理中的图割算法做准备的. 基本概念: 1.最大流是一个有向图. 2.一个流是最大流,当且仅当它的残余网络中不包括增广路径. 3.最小割就是网络中所有割中值最小的那个割,最小割 ...

  5. 「网络流24题」「LuoguP2774」方格取数问题(最大流 最小割

    Description 在一个有 m*n 个方格的棋盘中,每个方格中有一个正整数.现要从方格中取数,使任意 2 个数所在方格没有公共边,且取出的数的总和最大.试设计一个满足要求的取数算法.对于给定的方 ...

  6. HDU6582 Path【优先队列优化最短路 + dinic最大流 == 最小割】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6582 来源:2019 Multi-University Training Contest 1 题目大意 ...

  7. ISAP 最大流 最小割 模板

    虽然这道题用最小割没有做出来,但是这个板子还是很棒: #include<stdio.h> #include<math.h> #include<string.h> # ...

  8. UVA-11248 Frequency Hopping (最大流+最小割)

    题目大意:给一张网络,问是否存在一条恰为C的流.若不存在,那是否存在一条弧,使得改动这条弧的容量后能恰有为C的流? 题目分析:先找出最大流,如果最大流不比C小,那么一定存在一条恰为C的流.否则,找出最 ...

  9. Codeforces 965 枚举轮数贪心分糖果 青蛙跳石头最大流=最小割思想 trie启发式合并

    A /*#include<cstring>#include<algorithm>#include<queue>#include<vector>#incl ...

随机推荐

  1. JAAS - Document

    JAAS 参考文档: JAAS Reference Guide JAAS Authentication Tutorial JAAS Authorization Tutorial LoginModule ...

  2. (转)VS2012网站发布详细步骤

    2.弹出网站发布设置面板,点击<新建..>,创建新的发布配置文件: 4. 在配置中,要选择“Release”——发布模式(Release   称为发布版本,它往往是进行了各种优化,使得程序 ...

  3. ActionScript:Resampling PCM data

    使用基于flash的麦克风录音,如果想在获取完PCM采样数据后,通过Sound马上回放,必须经过resampling.(注意:如果录音是采用的44KHz的话,则不需要) 因此,需要as实现一个简便的函 ...

  4. 转: Oracle AWR 报告 每天自动生成并发送邮箱

    原贴地址:http://www.cnblogs.com/vigarbuaa/archive/2012/09/05/2671794.html Oracle AWR 介绍http://blog.csdn. ...

  5. Alljoyn 概述(1)

    Alljoyn Overview Feb. 2012- AllJoyn 是什么? • 2011年2月9日发布,由 QuiC(高通创新中心)开发维护的开源软 件项目,采用 Apache license ...

  6. NSArray 跟 NSMutableArray 使用 区别

    NSArray 可变数组 一.NSArray是静态数组,创建后数组内容及长度不能再修改. 实例: //用arrayWithObjects初始化一个不可变的数组对象. //初始化的值之间使用逗号分开,以 ...

  7. 读懂IL代码(一)

    以前刚开始学C#的时候,总有高手跟我说,去了解一下IL代码吧,看懂了你能更加清楚的知道你写出来的代码是如何运行互相调用的,可是那时候没去看,后来补的,其实感觉也不晚.刚开始看IL代码的时候,感觉非常吃 ...

  8. Vijos1734 NOI2010 海拔 平面图最小割

    建立平面图的对偶图,把最小割转化成最短路问题 Dijkstra算法堆优化 (被输入顺序搞WA了好几次T_T) #include <cstdio> #include <cstring& ...

  9. js submit的問題

    form 里面有input name="submit"的时候 $('#seachform').submit();不起作用

  10. [C#]获取最近在Windows上所使用的文件

    class RecentlyFileHelper { public static string GetShortcutTargetFile(string shortcutFilename) { var ...