USACO Section 4.2 The Perfect Stall(二分图匹配)

二分图的最大匹配。我是用最大流求解。加个源点s和汇点t;s和每只cow、每个stall和t 连一条容量为1有向边,每只cow和stall(that the cow is willing to produce milk in )也连一条容量为1的边。然后就用ISAP。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>

#define rep(i,l,r) for(int i=l;i<r;i++)
#define clr(x,c) memset(x,c,sizeof(x))

using namespace std;

const int inf=0x3f3f3f3f,maxn=400+5;

struct edge {
    int from,to,cap,flow;
};

struct ISAP {
    int n,m,s,t;
    vector<edge> edges;
    vector<int> g[maxn];
    int d[maxn];
    int cur[maxn];
    int p[maxn];
    int num[maxn];
    
    void init(int n) {
        this->n=n;
        rep(i,0,n) g[i].clear();
        edges.clear();
        clr(d,0);
        clr(num,0);
        clr(cur,0);
        rep(i,0,n) num[d[i]]++;
    }
    
    void addEdge(int from,int to,int cap) {
        edges.push_back((edge){from,to,cap,0});
        edges.push_back((edge){to,from,0,0,});
        m=edges.size();
        g[from].push_back(m-2);
        g[to].push_back(m-1);
    }
    
    int augment() {
        int x=t,a=inf;
        while(x!=s) {
            edge e=edges[p[x]];
            a=min(a,e.cap-e.flow);
            x=edges[p[x]].from;
        }
        x=t;
        while(x!=s) {
            edges[p[x]].flow+=a;
            edges[p[x]^1].flow-=a;
            x=edges[p[x]].from;
        }
        return a;
    }
    
    int maxFlow(int s,int t) {
        this->s=s; this->t=t;
        int flow=0;
        int x=s;
        while(d[s]<n) {
            if(x==t) {
                flow+=augment();
                x=s;
            }
            int ok=0;
            rep(i,cur[x],g[x].size()) {
                edge e=edges[g[x][i]];
                if(e.cap>e.flow && d[x]==d[e.to]+1) {
                    ok=1;
                    p[e.to]=g[x][i];
                    cur[x]=i;
                    x=e.to;
                    break;
                }
            }
            if(!ok) {
                int m=n-1;
                rep(i,0,g[x].size()) {
                    edge e=edges[g[x][i]];
                    if(e.cap>e.flow) m=min(m,d[e.to]);
                }
                if(--num[d[x]]==0) break;
                num[d[x]=m+1]++;
                cur[x]=0;
                if(x!=s) x=edges[p[x]].from;
            }
        }
        return flow;
    }
} isap;

int s() {
    int n,m;
    cin>>n>>m;
    isap.init(n+m+2);
    rep(i,0,n) {
        int t;
        scanf("%d",&t);
        isap.addEdge(0,i+1,1);
        rep(j,0,t) {
            int h;
            scanf("%d",&h);
            h+=n;
            isap.addEdge(i+1,h,1);
        }
    }
    rep(i,0,m) {
        int x=i+n+1;
        isap.addEdge(x,m+n+1,1) ;
    }
    return isap.maxFlow(0,n+m+1);
}

int main() {
    freopen("stall4.in","r",stdin);
    freopen("stall4.out","w",stdout);
    
    cout<<s()<<endl;
    
    return 0;
}
View Code
The Perfect Stall
Hal Burch

Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering problems, all the stalls in the new barn are different. For the first week, Farmer John randomly assigned cows to stalls, but it quickly became clear that any given cow was only willing to produce milk in certain stalls. For the last week, Farmer John has been collecting data on which cows are willing to produce milk in which stalls. A stall may be only assigned to one cow, and, of course, a cow may be only assigned to one stall.

Given the preferences of the cows, compute the maximum number of milk-producing assignments of cows to stalls that is possible.

PROGRAM NAME: stall4

INPUT FORMAT

Line 1:One line with two integers, N (0 <= N <= 200) and M (0 <= M <= 200). N is the number of cows that Farmer John has and M is the number of stalls in the new barn.
Line 2..N+1:N lines, each corresponding to a single cow. The first integer (Si) on the line is the number of stalls that the cow is willing to produce milk in (0 <= Si <= M). The subsequent Si integers on that line are the stalls in which that cow is willing to produce milk. The stall numbers will be integers in the range (1..M), and no stall will be listed twice for a given cow.

SAMPLE INPUT (file stall4.in)

5 5
2 2 5
3 2 3 4
2 1 5
3 1 2 5
1 2 

OUTPUT FORMAT

A single line with a single integer, the maximum number of milk-producing stall assignments that can be made.

SAMPLE OUTPUT (file stall4.out)

4

转载于:https://www.cnblogs.com/JSZX11556/p/4295764.html

内容概要:本文围绕可变桨叶四旋翼无人机的规范控制与点对点运动模拟展开,重点研究优化推力分配策略在翻转动作中的应用与性能比较。通过Matlab代码实现,构建了四旋翼动力学模型,并设计了多种控制算法以实现精确的姿态调整与轨迹跟踪。研究对比了不同推力分配方案在执行高机动性翻转动作时的稳定性、能耗效率与响应速度,旨在提升无人机在复杂飞行任务中的动态性能与控制精度。该仿真研究为无人机飞控系统的设计与优化提供了理论依据和技术支持。; 适合人群:具备一定自动控制理论基础和Matlab编程能力,从事无人机控制、飞行器动力学或机器人系统研究的科研人员及研究生。; 使用场景及目标:① 实现四旋翼无人机在三维空间中的精确点对点运动控制;② 对比分析不同推力分配策略在执行翻转等高难度动作时的控制效果与能耗表现,优化飞行性能;③ 为无人机自主飞行、特技飞行及复杂环境下的机动控制提供算法验证平台。; 阅读建议:此资源以Matlab仿真为核心,建议读者结合相关控制理论知识,深入理解代码实现细节,重点关注动力学建模、控制律设计与推力分配模块。在学习过程中,应动手调试参数,复现文中翻转动作的仿真结果,并尝试拓展至其他复杂飞行任务,以加深对无人机控制机理的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值