Codeforces 653D:Delivery Bears 二分+网络流

本文介绍了一道编程题目,涉及到利用二分查找和网络流算法解决熊在加权有向图中公平分配重量的问题。题目要求确定在使用指定数量的熊的情况下,最多可以运输多少重量的商品,保证每条边的负载不超过其容量。通过二分搜索确定每个熊的最大承重,并应用网络流算法计算最大流量,从而得出答案。

D. 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 nm and x (2 ≤ n ≤ 501 ≤ m ≤ 5001 ≤ 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 aibi and ci (1 ≤ ai, bi ≤ nai ≠ bi1 ≤ 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.


题意是给出了从1到n的一个加权有向图,有x个熊从1开始搬东西,每个熊搬的东西重量要相同。然后每一个熊可以走不同的路径,要求就是每一条边上的熊搬的东西的重量要小于等于该边的权值,问每一个熊最多能搬多重的东西。

二分每一个熊最多能搬的重量,然后对边的权值进行更改,看该边最多能通过多少个熊。跑一遍网络流,看最大流能否是x。

代码:

#pragma warning(disable:4996)
#include <iostream>
#include <functional>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
using namespace std;
typedef long long ll;

#define INF 0x33ffffff

const ll mod = 1000000007;
const int maxn = 505;
const double PI = acos(-1.0);

int n, m, x;
int u[maxn], v[maxn], cap[maxn];
struct ed
{
	int to;
	int cap;
	int flow;
	int next;
}edge[4 * maxn];

int edgen;
int stac[maxn], head[maxn], dis[maxn], vis[maxn], pre[maxn];

void addedge(int u, int v, int cap)
{
	edge[edgen].to = v;
	edge[edgen].cap = cap;
	edge[edgen].flow = 0;
	edge[edgen].next = head[u];
	head[u] = edgen++;

	edge[edgen].to = u;
	edge[edgen].cap = 0;
	edge[edgen].flow = 0;
	edge[edgen].next = head[v];
	head[v] = edgen++;
}
void init()
{
	edgen = 0;
	memset(head, -1, sizeof(head));
	memset(edge, -1, sizeof(edge));
}

bool bfs(int S, int T)
{
	int i, j, k, h, t;
	memset(dis, -1, sizeof(dis));
	dis[S] = 0;
	stac[0] = S;
	
	t = 1;
	for (h = 0; h < t; h++)
	{
		for (k = head[stac[h]]; k != -1; k = edge[k].next)
		{
			j = edge[k].to;
			if (dis[j] == -1 && edge[k].cap>edge[k].flow)
			{
				dis[j] = dis[stac[h]] + 1;
				stac[t++] = j;
			}
		}
	}
	if (dis[T] == -1)
	{
		return false;
	}
	else
	{
		return true;
	}
}

int dfs(int S, int T,int low)
{
	if (S == T)return low;
	int i, j, k;
	int res = 0, tmp;
	for (i = head[S]; i != -1 && res < low; i = edge[i].next)
	{
		k = edge[i].to;
		if (dis[k] == dis[S] + 1 && edge[i].cap>edge[i].flow)
		{
			if (tmp = dfs(k, T, min(low - res, edge[i].cap - edge[i].flow)))
			{
				res += tmp;
				edge[i].flow += tmp;
				edge[i^1].flow -= tmp;
			}
		}
	}
	if (res == 0)
	{
		dis[S] = -1;
	}
	return res;
}

int dinic(int s, int t)
{
	int maxflow = 0, tmp;
	while (bfs(s, t))
	{
		while (tmp = dfs(s, t, INF))
			maxflow += tmp;
	}
	return maxflow;
}

int check(double w)
{
	init();
	int i, j, k;
	for (i = 1; i <= m; i++)
	{
		addedge(u[i], v[i], (int)min(1.0*cap[i] / w, 1.0*x));
	}
	addedge(0, 1, x);
	addedge(n, n + 1, x);

	if (dinic(0, n + 1) == x)
	{
		return true;
	}
	else
	{
		return false;
	}
}

void solve()
{
	int i, j, k;
	scanf("%d%d%d", &n, &m, &x);
	for (i = 1; i <= m; i++)
	{
		scanf("%d%d%d", &u[i], &v[i], &cap[i]);
	}
	double mid, le = 0, ri = 1e9;
	for (k = 0; k < 100; k++)
	{
		mid = (le + ri) / 2.0;
		if (check(mid))
		{
			le = mid;
		}
		else
		{
			ri = mid;
		}
	}
	printf("%.7f", 1.0*x*le);
}

int main()
{
#ifndef ONLINE_JUDGE  
	freopen("i.txt", "r", stdin);
	freopen("o.txt", "w", stdout);
#endif

	solve();
	
	//system("pause");
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值