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
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
3 4 5 6
4 5
1 2 2
1 3 2
2 3 1
2 4 2
3 4 2
3
1 2 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;
}
上一篇:【算法】01分数规划 --- HNOI2009最小圈 & APIO2017商旅 & SDOI2017新生舞会


下一篇:基于visual Studio2013解决面试题之0307最后谁剩下