ZOJ 2676 Network Wars(网络流+分数规划)
题意:求无向图割集中平均边权最小的集合。
论文《最小割模型在信息学竞赛中的应用》原题。
分数规划。每一条边取上的代价为1。
#include <bits/stdc++.h>
using namespace std; inline int read() {
int x = , f = ; char ch = getchar();
while (ch < '' || ch > '') { if (ch == '-') f = -; ch = getchar(); }
while (ch >= '' && ch <= '') { x = x * + ch - ; ch = getchar(); }
return x * f;
} const int N = 1e5 + ;
const double EPS = 1e-;
struct Edge { int v, next; double f; } edge[N];
int n, m, head[N], cnt, level[N], iter[N], x[N], y[N];
double z[N];
bool vis[N];
inline void add(int u, int v, double f) {
edge[cnt].v = v; edge[cnt].f = f; edge[cnt].next = head[u]; head[u] = cnt++;
edge[cnt].v = u; edge[cnt].f = f; edge[cnt].next = head[v]; head[v] = cnt++;
}
bool bfs() {
for (int i = ; i <= n; i++) iter[i] = head[i], level[i] = -, vis[i] = ;
queue<int> que;
que.push();
level[] = ;
while (!que.empty()) {
int u = que.front(); que.pop();
for (int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].v; double f = edge[i].f;
if (level[v] < && f) {
level[v] = level[u] + ;
que.push(v);
}
}
}
return level[n] != -;
} double dfs(int u, double f) {
if (u == n) return f;
double flow = ;
for (int i = iter[u]; ~i; i = edge[i].next) {
iter[u] = i;
int v = edge[i].v;
if (level[v] == level[u] + && edge[i].f) {
double w = dfs(v, min(f, edge[i].f));
//if (fabs(w) < EPS) continue;
f -= w;
flow += w;
edge[i].f -= w, edge[i^].f += w;
if (f <= ) break;
}
}
return flow;
} double dinic() {
double ans = ;
while (bfs()) ans += dfs(, 1e8);
return ans;
} bool check(double mid) {
memset(head, -, sizeof(head));
cnt = ;
double flow = ;
for (int i = ; i <= m; i++) {
if (z[i] > mid) add(x[i], y[i], z[i] - mid);
else flow += z[i] - mid;
}
flow += dinic();
return flow >= EPS;
} void get_ans(int u) {
vis[u] = ;
for (int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].v;
if (!vis[v] && edge[i].f > EPS) {
get_ans(v);
}
}
}
int ans[N], tol; int main() {
int kase = ;
while (~scanf("%d%d", &n, &m)) {
if (kase > ) puts("");
kase = ;
for (int i = ; i <= m; i++) {
x[i] = read();y[i] = read();
int a = read();z[i] = (double)a;
}
double l = , r = 1e7;
while (r - l > EPS) {
double mid = (l + r) / 2.0;
if (check(mid)) l = mid;
else r = mid;
}
check(r);
get_ans();
tol = ;
for (int i = ; i <= m; i++) {
if (vis[x[i]] + vis[y[i]] == || z[i] <= r) {
ans[++tol] = i;
}
}
printf("%d\n", tol);
for (int i = ; i <= tol; i++) {
if (i - ) putchar(' ');
printf("%d", ans[i]);
}
puts("");
}
return ;
}
ZOJ 2676 Network Wars(网络流+分数规划)的更多相关文章
- 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 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分数规划)
[题意]给出一个带权无向图,求割集,且割集的平均边权最小. [分析] 先尝试着用更一般的形式重新叙述本问题.设向量w表示边的权值,令向量c=(1, 1, 1, --, 1)表示选边的代价,于是原问题等 ...
- ZOJ 2676 Network Wars(最优比例最小割)
Network Wars Time Limit: 5 Seconds Memory Limit: 32768 KB Special Judge Network of Bytelan ...
- zoj2676 Network Wars(0-1分数规划,最大流模板)
Network Wars 07年胡伯涛的论文上的题:http://wenku.baidu.com/view/87ecda38376baf1ffc4fad25.html 代码: #include < ...
- ZJU 2676 Network Wars
Network Wars Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Original I ...
- 01分数规划zoj2676(最优比例,最小割集+二分)
ZOJ Problem Set - 2676 Network Wars Time Limit: 5 Seconds Memory Limit: 32768 KB S ...
- ZOJ - 2676 01分数规划 浮点ISAP
题意:求最小割集\(C\),使得\(\frac{\sum_{i∈C} cost_i}{|C|}\)最小 模型就是01分数规划\(\frac{\sum_{i=1}^{m}cost_i*x}{\sum_{ ...
随机推荐
- html中定义模板
定义一个html中不解析的模板,有如下三种方法1.使用<xmp>标签 <xmp id="test"> <div>测试</div> & ...
- php怎么遍历关联和索引数组
foreach $arr = ['a' => 1, 2, 3]; foreach($arr as $key => $value){ // } for $arr = [0, 1, 2, 3] ...
- day52——jquery引入与下载、标签查找、操作标签
day52 jquery引入 下载链接:jQuery官网 https://jquery.com/ 中文文档:jQuery AP中文文档 http://jquery.cuishifeng.cn/ < ...
- 【数据结构】12.java源码关于ConcurrentHashMap
目录 1.ConcurrentMap的内部结构 2.ConcurrentMap构造函数 3.元素新增策略4.元素删除5.元素修改和查找6.特殊操作7.扩容8.总结 1.ConcurrentMap内部结 ...
- Partition5:Partiton Scheme是否指定Next Used?
在SQL Server中,为Partition Scheme多次指定Next Used,不会出错,最后一次指定的FileGroup是Partition Scheme的Next Used,建议,在执行P ...
- Linux文件比对,批量复制
--背景 工作中突然有一天文件服务器空间满了,导致文件存不进去,立马换了另外一台服务器作为文件服务器,将服务器挂载上去,原来的服务器修复之后需要重新换回来,但是需要将临时使用的服务器内的文件迁移至原文 ...
- SpringBoot 传入JSON对象参数
1.请求参数格式必须是正确的JSON. 2.在入参中使用注解@RequestBody,用于接收JSON参数,使其自动转对象 3.关于lombok在此产生的一点小坑,@Builder对@RequestB ...
- js 获取 对象 属性名称(转载)
来源:https://www.cnblogs.com/YuyuanNo1/p/9257634.html dataObj = {name : su,age : 26,height : 18cm }; f ...
- nginx的rewrite跳转
Rewrite标记flag
- maven mvn跳过生成javadoc 打包报错
遇到javadoc用maven打包报错的问题,起初没发现javadoc,后发现并在pom看到了javadoc的配置. [ERROR] Failed to execute goal org.apache ...