ZOJ 2676 Network Wars(最优比例最小割)
Network Wars
Time Limit: 5 Seconds Memory Limit: 32768 KB Special Judge
Network of Byteland consists of n servers, connected by m optical cables. Each cable connects two servers and can transmit data in both directions. Two servers of the network are especially important --- they are connected to global world network and president palace network respectively.
The server connected to the president palace network has number 1, and the server connected to the global world network has number n.
Recently the company Max Traffic has decided to take control over some cables so that it could see what data is transmitted by the president palace users. Of course they want to control such set of cables, that it is impossible to download any data from the global network to the president palace without transmitting it over at least one of the cables from the set.
To put its plans into practice the company needs to buy corresponding cables from their current owners. Each cable has some cost. Since the company's main business is not spying, but providing internet connection to home users, its management wants to make the operation a good investment. So it wants to buy such a set of cables, that cables mean cost} is minimal possible.
That is, if the company buys k cables of the total cost c, it wants to minimize the value of c/k.
Input
There are several test cases in the input. The first line of each case contains n and m (2 <= n <= 100 , 1 <= m <= 400 ). Next m lines describe cables~--- each cable is described with three integer numbers: servers it connects and the cost of the cable. Cost of each cable is positive and does not exceed107.
Any two servers are connected by at most one cable. No cable connects a server to itself. The network is guaranteed to be connected, it is possible to transmit data from any server to any other one.
There is an empty line between each cases.
Output
First output k --- the number of cables to buy. After that output the cables to buy themselves. Cables are numbered starting from one in order they are given in the input file. There should an empty line between each cases.
Example
Input | Output |
6 8 |
4 |
4 5 |
3 |
题目链接:ZOJ 2676
此题叫我们求$\Sigma w_{ei} \over |E|$的最小值,其中所有的边均在S-T的割中,可以发现当${\Sigma w_{ei} \over |E|}<r$时,存在$r'={\Sigma w_{ei} \over |E|}$作为更优的r,那我们写成$\Sigma w_{ei} - r*|E|<0$,存在一个左边的结果使得等式成立,即找到左边式子的最小值小于0即可,观察左边的式子,可以化简成$\Sigma (w_{ei}-r)<0$,然后边集e是一个割,又要求这个割集的最小值,那显然就是求s-t的最小割即可,先用二分求出最佳的比例,然后在最后剩下的那个残余网络中找出割集。
代码:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 110;
const int M = 410;
const double eps = 1e-6;
struct edge
{
int to, nxt;
double cap;
edge() {}
edge(int _to, int _nxt, double _cap): to(_to), nxt(_nxt), cap(_cap) {}
};
struct Node
{
int u, v;
double cap;
};
Node e[M];
edge E[M << 1];
int head[N], tot;
int d[N];
int use[M]; void init()
{
CLR(head, -1);
tot = 0;
CLR(use, 0);
}
inline void add(int s, int t, double cap)
{
E[tot] = edge(t, head[s], cap);
head[s] = tot++;
E[tot] = edge(s, head[t], cap);
head[t] = tot++;
}
int bfs(int s, int t)
{
CLR(d, -1);
d[s] = 0;
queue<int>Q;
Q.push(s);
while (!Q.empty())
{
int u = Q.front();
Q.pop();
for (int i = head[u]; ~i; i = E[i].nxt)
{
int v = E[i].to;
if (d[v] == -1 && E[i].cap > 0)
{
d[v] = d[u] + 1;
if (v == t)
return 1;
Q.push(v);
}
}
}
return ~d[t];
}
double dfs(int s, int t, double f)
{
if (s == t || !f)
return f;
double ret = 0;
for (int i = head[s]; ~i; i = E[i].nxt)
{
int v = E[i].to;
if (d[v] == d[s] + 1 && E[i].cap > 0)
{
double df = dfs(v, t, min(f, E[i].cap));
if (df > 0)
{
E[i].cap -= df;
E[i ^ 1].cap += df;
f -= df;
ret += df;
if (!f)
break;
}
}
}
if (!ret)
d[s] = -1;
return ret;
}
double dinic(int s, int t)
{
double ans = 0;
while (bfs(s, t))
ans += dfs(s, t, INF);
return ans;
}
double Mincut(int n, int m, double r)
{
int i;
init();
double ret = 0;
for (i = 1; i <= m; ++i)
{
if (e[i].cap < r)
{
ret += e[i].cap - r;
use[i] = 1;
}
else
add(e[i].u, e[i].v, e[i].cap - r);
}
return ret + dinic(1, n);
}
int main(void)
{
int n, m, i;
while (~scanf("%d%d", &n, &m))
{
for (i = 1; i <= m; ++i)
scanf("%d%d%lf", &e[i].u, &e[i].v, &e[i].cap);
double Rat = 0, L = 0, R = 400.0 / 3 * 1e7;
while (fabs(R - L) >= eps)
{
double mid = (L + R) / 2.0;
if (Mincut(n, m, mid) < 0)
{
R = mid;
Rat = mid;
}
else
L = mid;
}
vector<int>ans;
for (i = 1; i <= m; ++i)
{
if ((d[e[i].u]!=-1)^(d[e[i].v]!=-1))
use[i] = 1;
if (use[i])
ans.push_back(i);
}
int sz = ans.size();
printf("%d\n", sz);
for (i = 0; i < sz; ++i)
printf("%d%c", ans[i], " \n"[i == sz - 1]);
}
return 0;
}
ZOJ 2676 Network Wars(最优比例最小割)的更多相关文章
- zoj 2676 Network Wars 0-1分数规划+最小割
题目详解出自 论文 Amber-最小割模型在信息学竞赛中的应用 题目大意: 给出一个带权无向图 G = (V,E), 每条边 e属于E都有一个权值We,求一个割边集C,使得该割边集的平均边权最小,即最 ...
- HDU 2676 Network Wars 01分数规划,最小割 难度:4
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1676 对顶点i,j,起点s=1,终点t=n,可以认为题意要求一组01矩阵use ...
- ZOJ 2676 Network Wars[01分数规划]
ZOJ Problem Set - 2676 Network Wars Time Limit: 5 Seconds Memory Limit: 32768 KB Special J ...
- ZOJ 2676 Network Wars ★(最小割算法介绍 && 01分数规划)
[题意]给出一个带权无向图,求割集,且割集的平均边权最小. [分析] 先尝试着用更一般的形式重新叙述本问题.设向量w表示边的权值,令向量c=(1, 1, 1, --, 1)表示选边的代价,于是原问题等 ...
- ZOJ 2676 Network Wars(网络流+分数规划)
传送门 题意:求无向图割集中平均边权最小的集合. 论文<最小割模型在信息学竞赛中的应用>原题. 分数规划.每一条边取上的代价为1. #include <bits/stdc++.h&g ...
- ZJU 2676 Network Wars
Network Wars Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Original I ...
- BZOJ 3774: 最优选择( 最小割 )
最小割...二分染色然后把颜色不同的点的源汇反过来..然后就可以做了. 某个点(x,y): S->Id(x,y)(回报), Id(x,y)->T(代价), Id(i,j)&& ...
- 【BZOJ3774】最优选择 最小割
[BZOJ3774]最优选择 Description 小N手上有一个N*M的方格图,控制某一个点要付出Aij的代价,然后某个点如果被控制了,或者他周围的所有点(上下左右)都被控制了,那么他就算是被选择 ...
- BZOJ 3774 最优选择 (最小割+二分图)
题面传送门 题目大意:给你一个网格图,每个格子都有$a_{ij}$的代价和$b_{ij}$的回报,对于格子$ij$,想获得$b_{ij}$的回报,要么付出$a_{ij}$的代价,要么$ij$周围四联通 ...
随机推荐
- kafka 开机启动脚本
/etc/init.d$ vi kafka-start-up.sh #!/bin/bash #export KAFKA_HOME=$PATH export KAFKA_HOME=/opt/Kafka/ ...
- 访问虚拟机中web服务的
经常发现假如我们想弄一点小玩意或跑一些小demo,总是要不断的在自己的工作本本上搭建不同的运行环境,久而久之,本本上充斥着各种软件,速度下降了,同时管理也非常的不方便.于是想到用虚拟机来搭建运行环境, ...
- 推荐一个WebIDE在线编程语言编译器C9.io
有时借用别人电脑或者不想在电脑上安装各种乱七八糟的IDE,就可以考虑 Web IDE.随着Web技术发展,很多语言的编译工作都可以利用Web 浏览器来完成. 1. 推荐国外的 C9.io 个人可以免费 ...
- k8s的configMap基本概念及案例
pod中两种特殊类型的存储卷:secret,configMap pod.spec.volumes.secret pod.spec.volumes.configMap多数情况下,这两个存储卷不是给p ...
- IDEA整合Mybatis+Struts2+Spring (二)--整合框架
二.搭建目录结构 我这里列出的是搭建完了之后所有的目录和文件,诸位先把目录文件建起来,然后我在给出文件内容 这里的目录建好之后还需要设置一下,让idea识别目录作用,选择File-Project St ...
- python之质数
质数(prime number)又称素数,有无限个 质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数. 示例: num=[]; i=2 for i in range(2,100): j= ...
- 关于debug
2019-04-05 11:18:15 1. debug 需巧用两个工具 1.1 用‘#’把感觉会出错的代码段注释掉 多行注释有两种快捷操作: 在需要注释的多行代码块前后加一组三引号''' 选中代 ...
- python3 题目 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
方法一:for循环遍历 counter=0 for i in range(1,5): for j in range(1,5): for k in range(1,5): if i !=j and j ...
- 初学js之多组图片切换实例
需求是以上效果展示.话不多说,直接代码显示,不涉及代码优化.已实现功能为目的. 先看html部分: <body> <div class="dream" id=&q ...
- B1013 数素数(20分)
B1013 数素数(20分) 令 \(P_i\)表示第 i 个素数.现任给两个正整数 \(M≤N≤10^4\),请输出 \(P_M\)到 \(P_N\)的所有素数. 输入格式: 输入在一行中给出 M ...