poj 3308 Paratroopers 二分图最小点权覆盖集+ 最小割+邻接表

本文深入探讨了AI音视频处理领域中的视频分割与语义识别技术,介绍了如何通过计算机视觉算法实现对视频内容的智能解析与理解。包括视频分割的方法、语义识别的应用场景及其实现过程,旨在为开发者提供有效的解决方案,提升音视频处理的智能化水平。

Description

It is year 2500 A.D. and there is a terrible war between the forces of the Earth and the Mars. Recently, the commanders of the Earth are informed by their spies that the invaders of Mars want to land some paratroopers in them × n grid yard of one their main weapon factories in order to destroy it. In addition, the spies informed them the row and column of the places in the yard in which each paratrooper will land. Since the paratroopers are very strong and well-organized, even one of them, if survived, can complete the mission and destroy the whole factory. As a result, the defense force of the Earth must kill all of them simultaneously after their landing.

In order to accomplish this task, the defense force wants to utilize some of their most hi-tech laser guns. They can install a gun on a row (resp. column) and by firing this gun all paratroopers landed in this row (resp. column) will die. The cost of installing a gun in the ith row (resp. column) of the grid yard is ri (resp.ci ) and the total cost of constructing a system firing all guns simultaneously is equal to the product of their costs. Now, your team as a high rank defense group must select the guns that can kill all paratroopers and yield minimum total cost of constructing the firing system.

Input

Input begins with a number T showing the number of test cases and then,T test cases follow. Each test case begins with a line containing three integers 1 ≤m ≤ 50 , 1 ≤ n ≤ 50 and 1 ≤ l ≤ 500 showing the number of rows and columns of the yard and the number of paratroopers respectively. After that, a line withm positive real numbers greater or equal to 1.0 comes where the ith number isri and then, a line with n positive real numbers greater or equal to 1.0 comes where theith number is ci. Finally, l lines come each containing the row and column of a paratrooper.

Output

For each test case, your program must output the minimum total cost of constructing the firing system rounded to four digits after the fraction point.

Sample Input

1
4 4 5
2.0 7.0 5.0 2.0
1.5 2.0 2.0 8.0
1 1
2 2
3 3
4 4
1 4

Sample Output

16.0000

 

 //

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
const int inf=(1<<28);
using namespace std;
#define maxn 500000
struct edge
{
    int u,v,next,pre;
    double f;
}e[2*maxn];
int num,rnum;
int head[maxn],rhead[maxn];
int d[maxn];
int numb[maxn];
int start[maxn];
int n,m;//见图后的点数(1->n)和原图边数
int p[maxn];
int source,sink;
//要初始化source 和 sink,重定义n
void Init()
{
    memset(head,-1,sizeof(head));
    memset(rhead,-1,sizeof(rhead));
    memset(p,-1,sizeof(p));
    num=0;
    return ;
}
void BFS()
{
    int i,j;
    for(i=1;i<=n;i++)
    {
        d[i]=n;
        numb[i]=0;
    }
    int Q[maxn],head(0),tail(0);
    d[sink]=0;
    numb[0]=1;
    Q[++tail]=sink;
    while(head<tail)
    {
        i=Q[++head];
        for(j=rhead[i];j!=-1;j=e[j].pre)
        {
            if(e[j].f==0||d[e[j].u]<n)
                continue;
            d[e[j].u]=d[i]+1;
            numb[d[e[j].u]]++;
            Q[++tail]=e[j].u;
        }
    }
    return ;
}
double Augment()
{
    int i;
    double tmp=inf;
    for(i=p[sink];i!=-1;i=p[e[i].u])
    {
        if(tmp>e[i].f)
            tmp=e[i].f;
    }
    for(i=p[sink];i!=-1;i=p[e[i].u])
    {
        e[i].f-=tmp;
        e[i^1].f+=tmp;
    }
    return tmp;
}
int Retreat(int &i)
{
    int tmp,j,mind(n-1);
    for(j=head[i];j!=-1;j=e[j].next)
    {
        if(e[j].f>0&&d[e[j].v]<mind)
            mind=d[e[j].v];
    }
    tmp=d[i];
    d[i]=mind+1;
    numb[tmp]--;
    numb[d[i]]++;
    if(i!=source)
        i=e[p[i]].u;
    return numb[tmp];
}
double maxflow()
{
    double flow(0);
    int i,j;
    BFS();
    for(i=1;i<=n;i++)
        start[i]=head[i];
    i=source;
    while(d[source]<n)
    {
        for(j=start[i];j!=-1;j=e[j].next)
            if(e[j].f>0&&d[i]==d[e[j].v]+1)
                break;
        if(j!=-1)
        {
            start[i]=j;
            p[e[j].v]=j;
            i=e[j].v;
            if(i==sink)
            {
                flow+=Augment();
                i=source;
            }
        }
        else
        {
            start[i]=head[i];
            if(Retreat(i)==0)
                break;
        }
    }
    return flow;
}
//a->b=c;
void addedge(int a,int b,double c)
{
    e[num].next=head[a];
    head[a]=num;
    e[num].pre=rhead[b];
    rhead[b]=num;
    e[num].f=c;
    e[num].u=a;
    e[num++].v=b;
    e[num].next=head[b];
    head[b]=num;
    e[num].pre=rhead[a];
    rhead[a]=num;
    e[num].u=b;
    e[num].v=a;
    e[num++].f=0;
    return ;
}
int main()
{
    int ci;scanf("%d",&ci);
    while(ci--)
    {
        Init();
        int tn,tm,tp;scanf("%d%d%d",&tn,&tm,&tp);
        source=1,sink=tn+tm+2;
        n=tn+tm+2;
        for(int i=1;i<=tn;i++)
        {
            double x;scanf("%lf",&x);x=log(x);
            addedge(source,i+1,x);
        }
        for(int i=1;i<=tm;i++)
        {
            double x;scanf("%lf",&x);x=log(x);
            addedge(i+1+tn,sink,x);
        }
        for(int i=0;i<tp;i++)
        {
            int u,v;scanf("%d%d",&u,&v);
            addedge(u+1,v+tn+1,inf);
        }
        double cnt=maxflow();
        cnt=exp(cnt);
        printf("%.4lf\n",cnt);
    }
    return 0;
}

### 关于二分图小顶覆盖的算法实现 #### 1. 小顶覆盖的概念 小顶覆盖是指在一个二分图中选取尽可能少的节,使得这些节能够覆盖所有的边。换句话说,对于每一条边 (u, v),至少有一个端 u 或 v 被选入覆盖中。 根据 König 定理,在任意无向二分图中,小顶覆盖的数量等于该图的大匹配数量[^1]。 --- #### 2. 算法原理 为了求解二分图小顶覆盖,通常采用 **匈牙利算法** 来计算大匹配数。具体过程如下: - 构建一个二分图 G(X,Y,E),其中 X 和 Y 是两个互不相交的节合,E 表示连接它们的边。 - 使用匈牙利算法找到二分图大匹配 M。 - 基于大匹配的结果,通过以下方式构造小顶覆盖: - 将左侧未被匹配的节加入到覆盖中; - 对右侧已被匹配的节也加入到覆盖中。 终得到的覆盖大小即为小顶覆盖的数量[^2]。 --- #### 3. 实现代码 以下是基于 Python 的实现代码,利用匈牙利算法完成二分图小顶覆盖的计算: ```python from collections import defaultdict def hungarian_algorithm(graph, n, m): """ 匈牙利算法用于寻找二分图大匹配 :param graph: 邻接表表示的二分图 {X -> [Y]} :param n: 左侧节数目 :param m: 右侧节数目 :return: 大匹配结果 """ match_y = [-1] * m # 记录右侧节对应的匹配关系 visited = None # 当前轮次访问标记 def dfs(u): for v in graph[u]: if not visited[v]: visited[v] = True if match_y[v] == -1 or dfs(match_y[v]): match_y[v] = u return True return False matching_count = 0 for i in range(n): visited = [False] * m if dfs(i): matching_count += 1 return matching_count, match_y def min_vertex_cover(graph, n, m): """ 求解二分图小顶覆盖 :param graph: 邻接表表示的二分图 {X -> [Y]} :param n: 左侧节数目 :param m: 右侧节数目 :return: 小顶覆盖合 """ max_matching, match_y = hungarian_algorithm(graph, n, m) cover_x = set() # 左侧需要覆盖的节 cover_y = set() # 右侧需要覆盖的节 unmatched_in_x = set(range(n)) # 初始认为所有左侧节都未匹配 matched_in_y = set() for y in range(m): # 找出右侧已匹配的节 if match_y[y] != -1: unmatched_in_x.discard(match_y[y]) # 如果某个左侧节参与了匹配,则移除 matched_in_y.add(y) cover_x.update(unmatched_in_x) # 添加左侧未匹配的节 cover_y.update(set(range(m)) - matched_in_y) # 添加右侧未匹配的节 return list(cover_x), list(cover_y) # 测试用例 if __name__ == "__main__": # 输入邻接表形式的二分图 graph = { 0: [0, 1], 1: [0, 2], 2: [1, 3] } n = 3 # 左侧节数 m = 4 # 右侧节数 result_x, result_y = min_vertex_cover(graph, n, m) print(f"左侧需覆盖的节: {result_x}") print(f"右侧需覆盖的节: {result_y}") ``` 上述代码实现了二分图小顶覆盖功能,核心部分依赖匈牙利算法来获取大匹配,并据此推导出覆盖所需的节合[^3]。 --- #### 4. 应用实例 考虑 POJ 3041 Asteroids 这道题目,其本质是一个二分图小顶覆盖问题。给定一组障碍物坐标,将其转化为二分图模型后,可以通过以上方法高效解决。输出的是满足条件的小射击次数[^4]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值