题意:给定一个网络,一个服务器,其他的是客户机,有 m 条连线,每条有一个带宽和花费(单向边),让你用不超过 c 的花费,使得 0 到 所有的机器都能到达,并且使得最小带宽最大。
析:很明显是二分题,然后在判断,就是保证从 0 到所有的点都是通路,这就是最小树形图,直接上模板就好。
代码如下;
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #include <list> #include <assert.h> #include <bitset> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define fi first #define se second #define pb push_back #define sqr(x) ((x)*(x)) #define ms(a,b) memset(a, b, sizeof a) #define sz size() #define pu push_up #define pd push_down #define cl clear() #define all 1,n,1 #define FOR(i,x,n) for(int i = (x); i < (n); ++i) #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 1e20; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 60 + 10; const LL mod = 1e9 + 7; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c) { return r >= 0 && r < n && c >= 0 && c < m; } struct Edge{ int u, v, cost; }; Edge edge[10050], a[10050]; int b[10050]; int pre[maxn], id[maxn], vis[maxn], in[maxn]; int solve(int rt, int n, int m){ int ans = 0; while(true){ ms(in, INF); FOR(i, 0, m) if(edge[i].u != edge[i].v && edge[i].cost < in[edge[i].v]){ pre[edge[i].v] = edge[i].u; in[edge[i].v] = edge[i].cost; } FOR(i, 0, n) if(i != rt && in[i] == INF) return -1; int tn = 0; ms(id, -1); ms(vis, -1); in[rt] = 0; for(int i = 0; i < n; ++i){ ans += in[i]; int v = i; while(vis[v] != i && id[v] == -1 && v != rt){ vis[v] = i; v = pre[v]; } if(v != rt && id[v] == -1){ for(int u = pre[v]; u != v; u = pre[u]) id[u] = tn; id[v] = tn++; } } if(tn == 0) break; FOR(i, 0, n) if(id[i] == -1) id[i] = tn++; for(int i = 0; i < m; ){ int v = edge[i].v; edge[i].u = id[edge[i].u]; edge[i].v = id[edge[i].v]; if(edge[i].u != edge[i].v) edge[i++].cost -= in[v]; else swap(edge[i], edge[--m]); } n = tn; rt = id[rt]; } return ans; } bool judge(int mid, int c){ int cnt = 0; for(int i = 0; i < m; ++i) if(b[i] >= mid) edge[cnt++] = a[i]; int ans = solve(0, n, cnt); if(ans == -1) return false; bool ok = ans <= c; return ans <= c; } int main(){ int T; cin >> T; while(T--){ int c; scanf("%d %d %d", &n, &m, &c); int l = 1, r = 0; for(int i = 0; i < m; ++i) scanf("%d %d %d %d", &a[i].u, &a[i].v, &b[i], &a[i].cost), r = max(r, b[i]); if(!judge(0, c)){ puts("streaming not possible."); continue; } while(l <= r){ int m = l + r >> 1; if(judge(m, c)) l = m + 1; else r = m - 1; } printf("%d kbps\n", l - 1); } return 0; }