题意:给定上一个网络,每个边有一个容量,问你能不能从 1 到 n,使得流量为 c,如果不能,那么是不是可以修改一条边,使得达到。

析:背景就是一个网络流,如果原图能跑出来,那么就不用了,就肯定能达到,如果不能,那么修改的边肯定是最小割里的边,那么就枚举这最小割里的边,这样可能会超时,所以就优化,其中一个优化就是每次不是从0开始跑,而是在第一次的基础再走,把两次的流量加起来如果超过c了,那么就能,再就是可以每次不用跑出最大流,如果流量超过c了,就可以结束了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 100 + 10;
const int maxm = 1e5 + 10;
const int mod = 50007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
}
int c;
struct Edge{
int from, to, cap, flow;
};
struct Dinic{
int n, m, s, t;
vector<Edge> edges;
vector<int> G[maxn];
int d[maxn];
bool vis[maxn];
int cur[maxn]; void init(int n){
this-> n = n;
for(int i = 0; i < n; ++i) G[i].cl;
edges.cl;
} void addEdge(int from, int to, int cap){
edges.pb((Edge){from, to, cap, 0});
edges.pb((Edge){to, from, 0, 0});
m = edges.sz;
G[from].pb(m - 2);
G[to].pb(m - 1);
} bool bfs(){
ms(vis, 0); vis[s] = 1;
d[s] = 0;
queue<int> q;
q.push(s); while(!q.empty()){
int u = q.front(); q.pop();
for(int i = 0; i < G[u].sz; ++i){
Edge &e = edges[G[u][i]];
if(!vis[e.to] && e.cap > e.flow){
vis[e.to] = 1;
d[e.to] = d[u] + 1;
q.push(e.to);
}
}
}
return vis[t];
} int dfs(int x, int a){
if(x == t || a == 0) return a;
int flow = 0, f;
for(int &i = cur[x]; i < G[x].sz; ++i){
Edge &e = edges[G[x][i]];
if(d[e.to] == d[x] + 1 && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0){
e.flow += f;
edges[G[x][i]^1].flow -= f;
flow += f;
a -= f;
if(a == 0) break;
if(flow >= c) return flow;
}
}
return flow;
} int maxflow(int s, int t){
this->s = s; this->t = t;
int flow = 0;
while(bfs()){ ms(cur, 0); flow += dfs(s, INF); }
return flow;
} vector<int> mincut; void getmincut(){
mincut.cl;
for(int i = 0; i < edges.sz; i += 2){
Edge &e = edges[i];
if(vis[e.from] && !vis[e.to] && e.cap > 0) mincut.push_back(i);
}
} void clearflow(){
for(int i = 0; i < edges.sz; ++i) edges[i].flow = 0;
} void solve(int s, int t){
int flow = maxflow(s, t);
if(flow >= c){ puts("possible"); return ; }
c -= flow;
getmincut();
for(int i = 0; i < edges.sz; ++i)
edges[i].cap -= edges[i].flow;
vector<P> ans;
for(int i = 0; i < mincut.sz; ++i){
Edge &e = edges[mincut[i]];
e.cap = c;
clearflow();
if(maxflow(s, t) >= c) ans.push_back(P(e.from, e.to));
e.cap = 0;
}
if(ans.empty()) printf("not possible\n");
else{
sort(ans.begin(), ans.end());
printf("possible option:(%d,%d)", ans[0].fi, ans[0].se);
for(int i = 1; i < ans.sz; ++i)
printf(",(%d,%d)", ans[i].fi, ans[i].se);
printf("\n");
}
}
}; Dinic dinic; int main(){
int kase = 0;
while(scanf("%d %d %d", &n, &m, &c) == 3 && n){
dinic.init(n + 5);
for(int i = 0; i < m; ++i){
int u, v, c;
scanf("%d %d %d", &u, &v, &c);
dinic.addEdge(u, v, c);
}
printf("Case %d: ", ++kase);
dinic.solve(1, n);
}
return 0;
}

  

UVa 11248 Frequency Hopping (网络流)的更多相关文章

  1. UVA 11248 - Frequency Hopping(网络流量)

    UVA 11248 - Frequency Hopping 题目链接 题意:给定一个网络,如今须要从1到N运输流量C,问是否可能,假设可能输出可能,假设不可能,再问能否通过扩大一条边的容量使得可能,假 ...

  2. uva 11248 Frequency Hopping (最大流)

    uva 11248 Frequency Hopping 题目大意:给定一个有向网络,每条边均有一个容量. 问是否存在一个从点1到点N.流量为C的流.假设不存在,能否够恰好改动一条弧的容量,使得存在这种 ...

  3. UVA 11248 Frequency Hopping

    Frequency Hopping Time Limit: 10000ms Memory Limit: 131072KB This problem will be judged on UVA. Ori ...

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

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

  5. UVA 10480 Sabotage (网络流,最大流,最小割)

    UVA 10480 Sabotage (网络流,最大流,最小割) Description The regime of a small but wealthy dictatorship has been ...

  6. UVa 11082 Matrix Decompressing - 网络流

    开始眨眼一看怎么也不像是网络流的一道题,再怎么看也觉得像是搜索.不过虽然这道题数据范围很小,但也不至于搜索也是可以随随便便就可以过的.(不过这道题应该是special judge,因为一题可以多解而且 ...

  7. UVA 11082 矩阵解压(网络流建模)

    矩阵解压 紫书P374 建模真的是挺难的,如果直接给我这题,我是想不到用网络流的,所以还应多做网路流建模,学会如何转化成网络流 还有,现在用的EK算法是比较慢的,还应去看看Dnic和ISAP,并且理解 ...

  8. uva 10330 - Power Transmission(网络流)

    uva 10330 - Power Transmission 题目大意:最大流问题. 解题思路:増广路算法. #include <stdio.h> #include <string. ...

  9. Uva 11248 网络扩容

    题目链接:https://vjudge.net/contest/144904#problem/A 题意:给定一个有向网络,每条边均有一个容量.问是否存在一个从点1到点N,流量为C的流.如果不存在,是否 ...

随机推荐

  1. 有名管道FIFO

    管道和FIFO的特征之一是它们的数据是一个字节流.这是UNIX的原生I/O模型.进程往其中写入的是字节流,系统不对它作解释. FIFO不存数据,只是通过它找到内核文件. 一.建立有名管道 1.命令mk ...

  2. Hbase—学习笔记(一)

    此文的目的: 1.重点理解Hbase的整体工作机制 2.熟悉编程api,能够用来写程序 1.  什么是HBASE 1.1.   概念特性 HBASE是一个数据库----可以提供数据的实时随机读写 HB ...

  3. Microsoft SQL Server, 错误:4064的解决方法 (转载)

    SQL SERVER – Fix : Error: 4064 – Cannot open user default database. Login failed. Login failed for u ...

  4. Kuberentes-入门

    一.kubernetes架构介绍和集群规划 点击链接查看: 系统环境初始化:https://www.cnblogs.com/hwlong/p/9105742.html 二.CA证书创建和分发 点击链接 ...

  5. 23-新建maven 项目

    1. 新建:Maven Project; 2. 配置项 bulid path, 添加tomcat: 3. 新建一个sources文件夹: srt/test/resourses 4.配置她的默认输出路径 ...

  6. php 函数中静态变量的问题

    <?php function msg() { static $a = 0; echo $a++, '<br />'; } msg(); msg(); msg(); 上述代码,分别输出 ...

  7. PHP下ajax跨域的解决方案之window.name

    原理核心:window对象的name属性是一个很特别的属性,当该window的location变化,然后重新加载,它的name属性可以依然保持不变. 依此原理,我们可以在页面A中用iframe加载其他 ...

  8. mysql优化概述3

    1.前缀索引 建立索引关键字一种方案. 通常会使用字段的整体作为索引关键字. 有时,使用字段前部分数据,也可以去识别某些记录. 语法: index `索引名` (`字段`(N)); 使用字段前N个字符 ...

  9. datagridview 如何禁止行被选中

    如题,如何规定特定的行,光标不能定位,也不能被选中,就好想Button中的Enable属性那样,变灰,而且点击也没有反应那种,这样的效果,如何实现. datagridview [解决办法]dataGr ...

  10. Golang之hello,beego

    学习谢大神的beego记录 过程: 目录结构: 编译命令: go build -o myBeego.exe go_dev/day13/beego_example/main执行myBeego.exe即可 ...