uva11865 - Stream My Contest

G

Stream My Contest

Input: Standard Input

Output: Standard Output

 

During 2009 and 2010 ICPC world finals, the contest was webcasted via world wide web. Seeing this, some contest organizers fromAjobdesh 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, 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), the number 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), 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 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                               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

128 kbps

256 kbps

streaming not possible.

 

 



在最小生成树上提升的最小树形图,采用朱刘算法

不用建边遍历,也不用tarjan缩圈

方法很巧妙,了解了建图步骤后也就是个模版题了,本题再加上一个二分就行了

http://blog.sina.com.cn/s/blog_6af663940100ls4h.html

这个博客上说的很详细,

引用一下


朱刘算法 <wbr>求解最小树形图



#include<cstdio>
#include<cstring>
#include<iostream>
#include<stack>

using namespace std;

#define N 110
#define M 10010

struct Edge
{
    int u,v,b,c;
};

typedef long long LL;
const int inf=0x3f3f3f3f;

Edge e[M*2];
int n,m,cnt,cost;
int in[N],id[N];
int vis[N],pre[N];

bool Direct_Mst(int root,int n,int B)
{
    int  ans=0;
    while (1)
    {
        memset(pre,-1,sizeof(pre));
        for (int i=0; i<n; i++)
            in[i]=inf;
        // 求入边
        for (int i=0; i<m; i++)
        {
            if (e[i].u!=e[i].v&&e[i].b>=B&&e[i].c<in[e[i].v])
            {
                in[e[i].v]=e[i].c;
                pre[e[i].v]=e[i].u;
            }
        }
        //判是否有树
        for (int i=0; i<n; i++)
        {
            if (i==root) continue;
            if (in[i]==inf) return false;
        }
        //找环
        int cnt=0;
        memset(vis,-1,sizeof(vis));
        memset(id,-1,sizeof(id));
        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]=cnt;
                id[v]=cnt;
                cnt++;
            }
        }
        if (cnt==0) break;
        for (int i=0; i<n; i++)
            if (id[i]== -1)
                id[i]=cnt++;
        // 缩环,重新建边
        for (int i=0; i<m; i++)
        {
            int tmp=e[i].v;
            e[i].u=id[e[i].u];
            e[i].v=id[e[i].v];
            if (e[i].u!=e[i].v)
                e[i].c-=in[tmp];
        }
        //重置根节点
        root=id[root];
        n=cnt;
    }
    return (ans<=cost);
}


int main()
{
    int T;
    scanf("%d",&T);
    while (T--)
    {

        scanf("%d%d%d",&n,&m,&cost);
        int l=inf,r=0;
        for (int i=0; i<m; i++)
        {
            scanf("%d%d%d%d",&e[i].u,&e[i].v,&e[i].b,&e[i].c);
            e[i+m]=e[i];
            r=max(r,e[i].b);
            l=min(l,e[i].b);
        }
        if (Direct_Mst(0,n,l)==false)
        {
            printf("streaming not possible.\n");
            continue;
        }

        while (l<r)
        {
            int mid=l+(r-l+1)/2;
            for (int i=0; i<m; i++)
                e[i]=e[i+m];
            if (Direct_Mst(0,n,mid)==true ) l=mid;
            else
                r=mid-1;
        }
        printf("%d kbps\n",l);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值