You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

教室学生分配问题——基于网络流的求解方案问询

How to Model This Classroom Assignment Problem with Max-Flow

Great question! Let's walk through building a max-flow network to solve this problem, which will let us find the maximum number of students we can assign while following all your rules.

Step 1: Preprocess Valid Time Slots

First, formalize the valid options to simplify modeling:

  • For each student, collect all their available 50-minute time slots that fall entirely within the classroom's open hours for that day.
  • Assign a unique identifier to each distinct time slot (e.g., Slot_Mon_700 for Monday 7:00-7:50, Slot_Mon_750 for Monday 7:50-8:40, etc.).

Step 2: Define the Flow Network Nodes

We'll create a directed graph with these node types to encode all constraints:

  • Source Node (S): The starting point of all flow, representing the "supply" of students to assign.
  • Student Nodes: One node U_i for each student (e.g., U_Alice, U_Bob).
  • Stateful Slot Nodes: For each distinct time slot Slot_j, split it into three nodes to track consecutive usage state:
    • V_j_in: Entry point for the slot (students connect here regardless of sequence state).
    • V_j_0: Represents using this slot as the start of a new sequence (0 consecutive students before it).
    • V_j_1: Represents using this slot as the second in a consecutive sequence (1 student immediately before it).
    • V_j_out: Common exit node for the slot, used to route flow to the sink or subsequent valid slots.
  • Sink Node (T): The end point of all flow, representing successfully assigned students.

Step 3: Add Edges with Capacity Constraints

Each edge's capacity enforces a specific rule. Here's how to connect the nodes:

  1. Source to Students

    • Add a directed edge from S to each student node U_i with a capacity of 1.
    • This ensures each student is assigned at most one slot (since they only need one weekly visit).
  2. Students to Slot Entry Points

    • For each student U_i, add a directed edge from U_i to V_j_in for every slot Slot_j the student can attend. Set each edge's capacity to 1.
    • This represents the student's ability to choose that time slot.
  3. Slot State Transitions

    • Add directed edges from V_j_in to both V_j_0 and V_j_1, each with a capacity of 1.
    • This lets the slot be used either as the start of a new sequence or as the second in a consecutive pair.
    • Add directed edges from V_j_0 to V_j_out and V_j_1 to V_j_out, each with a capacity of 1. This enforces that only one student can use the slot.
  4. Enforce the 2-Student-Then-Rest Rule
    First, sort all slots by their start time. Then:

    • For each slot Slot_j (ends at time t_j), find all slots Slot_k where Slot_k starts exactly at t_j (immediately after Slot_j ends). Add a directed edge from V_j_out to V_k_1 with a capacity of 1. This allows a second consecutive student after the first.
    • For each slot Slot_j, find all slots Slot_m where Slot_m starts at least 20 minutes after t_j. Add a directed edge from V_j_out to V_m_0 with a capacity of 1. This lets us start a new sequence only after the required rest period.
    • Add a directed edge from every V_j_out to T with a capacity of 1. This accounts for ending the sequence after this slot (whether it's a single student or the second in a pair).

    This setup prevents three consecutive students: after using a slot as the second in a sequence, the only valid next step is starting a new sequence after a 20-minute rest.

Step 4: Compute the Maximum Flow

Once the network is built, compute the maximum flow from S to T. The value of the maximum flow equals the maximum number of students we can assign.

To get the actual assignments:

  • For each student node U_i with flow from S, trace the path through the network to find which slot node it connects to. That's the slot assigned to the student.

Example Walkthrough

Suppose:

  • Classroom is open Monday 7:00-9:00.
  • Valid slots: A (7:00-7:50), B (7:50-8:40) (slot C 8:40-9:30 is invalid since it exceeds closing time).
  • Students: Alice can attend A/B; Bob can attend A/B; Charlie can attend A.

The network will allow a maximum flow of 2: we can assign Alice to A and Bob to B (a valid consecutive pair), or Charlie to A and Alice to B, etc. We can't assign all three because slot A can only take one student, and no valid slots remain after B.


内容的提问来源于stack exchange,提问作者SoptikHa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:51:46