Uvaoj 11248 Frequency Hopping(Dinic求最小割)
题意:1到n节点(节点之间有一定的容量),需要流过C的流量,问是否可以?如果可以输出possible, 否则如果可以扩大任意一条边的容量
可以达到目的,那么输出possible option:接着输出每一条可以达到目的的边(按升序),再否则输出not possible
思路:先求一次最大流,如果流量至少为C,则直接输出possible,否则需要修改的弧一定在最小割里!
接着吧这些弧(最小割里的)的容量设为无穷大,然后在求最大流,看最大流的流量能否满足是C即可,如果满足了,那就把这一条边记录下来
分析:最大流的程序没有必要完全的执行完毕,知道当前的流量>=C那么就可以中止最大流的程序!
还有就是第一次的最大流程序如果没有找到>=C的最大流,那么此时的流量留着,下一次在最小割里扩容的时候,总是接着第一次Dinic的流量
继续寻找....
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#define N 105
#define INF 2000000005
using namespace std; struct EDGE{
int u, v, nt, cap;
bool flag;
bool vis;
EDGE(){}
EDGE(int u, int v, int nt, int cap, bool flag):u(u), v(v), nt(nt), cap(cap), flag(flag){}
}; struct node{
int x, y;
node(){}
node(int x, int y) : x(x), y(y){}
}; int pos[]; node ans[];
int preCost[];
int vis[];
int p[];
int pcnt;
int cnt; vector<EDGE>g;
int first[N]; int d[N];
int n, e, c; void addEdge(int u, int v, int c){
g.push_back(EDGE(u, v, first[u], c, true));
first[u] = g.size() - ;
g.push_back(EDGE(v, u, first[v], , false));
first[v] = g.size() - ;
} bool bfs(){
memset(d, , sizeof(d));
d[] = ;
queue<int>q;
q.push();
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = first[u]; ~i; i = g[i].nt){
int v = g[i].v;
if(!d[v] && g[i].cap > ){
d[v] = d[u] + ;
q.push(v);
}
}
}
if(d[n] == ) return false;
return true;
} bool cmp(node a, node b){
if(a.x == b.x)
return a.y < b.y;
return a.x < b.x;
} int leave; int dfs(int u, int totf){
int flow = ;
if(u ==n || totf==) return totf;
for(int i = first[u]; ~i; i = g[i].nt){
int v = g[i].v;
if(d[v] == d[u] + && g[i].cap > ){
int ff = dfs(v, min(totf-flow, g[i].cap));
if(ff > ){
if(!vis[i]){
p[pcnt++]=i;
preCost[i] = g[i].cap;
vis[i] = ;
}
g[i].cap -= ff; if(!vis[i^]){
p[pcnt++]=i^;
preCost[i^] = g[i^].cap;
vis[i^] = ;
}
g[i^].cap += ff;
flow += ff; if(flow >= leave){
flow = leave;
return flow;
} if(totf == flow) break;
}
else d[v] = -;
}
}
return flow;
} bool Dinic(){
leave = c;
while(bfs()){
leave -= dfs(, INF);
if(leave == ) break;
}
if(leave == ) return true;
return false;
} int main(){
int cas = ;
while(scanf("%d%d%d", &n, &e, &c)){
if(!n) break;
memset(first, -, sizeof(first));
g.clear();
cnt = ;
while(e--){
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
addEdge(x, y, z);
}
printf("Case %d: ", ++cas);//这一块差点没有把我气死...居然有一个空格,没有看清楚啊...一直PE. if(n==){
printf("possible\n");
continue;
} if(Dinic()) printf("possible\n");
else{
int len = g.size();
for(int i=; i<len; ++i)
if(g[i].cap == && g[i].flag)
pos[cnt++] = i;//得到最小割
int cc = leave;//第一次Dinic之后,还剩下多少的流量需要流过
int ret = ;
for(int i=; i<cnt; ++i){
c = cc;//新的需要流过的流量
pcnt = ;
g[pos[i]].cap = INF;
memset(vis, , sizeof(vis));
if(Dinic())//如果增广成功,那么这条最小割满足
ans[ret++] = node(g[pos[i]].u, g[pos[i]].v);
for(int j=; j<pcnt; ++j)
g[p[j]].cap = preCost[p[j]];//将Dinic中所经过的边的值恢复成第一次Dinic之后的值!
g[pos[i]].cap = ;
}
if( ret > ){
sort(ans, ans+ret, cmp);
printf("possible option:(%d,%d)", ans[].x, ans[].y);
for(int i=; i<ret; ++i)
printf(",(%d,%d)", ans[i].x, ans[i].y);
printf("\n");
}
else printf("not possible\n");
}
}
return ;
}
Uvaoj 11248 Frequency Hopping(Dinic求最小割)的更多相关文章
- uva 11248 Frequency Hopping (最大流)
uva 11248 Frequency Hopping 题目大意:给定一个有向网络,每条边均有一个容量. 问是否存在一个从点1到点N.流量为C的流.假设不存在,能否够恰好改动一条弧的容量,使得存在这种 ...
- UVA 11248 - Frequency Hopping(网络流量)
UVA 11248 - Frequency Hopping 题目链接 题意:给定一个网络,如今须要从1到N运输流量C,问是否可能,假设可能输出可能,假设不可能,再问能否通过扩大一条边的容量使得可能,假 ...
- [ZJOI2011]最小割 & [CQOI2016]不同的最小割 分治求最小割
题面: [ZJOI2011]最小割 [CQOI2016]不同的最小割 题解: 其实这两道是同一道题.... 最小割是用的dinic,不同的最小割是用的isap 其实都是分治求最小割 简单讲讲思路吧 就 ...
- poj 3469 Dual Core CPU【求最小割容量】
Dual Core CPU Time Limit: 15000MS Memory Limit: 131072K Total Submissions: 21453 Accepted: 9297 ...
- BZOJ_1001_狼抓兔子_(平面图求最小割+对偶图求最短路)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1001 1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec ...
- HDU - 3035 War(对偶图求最小割+最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3035 题意 给个图,求把s和t分开的最小割. 分析 实际顶点和边非常多,不能用最大流来求解.这道题要用 ...
- sw算法求最小割学习
http:// blog.sina.com.cn/s/blog_700906660100v7vb.html 转载:http://www.cnblogs.com/ylfdrib/archive/201 ...
- UVA 11248 Frequency Hopping
Frequency Hopping Time Limit: 10000ms Memory Limit: 131072KB This problem will be judged on UVA. Ori ...
- BZOJ 1001: [BeiJing2006]狼抓兔子(s-t平面图+最短路求最小割)
http://www.lydsy.com/JudgeOnline/problem.php?id=1001 题意: 思路:这道题目是最小割题目,但是吧你直接套用Dinic是会超时的. 这里有种很奇妙的做 ...
随机推荐
- 出现Bad command or the file name的原因
出现Bad command or file name的原因 中文释义:错误的命令或文件名 . 错误原因:不能识别输入的命令 . 方法:检查所输入的指令是否正确,包括拼写和大小写等情况.
- SQL Server 内存中OLTP内部机制概述(三)
----------------------------我是分割线------------------------------- 本文翻译自微软白皮书<SQL Server In-Memory ...
- Speech两种使用方法
COM组件使用speech: public class Speach { private static Speach _Instance = null ; private SpeechLib.SpVo ...
- Advanced Puppet 系列的前言
什么是Advanced 在网络上,你能找到大量关于Puppet的安装,配置以及基础用法的文章和博客.你在通过一段时间的实战后,熟练掌握了Puppet基础用法,随着你管理的集群日益扩大,你的部署逻辑越来 ...
- 解决Visual Studio 2010/2012在调试时lock文件的方法
调试3dsmax插件,有一个避免每次修改插件代码都需要重启3dsmax的方法,就是将导出的核心代码写在一个独立的DLL中,然后在插件代码需要导出时LoadLibrary这个DLL,导出之后再FreeL ...
- 支付宝Wap支付你了解多少?
上几篇文章详细介绍了支付宝APP支付.微信APP支付 此文章来介绍下支付宝Wap支付(也叫作手机网站支付) 目录 1.创建应用并获取APPID 2.配置应用环境 3.配置沙箱环境 4.服务端实现(Ma ...
- Java蛇形数组的简单实现代码
上周五和朋友聊天谈到个蛇形数组的java实现办法,命题是:假设一个二维数组宽w高h,从1开始蛇形输出. int[][] numberMatric = new int[w][h]; 当时午睡过头脑袋不清 ...
- Scala 深入浅出实战经典 第67讲:Scala并发编程匿名Actor、消息传递、偏函数解析
王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...
- asp.net 后台实现删除,划掉效果
效果: name = "<S>" + fircon + "</br>" + "</S>"; 增加“< ...
- .NET 4.6中的性能改进
.NET 4.6中带来了一些与性能改进相关的CLR特性,这些特性中有一部分将会自动生效,而另外一些特性,例如SIMD与异步本地存储(Async Local Storage)则需要对编写应用的方式进行某 ...