ZJU 2676 Network Wars
Network Wars
This problem will be judged on ZJU. Original ID: 2676
64-bit integer IO format: %lld Java class name: Main
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 exceed 107.
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
6 8
1 2 3
1 3 3
2 4 2
2 5 2
3 4 2
3 5 2
5 6 3
4 6 3
4 5
1 2 2
1 3 2
2 3 1
2 4 2
3 4 2 Output
4
3 4 5 6
3
1 2 3
Source
解题:最大流-分数规划?不懂。。。好厉害的样子啊
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
const double exps = 1e-;
struct arc{
int to,next,id;
double flow;
arc(int x = ,double y = ,int z = ,int nxt = -){
to = x;
flow = y;
id = z;
next = nxt;
}
};
arc e[];
int head[maxn],d[maxn],cur[maxn],xx[maxn<<],yy[maxn<<];
int tot,S,T,n,m;
double ww[maxn<<];
bool used[maxn<<],vis[maxn<<];
void add(int u,int v,double flow,int id){
e[tot] = arc(v,flow,id,head[u]);
head[u] = tot++;
e[tot] = arc(u,,id,head[v]);
head[v] = tot++;
}
bool bfs(){
queue<int>q;
memset(d,-,sizeof(d));
d[S] = ;
q.push(S);
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].flow > exps && d[e[i].to] == -){
d[e[i].to] = d[u] + ;
q.push(e[i].to);
}
}
}
return d[T] > -;
}
double dfs(int u,double low){
if(u == T) return low;
double tmp = ,a;
for(int &i = cur[u]; ~i; i = e[i].next){
if(e[i].flow > exps && d[e[i].to] == d[u] + && (a = dfs(e[i].to,min(low,e[i].flow))) > exps){
e[i].flow -= a;
e[i^].flow += a;
tmp += a;
low -= a;
if(low < exps) break;
}
}
if(tmp < exps) d[u] = -;
return tmp;
}
double dinic(){
double tmp = ;
while(bfs()){
memcpy(cur,head,sizeof(head));
tmp += dfs(S,INF);
}
return tmp;
}
double init(double delta){
double tmp = tot = ;
memset(head,-,sizeof(head));
memset(used,false,sizeof(used));
for(int i = ; i <= m; ++i){
if(ww[i] - delta <= exps){
used[i] = true;
tmp += ww[i] - delta;
}else{
add(xx[i],yy[i],ww[i]-delta,i);
add(yy[i],xx[i],ww[i]-delta,i);
}
}
return tmp;
}
void dfs2(int u){
vis[u] = true;
for(int i = head[u]; ~i; i = e[i].next)
if(e[i].flow > exps && !vis[e[i].to]) dfs2(e[i].to);
}
int main(){
bool flag = false;
while(~scanf("%d %d",&n,&m)){
S = ;
T = n;
if(flag) puts("");
flag = true;
for(int i = ; i <= m; ++i) scanf("%d %d %lf",xx+i,yy+i,ww+i);
double mid,low = ,high = INF,ans = ;
while(fabs(high - low) > 1e-){
mid = (low + high)/2.0;
ans = init(mid);
ans += dinic();
if(ans > ) low = mid;
else high = mid;
}
memset(vis,false,sizeof(vis));
dfs2(S);
vector<int>res;
for(int i = ; i < n; ++i)
for(int j = head[i]; ~j; j = e[j].next)
if(vis[i] != vis[e[j].to]) used[e[j].id] = true;
for(int i = ; i <= m; ++i)
if(used[i]) res.push_back(i);
printf("%d\n",res.size());
for(int i = ; i < res.size(); ++i)
printf("%d%c",res[i],i == res.size()-?'\n':' ');
}
return ;
}
ZJU 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(最优比例最小割)
Network Wars Time Limit: 5 Seconds Memory Limit: 32768 KB Special Judge Network of Bytelan ...
- 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 0-1分数规划+最小割
题目详解出自 论文 Amber-最小割模型在信息学竞赛中的应用 题目大意: 给出一个带权无向图 G = (V,E), 每条边 e属于E都有一个权值We,求一个割边集C,使得该割边集的平均边权最小,即最 ...
- ZOJ 2676 Network Wars(网络流+分数规划)
传送门 题意:求无向图割集中平均边权最小的集合. 论文<最小割模型在信息学竞赛中的应用>原题. 分数规划.每一条边取上的代价为1. #include <bits/stdc++.h&g ...
- zoj2676 Network Wars(0-1分数规划,最大流模板)
Network Wars 07年胡伯涛的论文上的题:http://wenku.baidu.com/view/87ecda38376baf1ffc4fad25.html 代码: #include < ...
- Network Wars
zoj2676:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1676 题意:给出一个带权无向图 ,每条边e有一个权 .求将点 ...
- zju2676 Network Wars 分数规划+网络流
题意:给定无向图,每条边有权值,求该图的一个割集,是的该割集的平均边权最小 Amber的<最小割模型在信息学竞赛中的应用>中讲的很清楚了. 二分答案k,对每条边进行重新赋值为原边权-k,求 ...
随机推荐
- vue+Ueditor集成 [前后端分离项目][图片、文件上传][富文本编辑]
后端DEMO:https://github.com/coderliguoqing/UeditorSpringboot 前端DEMO:https://github.com/coderliguoqing/ ...
- 关于参数net_buffer_length How MySQL Uses Memory
http://dev.mysql.com/doc/refman/5.6/en/memory-use.html The following list indicates some of the ways ...
- BA-传感器
01.室内温度传感器 壁装,西门子,QAA2061D 1.默认范围:温度0-50℃,湿度0-100% 2.供电方式:24vac 3.穿线方式:4芯屏蔽线 02.风管温度传感器 西门子,QAM2120. ...
- WebApi传参总动员(一)
目前自己的工作和WebApi相关,免不了传入.接收参数.以前的老办法是从请求流中获取json,再反序列化,这中间有2个不能控制的地方,一个是流,一个是反序列化,都需要try,总感觉非常的不爽.因此对W ...
- 在IntelliJ IDEA中创建Maven多模块项目
在IntelliJ IDEA中创建Maven多模块项目 1,创建多模块项目选择File>New>Project 出现New Project窗口左侧导航选择Maven,勾选右侧的Create ...
- HDOJ 3339 In Action
最短路+01背包 In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 2054 A==B? 大数
Problem Description Give you two numbers A and B, if A is equal to B, you should print "YES&quo ...
- ExtJs--16--Ext.override()方法专门用来重写对象的方法
Ext.onReady(function(){ /** * Ext.override()方法专门用来重写对象的方法 */ //定义个类 Ext.define("U",{ //该类的 ...
- 【刷题笔记】LeetCode 606. Construct String from Binary Tree
题意 给一棵二叉树,把它转化为字符串返回.转化字符串的要求如下: 1. null 直接转化为 () ;(这个要求其实有点误导人~) 2. 子节点用 () 包裹起来:(这是我自己根据例子添加的要求) ...
- MacOS系统下简单安装以及配置MongoDB数据库(一)
最近写了一个用node来操作MongoDB完成增.删.改.查.排序.分页功能的示例,并且已经放在了服务器上地址:http://39.105.32.180:3333. 项目一共四部分: 1.MacOS下 ...