Stream My Contest UVA - 11865(带权最小树形图+二分最小值最大化)

Posted wtsruvf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Stream My Contest UVA - 11865(带权最小树形图+二分最小值最大化)相关的知识,希望对你有一定的参考价值。

技术分享图片

 

 

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 10010, INF = 0x7fffffff;
int ID[maxn], IN[maxn], vis[maxn], pre[maxn];

struct node
{
    int u, v, c, b;
}Node[maxn*2];

bool dirmst(int root, int n, int m, int cost, int B)
{
    LL ans = 0;
    while(true)
    {
        for(int i=0; i<n; i++) IN[i] = INF; //记录最小前驱边的值

        //1、找最小前驱边
        for(int i=0; i<m; i++)
        {
            int u = Node[i].u;
            int v = Node[i].v;
            if(Node[i].c < IN[v] && u != v && Node[i].b >= B)
            {
                pre[v] = u;
                IN[v] = Node[i].c;
            //    cout<< e.v << "     " << e.u <<endl;
            }
        }

        //2、判断是否联通
        for(int i=0; i<n; i++)
        {
            if(i == root) continue;
            if(IN[i] == INF) return false;
        }

        //3、找环
        int cntnode = 0;
        mem(ID, -1);
        mem(vis, -1);
        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 j=pre[v]; j!=v; j=pre[j])
                {
                    ID[j] = 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<m; i++)
        {
            int v = Node[i].v;
            Node[i].u = ID[Node[i].u];
            Node[i].v = ID[Node[i].v];
            if(Node[i].u != Node[i].v)
                Node[i].c -= IN[v];
        }
        n = cntnode;
        root = ID[root];
    }
    if(ans <= cost)
        return true;
    return false;

}

int main()
{
    int T, n, m, cost;
    scanf("%d", &T);
    while(T--)
    {
        int l = INF, r = 0;
        scanf("%d%d%d", &n, &m, &cost);
        for(int i=0; i<m; i++)
        {
            scanf("%d%d%d%d", &Node[i].u, &Node[i].v, &Node[i].b, &Node[i].c);
            Node[i+m] = Node[i];
            l = min(l, Node[i].b);
            r = max(r, Node[i].b);
        }
        if(!dirmst(0, n, m, cost, l))
        {
            printf("streaming not possible.
");
            continue;
        }

        while(l <= r)
        {
            int mid = l + (r - l) / 2;
            for(int i=0; i<m; i++)
                Node[i] = Node[i+m];
            if(!dirmst(0, n, m, cost, mid)) r = mid - 1;
            else l = mid + 1;
        }
        printf("%d kbps
", r);

    }

    return 0;
}

 

以上是关于Stream My Contest UVA - 11865(带权最小树形图+二分最小值最大化)的主要内容,如果未能解决你的问题,请参考以下文章

UVA11161 Help My Brother (II)大数+递推

Uva 12299 带循环移动的RMQ(线段树)

UVA10305 拓扑排序

UVa11827(欧几里得算法)

UVA - 315 Network(tarjan求割点的个数)

Codeforces Round #588 (Div. 2) E. Kamil and Making a Stream(DFS)