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$周围四联通 ...
随机推荐
- java之接口相关知识
1.接口用interface来声明 //定义一个动物接口 public interface Animal{ public void eat(); public void travel(); } 2.接 ...
- latex目录标题常用宏包说明与示例
http://blog.sina.com.cn/s/blog_5e16f1770100gyxn.html
- Redis常用诊断命令
1.info 命令查看redis信息,可以指定要查看的section名 sections:Server,clients,memory,persistence,stats,replication,cpu ...
- dom 添加删除节点
//找到 div1 var div1 = document.getElementById("div1"); //创建 一个 p标签 var p = document.createE ...
- windows_Bat_Scripts查看系统IP-更改regedit-更新系统补丁
1.1 脚本名称 Update_patch.bat 1.2 脚本代码 @echo off :menu cls mode con cols=48 lines=27 & color 0 ...
- ZendFramework-2.4 源代码 - 路由(类图)
<?php return array( // console 模式 'console'=>array( 'router' => array( //.... ), ), // http ...
- Linux 服务器用户权限管理改造方案与实施项目
Linux 服务器用户权限管理改造方案与实施项目 在了解公司业务流程后,提出权限整改方案改进公司超级权限root泛滥的现状. 我首先撰写方案后,给boss看,取得boss的支持后,召集大家开会讨论. ...
- Redis之Hash类型操作
接口IRedisDaoHash: package com.net.test.redis.base.dao; import com.net.test.redis.base.entity.UserPsg; ...
- Ubuntu强制卸载VMware-player
有时候安装了vmwar-player,想再安装vmware-workstation,却提示一些古怪的消息(现在忘记具体是什么了).只能先卸载再安装 首先你可以尝试常规卸载: sudo vmware-i ...
- ACM二分搜索中的最大化最小值 总结
这类题目都有个相似的地方就是需要你去找一个临界点. 分析题目要你求什么,例如时间 那么mid就是时间 看求得这个跟什么相关 例如 poj 3258 求得是距离 这个距离跟两者之间的差相关 那题目要求你 ...