UVA11248 网络扩容(枚举割边扩充)
题意:
给你一个有向图,问你从1到n的最大流是多少?如果流量大于等于C那么直接输出一个串,否则输出只扩充一条边的流量就可以达到1->n大于等于C的所有边,如果扩充不了就
输出另一个串。
Sample Input
4 4 5
1 2 5
1 3 5
2 4 5
3 4 5
4 4 5
1 2 1
1 3 5
2 4 5
3 4 1
4 4 5
1 2 1
1 3 1
2 4 1
3 4 1
0 0 0
Output for Sample Input
Case 1: possible
Case 2: possible option:(1,2),(3,4)
Case 3: not possible
思路:
很容易想到的一点就是扩展后有作用的点肯定是割边,那么我们可以先跑一遍最大流把割边找出来,然后枚举割边,扩充割边流量,看最大流是否大于等于C,但是这样会TLE,有两个比较有用的优化,就是每次都在残余网络上改流量,然后加上残余网络之前跑出来的流量,还有一个优化就是跑最大流的时候,如果当前流量大于等于C了就已经满足了,没必要再跑了。
#include<queue>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N_node 100 + 5
#define N_edge 20000 + 10
#define INF 2005000000
using namespace std;
typedef struct
{
int from ,to ,next;
long long cost;
}STAR;
typedef struct
{
int x ,t;
}DEP;
typedef struct
{
int a ,b;
}EDGE;
STAR E[N_edge] ,mkE[N_edge];
EDGE edge[N_edge] ,Ans_Edge[N_edge];
DEP xin ,tou;
int list[N_node] ,list2[N_node] ,mklist[N_node] ,tot;
int deep[N_node];
void add(int a ,int b ,long long c)
{
E[++tot].from = a;
E[tot].to = b;
E[tot].cost = c;
E[tot].next = list[a];
list[a] = tot;
E[++tot].from = b;
E[tot].to = a;
E[tot].cost = 0;
E[tot].next = list[b];
list[b] = tot;
}
bool camp(EDGE a ,EDGE b)
{
return a.a < b.a || a.a == b.a && a.b < b.b;
}
long long minn(long long a ,long long b)
{
return a < b ? a : b;
}
bool BFS_Deep(int s ,int t ,int n)
{
memset(deep ,255 ,sizeof(deep));
xin.x = s ,xin.t = 0;
deep[s] = 0;
queue<DEP>q;
q.push(xin);
while(!q.empty())
{
tou = q.front();
q.pop();
for(int k = list[tou.x] ;k ;k = E[k].next)
{
xin.x = E[k].to;
xin.t = tou.t + 1;
if(deep[xin.x] != -1 || !E[k].cost)
continue;
deep[xin.x] = xin.t;
q.push(xin);
}
}
for(int i = 0 ;i <= n ;i ++)
list2[i] = list[i];
return deep[t] != -1;
}
long long DFS_Flow(int s ,int t ,long long flow ,long long C)
{
if(s == t) return flow;
long long nowflow = 0;
for(int k = list2[s] ;k ;k = E[k].next)
{
list2[s] = k;
int to = E[k].to;
long long c = E[k].cost;
if(deep[to] != deep[s] + 1 || !c) continue;
long long tmp = DFS_Flow(to ,t ,minn(c ,flow - nowflow) ,C);
nowflow += tmp;
E[k].cost -= tmp;
E[k^1].cost += tmp;
if(nowflow == flow) break;
}
if(!nowflow) deep[s] = 0;
return nowflow;
}
long long DINIC(int s ,int t ,int n ,long long C)
{
long long Ans = 0;
while(BFS_Deep(s ,t ,n) && Ans < C)
{
Ans += DFS_Flow(s ,t ,INF ,C);
}
return Ans;
}
int main ()
{
int n ,m ,C ,cas = 1;
int a ,b ,c ,i ,j;
while(~scanf("%d %d %d" ,&n ,&m ,&C) && n + m + C)
{
memset(list ,0 ,sizeof(list)) ,tot = 1;
for(i = 1 ;i <= m ;i ++)
{
scanf("%d %d %d" ,&a ,&b ,&c);
add(a ,b ,(long long)c);
}
long long Ans = DINIC(1 ,n ,n ,C);
printf("Case %d: " ,cas ++);
if(Ans >= C)
{
printf("possible\n");
continue;
}
int nowid = 0;
for(i = 2 ;i <= tot ;i += 2)
{
mkE[i] = E[i] ,mkE[i+1] = E[i+1];
if(!E[i].cost)
{
edge[++nowid].a = E[i].from;
edge[nowid].b = E[i].to;
}
}
for(i = 1 ;i <= n ;i ++)
mklist[i] = list[i];
int Ans_Id = 0;
int mktot = tot;
for(i = 1 ;i <= nowid ;i ++)
{
add(edge[i].a ,edge[i].b ,C);
long long tmp = DINIC(1 ,n ,n ,C);
if(tmp + Ans >= C) Ans_Edge[++Ans_Id] = edge[i];
tot = mktot;
for(j = 2 ;j <= tot ;j ++) E[j] = mkE[j];
for(j = 1 ;j <= n ;j ++) list[j] = mklist[j];
}
if(!Ans_Id)
{
puts("not possible");
continue;
}
sort(Ans_Edge + 1 ,Ans_Edge + Ans_Id + 1 ,camp);
printf("possible option:");
for(i = 1 ;i <= Ans_Id ;i ++)
if(i != 1) printf(",(%d,%d)" ,Ans_Edge[i].a ,Ans_Edge[i].b);
else printf("(%d,%d)" ,Ans_Edge[i].a ,Ans_Edge[i].b);
puts("");
}
return 0;
}
UVA11248 网络扩容(枚举割边扩充)相关推荐
- bzoj1834: [ZJOI2010]network 网络扩容
努力看了很久样例一直过不了...然后各种输出中间过程啊巴拉巴拉弄了1h,没办法了...然后突然想到啊原来的边可以用啊为什么不用...于是A了...感人肺腑 #include<cstdio> ...
- [ZJOI2010]网络扩容[网络流24题]
[ZJOI2010]网络扩容[网络流24题] 题意: 给定一张有向图,每条边都有一个容量 c 和一个扩容费用 w.这里扩容费用是指将容量扩大 1 所需的费用.求: 在不扩容的情况下,1 到 n 的最大 ...
- P2604 ZJOI2010 网络扩容,费用流裸题
网络扩容 题目链接 https://www.luogu.org/problemnew/show/P2604 题解 对于每条边u→vu \rightarrow vu→v,我们将按照容量=C=C=C,费用 ...
- [BZOJ 1834] [ZJOI2010]network 网络扩容
1834: [ZJOI2010]network 网络扩容 Time Limit: 3 SecMemory Limit: 64 MB Description 给定一张有向图,每条边都有一个容量C和一个扩 ...
- [BZOJ1834][ZJOI2010]network 网络扩容 最大流+费用流
1834: [ZJOI2010]network 网络扩容 Time Limit: 3 Sec Memory Limit: 64 MB Submit: 3330 Solved: 1739 [Subm ...
- 御坂网络(枚举基准,二分图)
御坂网络(枚举基准,二分图) 现在有n个A点,m个B点(n,m<=200),和最大半径rmax.可以选定一个半径r<rmax,以r为半径,A点或B点为圆心,同时保证A点构造出的圆和B点构造 ...
- 网络工程师成长日记383-某银行某市中心支行市县网络扩容项目工程感想
网络工程师成长日记383-某银行某市中心支行市县网络扩容项目工程感想 这是我的第383篇原创文章,记录网络工程师行业的点点滴滴,结交IT行业有缘之人 某银行某市中心支行市县网络扩容项目工程感想 接到老 ...
- 【BZOJ】1834: [ZJOI2010]network 网络扩容(最大流+费用流)
http://www.lydsy.com/JudgeOnline/problem.php?id=1834 我又思考人生了T_T,nd的数组开小了,一直wa,调了一个小时才发现啊!!!!!我一直以为我的 ...
- [ZJOI2010]网络扩容
Description 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用. 求: 1.在不扩容的情况下,1到N的最大流: 2.将1到N的最大流增加K所需的最 ...
最新文章
- XML——XSLT的一个简单荔枝
- 论文浅尝 | 多内容实体和关系联合抽取的对抗训练
- 从1维到6维,一文读懂多维数据可视化策略
- adb interface找不到驱动程序_Windows 10现支持更多设备的驱动程序更新
- 80后屌丝站长自曝日赚6000(纯分享贴)
- 每天学点Python Cookbook(三)
- 基于JVM原理JMM模型和CPU缓存模型深入理解Java并发编程
- 让“王码五笔输入法”成为你的专用输入法!
- 每个设计师都在用的UI标注工具UI切图软件——PxCook像素大厨
- 天空卫士API数据安全解决方案
- 亲测win10安装mac虚拟机+网络配置完整过程
- Excel中Sheet(s)和Worksheet(s)的区别
- Windows Office 365 Pro Plus安装
- Unity技能系统架构
- 日活四千万的汤姆猫游戏家族,用AWS云服务打造“无感”用户体验
- python实现clahe对比度增强
- 海赢科技分享速卖通账号开店注册认证介绍及常见问题解答
- CSS------定位和动画
- 云原生时代崛起的编程语言Go常用标准库实战
- Python 繁简转换