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
二分每个人应该运输的重量w, 再把每条路的容量除以w取整就是这条路可以走过的人数。再用再求出最大流判断是否大于等于总人数

#include <bits/stdc++.h>
#define MOD 1000000007
#define maxn 505
#define INF 1e18
using namespace std;
typedef long long ll;

struct Node{
	Node(){
	}
	Node(int a, int b, int c){
		from = a;
		to = b;
		s = c;
		cap = 0;
		flow = 0;
	}
	int from, to, cap, flow, s;
};
vector<Node> e;
vector<int> v[maxn];
int p[maxn], a[maxn];
queue<int> q;
int n, m, x;
int max_flow(){
	
	int flow = 0;
	while(1){
		while(!q.empty())
		 q.pop();
		memset(a, 0, sizeof(a));
		a[1] = 1e9;
		p[1] = -1;
		q.push(1);
		while(!q.empty()){
			int h = q.front();
			q.pop();
			for(int i = 0; i < v[h].size(); i++){
				Node node = e[v[h][i]];
				if(a[node.to] == 0 && node.cap > node.flow){
					p[node.to] = v[h][i];
					a[node.to] = min(a[node.from], node.cap - node.flow);
					q.push(node.to);
				}
			}
			if(a[n])
			  break;
		}
		if(a[n] == 0)
		 break;
		flow += a[n];
		for(int i = p[n]; i != -1; i = p[e[i].from]){
			e[i].flow += a[n];
			e[i^1].flow -= a[n];
		}
	}
	return flow;
}
int main(){
//	freopen("in.txt", "r", stdin);
	int a, b, c;
	scanf("%d%d%d", &n, &m, &x);
	for(int i = 1; i <= m; i++){
		scanf("%d%d%d", &a, &b, &c);
		e.push_back(Node(a, b, c));
		e.push_back(Node(b, a, c));
		int h = e.size();
		v[a].push_back(h-2);
		v[b].push_back(h-1);
	}
	 double l = 0, r = 1000005;
	int tt = 100;
	while(tt--){
		 double mid = (l + r) / 2;
		for(int i = 0; i < e.size(); i++){
			if(i % 2 == 0){
				double ss = e[i].s / mid > x ? x : e[i].s / mid;
				e[i].cap = ss;
		    }
		    e[i].flow = 0;
		}
		if(max_flow() >= x)
		 l = mid;
		else
		 r = mid;
	}
	printf("%.10lf\n", l * x);
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值