Time Limit: 2 second(s) | Memory Limit: 32 MB |
During 2009 and 2010 ICPC world finals, the contests were webcasted via World Wide Web. Seeing this, some contest organizers from Ajobdesh decided that, they will provide a live stream of their contests to every university in Ajobdesh. The organizers have decided that, they will provide best possible service to 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's available 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 get 256kbps bandwidth, although, if you have a stream of 128kbps, you can stream to any number of users at that bandwidth.
Given C, find the maximum possible bandwidth they can stream.
Input
Input starts with an integer T (≤ 35), denoting the number of test cases.
Each case starts with a blank line. Next line contains three integers N, M, C (1 ≤ N ≤ 60, 1 ≤ M ≤ 104, 1 ≤ C ≤ 109), the number of universities and the number of possible links, and the budget for setting up the network respectively. Each university is identified by an integer between 0 and N-1, where 0denotes the server.
Each of the next M lines contains four integers u, v, b, c (0 ≤ u, v < N, 1 ≤ b, c ≤ 106), describing a possible link from university u to university v, that has the bandwidth of b kbps and of cost c. All links are unidirectional. There can be multiple links between two universities.
Output
For each case, print the case number and the maximum possible bandwidth to stream. If it's not possible, print "impossible". See the samples for details.
Sample Input | Output for 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 | Case 1: 128 kbps Case 2: 256 kbps Case 3: impossible |
题意:n个点,m条有向边,每条边有一个传输数据的最大容量b和修建该条边的费用cost,现在总服务器在点0位置,要修若干条边使得每个点都能接收到点0的信息,并且总的修建费用不超过C,求满足条件的数据传输容量最小值的最大值是多少。
思路:二分+最小树形图。
代码:
#include <iostream>
#include <functional>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define DBG pf("Hi\n")
typedef long long ll;
using namespace std;
#define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 1005;
const int MAXN = 100;
const int MAXM = 50010;
const int N = 1005;
struct Edge
{
int u,v,b,cost;
}edge[MAXM],e[MAXM];
int pre[MAXN],id[MAXN],visit[MAXN],in[MAXN];
int n,m,C,num;
void addedge(int u,int v,int w)
{
edge[num].u=u;
edge[num].v=v;
edge[num++].cost=w;
}
int zhuliu(int root, int n, int m, Edge edge[])
{
int res = 0, u, v;
while (1)
{
for (int i = 0; i < n; i++)
in[i] = INF;
for (int i = 0; i < m; i++)
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 (int i = 0; i < n; i++)
if (i != root && in[i] == INF)
return -1;
int tn = 0;
memset(id, -1, sizeof(id));
memset(visit, -1, sizeof(visit));
in[root] = 0;
for (int i = 0; i < n; i++)
{
res += in[i];
v = i;
while (visit[v] != i && id[v] == -1 && v != root)
{
visit[v] = i;
v = pre[v];
}
if (v != root && id[v] == -1)
{
for (int u = pre[v]; u != v; u = pre[u])
id[u] = tn;
id[v] = tn++;
}
}
if (tn == 0)break;
for (int i = 0; i < n; i++)
if (id[i] == -1)
id[i] = tn++;
for (int i = 0; i < m;)
{
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;
root = id[root];
}
return res;
}
void Build(int mid)
{
num=0;
for (int i=0;i<m;i++)
if (e[i].b>=mid)
addedge(e[i].u,e[i].v,e[i].cost);
}
bool isok(int mid)
{
Build(mid);
int ans=zhuliu(0,n,num,edge);
if (ans==-1) return false;
if (ans<=C) return true;
return false;
}
void solve(int x)
{
int l=0,r=x,ans=-1;
while (l<=r)
{
int mid=(l+r)>>1;
if (!isok(mid))
r=mid-1;
else
{
l=mid+1;
ans=mid;
}
}
if (ans==-1) printf("impossible\n");
else printf("%d kbps\n",ans);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
#endif
int t,i,j,u,v,b,w,cas=0;
scanf("%d",&t);
while (t--)
{
scanf("%d%d%d",&n,&m,&C);
int Max=0;
for (i=0;i<m;i++)
{
scanf("%d%d%d%d",&e[i].u,&e[i].v,&e[i].b,&e[i].cost);
Max=max(Max,e[i].b);
}
printf("Case %d: ",++cas);
solve(Max);
}
return 0;
}