ZOJ 3792 Romantic Value(最小割问题)

本文深入探讨了大数据开发领域的关键技术,包括Hadoop、Spark、Flink等,并结合实际应用场景,详细介绍了如何利用这些技术进行数据处理、分析与挖掘。

题目的意思很扯,但是题意很好理解就是让你求出那个很神奇的公式,需要求出给出的p,q点之间的最小割,还有就是割边的数目。

在统计割边的数目的时候,把权值乘了一个大数这样的话取余就是割边的数目了啊。

需要用dinic算法求出最大流。

基本上就是模版题啊。

Romantic Value

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Farmer John is a diligent man. He spent a lot of time building roads between his farms. From his point of view, every road is romantic because the scenery along it is very harmonious and beautiful. Recently, John is immersed in poetry, he wants stay alone and enjoy the wonderful words. But his little brother always disturbs him. This night, fortunately, his little brother does not stay the same farm with him. So, he wants to destroy some roads to keep himself quiet for a few days(then no route exist between John and his brother). Of course, John love his romantic roads, so he want to separate him and his brother with least romantic cost.

There are N farms(numbered from 1 to N) and M undirected roads each with a romantic value c(indicate how much Farmer John loves it). Now John stays in farm p and his little brother stay in farm q. John wants to first minimize the romantic value lost, then to destroy as few roads as possible. Help him to calculate the ratio of [sum of the remainder roads' value]/[the amount of removed roads](not necessary to maximisation this ratio) when he achieves his goal.

Input

The first line is a single integer T, indicate the number of testcases. Then follows T testcase. For each testcase, the first line contains four integers N M p q(you can assume p and q are unequal), then following M lines each contains three integer a b c which means there is an undirected road between farm a and farm b with romantic value c. (2<=N<=50, 0<=M<=1000, 1<=c<1000, 1<=p,q<=N)

Output

For each test case, print the ratio in a single line(keep two decimal places). If p and q exist no route at the start, then output "Inf".

Sample Input
1
4 5 1 4
1 2 1
1 3 1
2 4 2
3 4 2
2 3 1
Sample Output
2.50
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-8
#define M 1000100
//#define LL __int64
#define LL long long
#define INF 0x7ffffff
#define PI 3.1415926535898


const int maxn = 10010;

using namespace std;

int pre[maxn];
int head[maxn];
int cnt;
int n, m;
int deep[maxn];

struct node
{
    int v, next;
    int w;
};

node f[maxn];
void init()
{
    cnt = 0;
    memset(head, -1, sizeof(head));
}

void add(int u, int v, int w)
{
    f[cnt].v = v;
    f[cnt].w = w;
    f[cnt].next = head[u];
    head[u] = cnt++;

    f[cnt].v = u;
    f[cnt].w = 0;
    f[cnt].next = head[v];
    head[v] = cnt++;
}

bool bfs(int s, int t)
{
    memset(deep, -1, sizeof(deep));
    queue<int>q;
    while(!q.empty())
        q.pop();
    q.push(s);
    deep[s] = 0;
    while(!q.empty())
    {
        int temp = q.front();
        q.pop();
        int p = head[temp];
        while(p != -1)
        {
            int v = f[p].v;
            if(deep[v] == -1 && f[p].w > 0)
            {
                deep[v] = deep[temp]+1;
                q.push(v);
            }
            p = f[p].next;
        }
    }
    return deep[t] != -1;
}

int dfs(int s, int a, int t)
{
    if(s == t || a == 0)
        return a;
    int flow = 0, ff;
    for(int kk = head[s]; kk != -1; kk = f[kk].next)
    {
        node e;
        e.next = f[kk].next;
        e.v = f[kk].v;
        e.w = f[kk].w;
        if(deep[e.v] == deep[s]+1 && (ff = dfs(e.v, min(a, e.w), t)) > 0)
        {
            f[kk].w -= ff;
            f[kk^1].w += ff;
            flow += ff;
            a -= ff;
            if(a == 0)
                return flow;
        }
    }
    deep[s] = -1;
    return flow;
}

int main()
{
    int T;
    cin >>T;
    while(T--)
    {
        int s, t;
        init();
        cin >>n>>m>>s>>t;
        int u, v, w;
        int sum = 0;
        for(int i = 1; i <= m; i++)
        {
            cin >>u>>v>>w;
            sum += w;
            add(u, v, w*10000+1);
            add(v, u, w*10000+1);
        }
        int flow = 0;
        while(bfs(s,t))
            flow += dfs(s, INF, t);
        if(flow == 0)
        {
            cout<<"Inf"<<endl;
            continue;
        }
        int ans = flow/10000;
        int ans2 = flow%10000;
        double res = (double)(sum-ans)/ans2;
        printf("%.2lf\n",res);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值