【IndiaHacks 2016 - Online Edition (Div 1 + Div 2) ErrichtoD】【二分答案 最大流】Delivery Bears x只熊运输同样的实物重量货物

Delivery Bears
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Niwel is a little golden bear. As everyone knows, bears live in forests, but Niwel got tired of seeing all the trees so he decided to move to the city.

In the city, Niwel took on a job managing bears to deliver goods. The city that he lives in can be represented as a directed graph with nnodes and m edges. Each edge has a weight capacity. A delivery consists of a bear carrying weights with their bear hands on a simple path from node 1 to node n. The total weight that travels across a particular edge must not exceed the weight capacity of that edge.

Niwel has exactly x bears. In the interest of fairness, no bear can rest, and the weight that each bear carries must be exactly the same. However, each bear may take different paths if they like.

Niwel would like to determine, what is the maximum amount of weight he can deliver (it's the sum of weights carried by bears). Find the maximum weight.

Input

The first line contains three integers n, m and x (2 ≤ n ≤ 50, 1 ≤ m ≤ 500, 1 ≤ x ≤ 100 000) — the number of nodes, the number of directed edges and the number of bears, respectively.

Each of the following m lines contains three integers ai, bi and ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 1 000 000). This represents a directed edge from node ai to bi with weight capacity ci. There are no self loops and no multiple edges from one city to the other city. More formally, for each i and j that i ≠ j it's guaranteed that ai ≠ aj or bi ≠ bj. It is also guaranteed that there is at least one path from node 1 to node n.

Output

Print one real value on a single line — the maximum amount of weight Niwel can deliver if he uses exactly x bears. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.

Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if .

Examples
input
4 4 3
1 2 2
2 4 1
1 3 1
3 4 2
output
1.5000000000
input
5 11 23
1 2 3
2 3 4
3 4 5
4 5 6
1 3 4
2 4 5
3 5 6
1 4 2
2 5 3
1 5 2
3 2 30
output
10.2222222222
Note

In the first sample, Niwel has three bears. Two bears can choose the path , while one bear can choose the path . Even though the bear that goes on the path  can carry one unit of weight, in the interest of fairness, he is restricted to carry 0.5 units of weight. Thus, the total weight is 1.5 units overall. Note that even though Niwel can deliver more weight with just 2 bears, he must use exactly 3 bears on this day.


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 55, M = 1010, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int ST, ED;
int first[N], id;
int w[M],  nxt[M], cap[M];
void ins(int x, int y, int cap_)
{
	w[++id] = y;
	cap[id] = cap_;
	nxt[id] = first[x];
	first[x] = id;

	w[++id] = x;
	cap[id] = 0;
	nxt[id] = first[y];
	first[y] = id;
}
int d[N];
bool bfs()
{
	MS(d, -1); d[ST] = 0;
	queue<int>q; q.push(ST);
	while (!q.empty())
	{
		int x = q.front(); q.pop();
		for (int z = first[x]; z; z = nxt[z])if (cap[z])
		{
			int y = w[z];
			if (d[y] == -1)
			{
				d[y] = d[x] + 1;
				if (y == ED)return 1;
				q.push(y);
			}
		}
	}
	return 0;
}
int dfs(int x, int aint)
{
	if (x == ED)return aint;
	int use = 0;
	for (int z = first[x]; z; z = nxt[z])if (cap[z])
	{
		int y = w[z];
		if (d[y] == d[x] + 1)
		{
			int tmp = dfs(y, min(cap[z], aint - use));
			cap[z] -= tmp;
			cap[z ^ 1] += tmp;
			use += tmp;
			if (use == aint)break;
		}
	}
	if (use == 0)d[x] = -1;
	return use;
}
int dinic()
{
	int tmp = 0;
	while (bfs())tmp += dfs(ST, ms63);
	return tmp;
}
int n, m, g;
struct A
{
	int x, y, z;
}a[505];
int main()
{
	while (~scanf("%d%d%d", &n,&m,&g))
	{
		ST = 1;ED = n;
		for (int i = 1; i <= m; ++i)scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].z);
		//每只熊最少运0,最多运1e6
		double l = 0;
		double r = 1e6;
		for (int tim = 1; tim <= 200;++tim)
		{
			double mid = (l + r) / 2;
			MS(first, 0); id = 1;
			for (int i = 1; i <= m; ++i)
			{
				int x = a[i].x;
				int y = a[i].y;
				//注意,这里转变的时候,一定要把前者转化为double,而不是把后者转化为int,否则会溢出。
				int z = min((double)g, (a[i].z / mid));
				ins(x, y, z);
			}
			if (dinic() >= g)l = mid;
			else r = mid;
		}
		printf("%.15f\n", l*g);
	}
	return 0;
}
/*
【题意】
n(50)个点,m(500)条有向边,我们有g(1e5)只运输熊。
每只熊都必须运输同样重要的货物(重量可以为double),从1点到n点。
问你每只熊最多可以运多少的货物

【类型】
最大流

【分析】
如何把我们转化成——每只熊都运同样重量的货物呢?
我们很快就可以发现,可以二分每只熊运送的货物w,
然后对于每条边,用a[i].z/w来算出这条边的容量。
然后跑最大流检测是否>=g

【时间复杂度&&优化】
O(100(二分次数)*n^2m)=125e6

*/


你可能感兴趣的:(codeforces,二分,题库-CF,图论-网络流之最大流)