Network Wars
Time Limit: 5 Seconds
Memory Limit: 32768 KB
Special Judge
Network of Byteland consists of n servers, connected by m
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
To put its plans into practice the company needs to buy corresponding cables from their current owners. Each cable has somecost. Since the company's main business is not spying, butproviding internet connection to home users, its management wants to make the operation a good investment. So it wants to buy sucha 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 describecables~--- each cable is described with three integer numbers:servers it connects and the cost of the cable. Cost of each cableis positive and does not exceed 107
.
Any two servers are connected by at most one cable. Nocable connects a server to itself.The network is guaranteed to be connected, it is possibleto 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 thatoutput the cables to buy themselves. Cables are numbered startingfrom one in order they are given in the input file.There should an empty line between each cases.
Example
Input | Output |
6 81 2 31 3 32 4 22 5 23 4 23 5 25 6 34 6 3 | 43 4 5 6 |
4 51 2 21 3 22 3 12 4 23 4 2 | 31 2 3 |
Source:
Andrew Stankevich's Contest #8
大致题意:给你一个网络,让你求一个最小割[S,T],使得这个割中Σcapi/|[S,T]|最小。
Σcapi/|[S,T]|,那么可以有0= Σcapi-ans*|[S,T]|,二分这样的一个ans,使得f(x)=min(Σcapi-ans*|[S,T]|)为0即可。问题的关键在于怎么求这个f(x)。
我们不妨把cap 看作一个向量,表示从1 到m 每一条边的容量,然后设置一个01 向量x ,它的第i 维对应表示第i 条边的取与不取,c=(1,1,...,1)如此一来,f(x)可以表示为f(x)=min((cap-ans*c)*x),拆开成每一条边的形式有f(x)=min(Σ(capi*xi-ans))。这意味着,对于第i条边,如果我选取它,那么它的代价就是capi-ans。现在要求最小的f(x),同时要求答案要至少是一个割,那就相当于把原图中的边权改为capi-ans,求一个最小割即可。
capi-ans小于0的情况,由于网络流中不能有负的权值,所以不能建边。但根据取最小的f(x)定义,要把capi-ans加入到f(x)中。之后二分这个x,找到一个使得f(x)等于0的x即可。具体见代码: