uva11865 Stream My Contest (朱刘算法+二分)

来源:互联网 发布:易语言攻击集群源码 编辑:程序博客网 时间:2024/06/05 16:40

题意有n台机器(编号为0—n-1,其中机器0是服务器),m条网线(每条网线相当于一条有向边,有相应的带宽和代价),c的钱,给定c,求用m条网线搭建一个网路,使得每台机器都可以从服务器接收到信息,且带宽的最小值达到最大

思路:朱刘算法模板+二分宽带


#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;typedef long long ll;const int inf = 0x3f3f3f3f;const int maxn = 105;struct DMST {struct Edge {int u, v, d, b;Edge(int u = 0, int v = 0, int d = 0, int b = 0): u(u), v(v), d(d), b(b) {}bool operator < (const Edge& u) const { return d < u.d; }};int in[maxn], pre[maxn], id[maxn], vis[maxn];vector<Edge> edges, saves;void init () { saves.clear(); }void addEdge(int u, int v, int d, int b) { saves.push_back(Edge(u, v, d, b)); }bool directedMST(int root, int n, int cost, int bandwith) {ll ans = 0;edges = saves;while (true) {// 删除自环+找最小入边//memset(in, inf, sizeof(in) * n);memset(in, inf, sizeof(in));for (int i = 0; i < edges.size(); i++) {int u = edges[i].u, v = edges[i].v;if (edges[i].d < in[v] && u != v && edges[i].b >= bandwith) {pre[v] = u;in[v] = edges[i].d;}}// 判断是否可以到达所有节点for (int i = 0; i < n; i++) {if (i == root) continue;if (in[i] == inf) return false;}int cntnode = 0;//memset(id, -1, sizeof(int) * n);//memset(vis, -1, sizeof(int) * n);memset(id, -1, sizeof(id));memset(vis, -1, sizeof(vis));in[root] = 0;for (int i = 0; i < n; i++) {ans += in[i];int v = i;while (vis[v] != i && id[v] == -1 && v != root) {vis[v] = i;v = pre[v];}if (v != root && id[v] == -1) {for (int u = pre[v]; u != v; u = pre[u])id[u] = cntnode;id[v] = cntnode++;}}// 没有找到环, 终止if (cntnode == 0) break;// 缩点,重新标记for (int i = 0; i < n; i++) {if (id[i] == -1)id[i] = cntnode++;}for (int i = 0; i < edges.size(); i++) {int v = edges[i].v;edges[i].u = id[edges[i].u];edges[i].v = id[edges[i].v];if (edges[i].u != edges[i].v)edges[i].d -= in[v];}n = cntnode;root = id[root];}return ans <= cost;}}solver;int main () {int cas, n, m, cost, u, v, c, b;scanf("%d", &cas);while (cas--) {int l = inf, r = 0;solver.init();scanf("%d%d%d", &n, &m, &cost);while (m--) {scanf("%d%d%d%d", &u, &v, &b, &c);solver.addEdge(u, v, c, b);r = max(r, b);l = min(l, b);}if (!solver.directedMST(0, n, cost, l))printf("streaming not possible.\n");else {while (l < r - 1) {int mid = (l + r) >> 1;if (!solver.directedMST(0, n, cost, mid))r = mid;elsel = mid;}printf("%d kbps\n", l);}}return 0;}


题目

During 2009 and 2010 ICPC world finals, the contest was webcasted via world wide web. Seeing this,some contest organizers from Ajobdesh decided that, they will provide a live stream of their contests toevery university in Ajobdesh. The organizers have decided that, they will provide best possible serviceto them. But there are two problems:1. There is no existing network between universities. So, they need to build a new network. However,the maximum amount they can spend on building the network is C.2. Each link in the network has a bandwidth. If, the stream’s bandwidth exceeds any of the link’savailable bandwidth, the viewers, connected through that link can’t view the stream.Due to the protocols used for streaming, a viewer can receive stream from exactly one other user(or the server, where the contest is organized). That is, if you have two 128kbps links, you won’t get256kbps bandwidth, although, if you have a stream of 128kbps, you can stream to any number of usersat that bandwidth.Given C, you have to maximize the minimum bandwidth to any user.

Input

First line of input contains T (≤ 50), the number of test cases. This is followed by T test cases.Each test case starts with an integer N, M, C (1 ≤ N ≤ 60, 1 ≤ M ≤ 104, 1 ≤ C ≤ 109), thenumber of universities and the number of possible links, and the budget for setting up the network.Each university is identified by an integer between 0 and N − 1, where 0 is the server.Next M lines each contain 4 integers, u, v, b, c (0 ≤ u, v < N, 1 ≤ b, c ≤ 106, u ̸= v), describinga possible link from university u to university v, that has the bandwidth of b kbps and of cost c. Alllinks are unidirectional.There is a blank line before each test case.

Output

For each test case, output the maximum bandwidth to stream.If it’s not possible, output ‘streaming not possible.’.

Sample Input

3

3 4 300

0 1 128 100

1 2 256 200

2 1 256 200

0 2 512 300

3 4 500

0 1 128 100

1 2 256 200

2 1 256 200

0 2 512 300

3 4 100

0 1 128 100

1 2 256 200

2 1 256 200

0 2 512 300

Sample Output

128 kbps

256 kbps

streaming not possible.

0 0
原创粉丝点击