We need to find optimal schedules for k agents starting at given nodes (crossroads) to reach node 1. We control when they start moving, their waiting times, and the routes (paths) they take (they can wait at any node). The discontent has two components:
-
For each agent, when they arrive at time t (minutes taken from start to arrival), we add c * t to the discontent. So sum over agents of c * arrival_time.
-
For each street (edge) and each direction, at each discrete time minute (the beginning? "whenever x members of the organization walk along the same street at the same moment in the same direction, dx^2 is added". The phrasing: "In the beginning of each minute each person is located at some crossroad. He or she can either wait a minute at this crossroad, or spend a minute to walk from the current crossroad along some street to another crossroad ... As soon as a person reaches crossroad number 1, he or she immediately comes to the leader's house and attends the meeting." So time is discrete, each minute is a step. At each minute, each person either stays (wait) or moves along an edge, taking one minute to traverse it. If multiple persons traverse the same edge in the same direction during the same minute, then we incur a cost d * (number of persons)^2.
But note: if persons are on the same edge but maybe at different positions? Since each minute they either start at node u, then decide to walk to v, they will be walking for the entire minute and then at the end of minute they arrive at v. So at the instant they are walking, they are on the edge. The cost is incurred "whenever x members of the organization walk along the same street at the same moment in the same direction, dx^2 is added". So each minute we consider each edge and each direction, count how many people are walking on that edge that minute (in that direction), add d * (count)^2.
Note: If persons cross the edge in opposite directions in the same minute, they are not considered together because directions differ; also no cost for waiting at nodes.
Goal: minimize total discontent.
We have n ≤ 50, m ≤ 50, k ≤ 50, c,d ≤ 50. So small sizes. But time horizon could be large? Potentially we can wait arbitrarily long, but we wouldn't want to wait too much because that adds c per agent per minute waiting and arrival time delayed. Also moving along edges adds c per minute as part of total travel time, and also possibly d cost for congestion.
We need to decide routes and timing to balance congestion cost vs travel time.
This looks like a flow over time or scheduling problem with quadratic congestion costs. Because agents are identical in cost structure (except starting nodes). Since n,m,k small, maybe we can model it as min-cost flow in a time-expanded network? But quadratic costs per edge per time unit are not linear.
But we note that d * x^2 is convex in x. So we can use techniques like representing each additional person on an edge at a time adds marginal cost d*( (x+1)^2 - x^2) = d*(2x+1). So it's convex. This suggests we can use min-cost flow with edge costs that are convex (piecewise linear) by splitting edges into multiple unit-capacity edges with increasing costs.
Because we have discrete time, we can build a time-expanded graph where each node (v,t) represents being at crossroad v at the beginning of minute t (or after t steps?). We need to define time steps from 0 to T (some max time). Each agent must start at its source at time 0 (t=0), and must end at node 1 at some time t (arrival). The action: at each time step t, they can either stay (wait) at node v, which moves them to (v, t+1) with cost? Wait doesn't incur congestion cost because not on edge, but adds to time cost via arrival time. However the ct arrival cost is added at the moment of arrival. So we need to account for arrival time cost: it's c * (arrival_time). That can be modeled as a cost on the "arrival" edge from (1,t) to sink with cost ct? Or we could add it as cost per time spent anywhere (including waiting) because arrival time is total minutes taken. If we consider that each minute that passes before arrival adds c to the cost? Actually for each agent, the total cost contributed from the arrival time is c * (time when reaching node 1). That's equivalent to paying c per minute for each minute from start until arrival (including waiting and travel). Because sum of c per minute over all minutes before arrival = c * arrival_time. So we can model that as: for each agent, for each time step they are still "in transit" (i.e., haven't reached node 1 yet), they incur a cost c per minute. That is easier: we can add a cost of c for each agent for each unit of time they are in the system (whether waiting or moving). Additionally, when moving, they incur congestion cost d * (x^2) per edge-direction per minute (shared among agents). So total cost = sum over agents of c * (arrival time) + sum over minutes, edges, directions d * (number on that edge-direction)^2.
But careful: The arrival time cost is c * x where x is number of minutes it took to reach crossroad 1. That's exactly c per minute per agent. So yes.
Thus we can think of each agent being a "flow unit" that moves from source node (starting crossroad at time 0) to sink (any time at node 1) and we incur costs: a "holding cost" of c per unit time per unit of flow that is still in the system (i.e., not yet absorbed at sink), plus movement costs that are convex congestion costs on edges per time step.
This is reminiscent of min-cost flow over time with convex costs. We can construct a time-expanded network up to some maximum time T. Because n,m,k small, we can bound T. How large T might be needed? We could wait arbitrarily long, but costs increase with time, so we wouldn't wait too long. Upper bound: since waiting adds c per minute per agent, it's always better to arrive as early as possible if no congestion. But congestion may cause waiting to avoid high d*x^2 cost. However there is a trade-off: you might want to spread out usage of edges over time to avoid high squared cost. But since c per minute per agent is linear, and congestion cost is quadratic in batch size, there is an optimal dispersion.
We need to compute minimum total cost. Since k ≤ 50, we could consider T not too large: maybe up to something like (k * path length) but we might need larger if waiting helps reduce congestion? For each edge, it's beneficial to not send too many at once. With convex cost, sending them evenly over time might be optimal. The worst-case might be when all agents start at same node far away, and the only path to 1 is a single edge (like a star). Then we have to send k agents across that edge. If we send them all at same time, cost = dk^2 for that minute, plus c(travel times). If we stagger them, we incur additional waiting cost for later ones (c per minute) but reduce congestion cost. With linear waiting cost vs quadratic congestion, the optimal schedule would balance them. Since d and c are up to 50, k up to 50, the optimal spread might be limited. For a single bottleneck edge, we would send them in batches. Suppose we have a path of length L (number of edges) from start to 1. The optimal schedule would likely involve sending agents at intervals to keep edge usage low. The total time horizon might be around k + L. In worst case, maybe T up to k * something? But we can bound T by, say, k * (n) maybe? Because if we delay each agent up to some time, we might need up to k * L? But let's think: Each agent takes at least shortest distance L_i from its start to node 1. The additional waiting time is at most some value where the cost of waiting (c per minute) outweighs the benefit of reducing congestion. Since congestion cost is bounded: max congestion cost if all go at once on an edge: dk^2. If we delay one agent by t minutes, we incur ct extra waiting cost, but we might reduce congestion cost by up to d*(something). So t shouldn't exceed something like (dk^2)/c. With max d=50,k=50,c=1, that gives 125000 minutes? That's huge. But can that be beneficial? Let's examine: Suppose all k agents need to traverse the same edge exactly once (and the rest of the path has no congestion). If we send them all at once, congestion cost for that edge minute = dk^2. If we send them one per minute, congestion cost = sum_{i=1 to k} d * 1^2 = dk, but waiting cost: the i-th agent (i starting from 1) would be delayed by (i-1) minutes relative to the first, so total extra waiting time = sum_{i=1 to k} (i-1) = k(k-1)/2. So total cost compared to sending all at once (where waiting is zero because no extra delay? But all start at time 0, they all travel simultaneously; arrival times for each would be the same L (the distance) if path length L, so total arrival cost = c * L per agent = cL*k. That is same regardless of staggering? Actually arrival time includes waiting; if we stagger, some agents wait longer, so their arrival time increases, so total arrival cost increases by sum of delays. So the trade-off is: sending all at once gives high congestion cost but no extra waiting; sending one per minute gives low congestion but extra waiting. The total cost difference:
- All at once: total cost = c * L * k + d * k^2 + (congestion on other edges maybe zero? we consider just this edge). Actually also congestion on other edges if multiple edges; but consider only one edge.
- Staggered (one per minute): total cost = c * L * k + c * (total delay) + d * k (since each minute one agent uses edge). total delay = 0 + 1 + 2 + ... + (k-1) = k(k-1)/2. So extra cost compared to base = c * k(k-1)/2 + dk. While all at once extra cost = dk^2. So staggering is better if ck(k-1)/2 + dk < dk^2 => ck(k-1)/2 < dk(k-1) => c/2 < d => if 2d > c. Actually check: dk^2 - dk = dk(k-1). So staggering is better if ck(k-1)/2 < dk(k-1) => c/2 < d => if d > c/2. For c=1,d=50, definitely better to stagger. The total waiting added can be up to k(k-1)/2. That could be up to 1225 for k=50. So T could be up to L + (k-1) which is about 50+49=99? Actually if we stagger one per minute, the last agent starts crossing the edge at minute (k-1) after first, then still need L-1 more steps to get to 1. So total horizon ~ L + k. That's not large.
But what if we need to send through multiple bottlenecks? Might require more spread. However the quadratic congestion cost is per edge per minute, so staggering across multiple edges might require larger delays. But we can also offset: you can schedule them to not overlap on multiple edges. For a path of length L, if you send agents spaced by enough to avoid congestion on all edges, you might need spacing at least 1 minute to avoid any overlap if they are on the same edge simultaneously. But if they are spaced by 1 minute, then on each edge, they will cross one per minute, so congestion cost is d per minute, total d * (k) for that edge over all minutes. That's minimal congestion. So the total waiting added for i-th agent is (i-1) minutes (if we start each at source with delay). Then total extra waiting cost = c * sum_{i=1 to k} (i-1) = c * k(k-1)/2. So horizon = shortest distance + (k-1). That's at most (n-1)+(k-1) ≤ 50+49=99. But is it always possible to achieve this spacing? Possibly yes if we start each agent at appropriate times and they all follow the same shortest path without interfering? But wait: If we space start times by 1 minute, then the first agent goes through edges, second starts 1 minute later, etc. Then on each edge, the first crosses at time t, second at t+1, etc. They will not be on the same edge at the same time as long as the spacing is at least 1? Actually if the edge traversal takes 1 minute, and agents are spaced by exactly 1 minute, then the first finishes crossing at time t+1, second starts crossing at time t+1? Let's simulate: Suppose the first agent arrives at node u (start of edge) at time t, then traverses edge u->v during minute [t, t+1] (or we consider discrete steps: at beginning of minute t they are at u, they decide to move to v, during minute t they travel, at beginning of minute t+1 they are at v). The second agent, if it arrives at node u at time t+1, then it can start traversing at minute t+1, which is the minute after first finished. So they are not on the edge simultaneously. So spacing of 1 is enough to avoid overlap. So indeed, with k agents all taking the same path, we can schedule them by starting the i-th agent at its source with delay i-1 (wait at source) and then all follow the same route without waiting later. Then the arrival time of i-th is (distance + i-1). So total horizon = distance + k-1.
But is that always optimal? Possibly not, because we might want to use different paths to reduce congestion. However that just gives an upper bound on optimal arrival times. Since costs are positive, we would never want to wait longer than necessary if it doesn't reduce congestion further. Because waiting only increases c per minute, while congestion cost cannot be negative. So the maximum sensible waiting time for any agent is at most (k-1) maybe plus some path length differences. But we might consider a scenario where agents start from different nodes, so staggering at source may not align path overlaps; but we could still have some agents wait at intermediate nodes to avoid congestion. However total waiting across all agents is limited by the fact that each waiting minute adds c per agent; total waiting cost can't exceed the maximum possible reduction in congestion cost. Maximum congestion cost saving: If we send all agents simultaneously, on each edge-direction we incur d*(x)^2 where x is number of agents using that edge-direction at that minute. If we spread them perfectly to avoid any overlap (i.e., at most 1 per edge per minute), total congestion cost becomes d * (total number of edge traversals across all agents). Because each edge traversal by an agent occurs exactly once, at some minute, and if never overlapped, cost = d1^2 per traversal. So the maximum saving per edge is from sum of squares to number of traversals (since sum of squares >= sum of ones). The maximum possible saving for an edge is d(sum x_i^2 - sum x_i) where x_i are counts per minute, but under the constraint that sum_i x_i = total traversals N (the number of agents using that edge). The minimal sum of squares given fixed sum N is achieved when the x_i are as balanced as possible, i.e., each x_i is either floor(N/T) or ceil(N/T) if we have T minutes. However we control T (the time horizon). Actually to minimize squares, you would spread as evenly as possible over time, but each minute you can have at most k, but you can use as many minutes as you like. The absolute minimum sum of squares for N traversals is when you use N minutes each with 1 traversal, yielding sum = N. That's the absolute minimum (since squares convex, distributing over more minutes reduces sum of squares). So the best you can do is d * N per edge. The maximum cost if you bunch them all in one minute is d * N^2. So maximum saving per edge = d*(N^2 - N). Sum over edges maybe large. But waiting cost to achieve such perfect spreading might be large: to spread N traversals over N minutes, you need to delay some traversals. The total delay across agents for that edge? Actually if you have N agents that need to cross an edge, you need to schedule them at distinct minutes. If they all arrive at the start node of the edge at the same time (or within a window), you must delay some before crossing. The total additional waiting time incurred (beyond the earliest possible crossing) depends on the arrival times to that node. If all N arrive at the node at time t (the earliest they could get there), to spread them across N minutes, you would have the first cross at t, second at t+1, ..., last at t+N-1. So total extra waiting = sum_{i=1}^{N} (i-1) = N(N-1)/2. This waiting time adds to the overall time each agent spends before reaching destination (since they wait at that node). So total extra waiting cost = c * N(N-1)/2. Compare to the saving: d*(N^2 - N). The net benefit = d*(N^2 - N) - c * N(N-1)/2 = N(N-1)*( d - c/2 ) roughly. So if d > c/2, it's beneficial to fully spread. If d <= c/2, it's better to bunch (maybe not fully bunch but some trade-off). This suggests that the optimal may involve some spreading, but not necessarily to the full extent if c is relatively large.
Thus the maximum waiting (delay) for any agent relative to earliest possible arrival might be up to (k-1) across the most congested edge. But if multiple edges need spreading, waiting could accumulate? However you can coordinate: if you have a path with multiple edges, you might need to spread at each edge. But if you space agents by 1 minute at the start, then they will naturally be spaced on all edges along the path, provided they all follow same path. So total delay for the i-th agent is i-1, which is at most k-1. So maximum T is distance + (k-1) ≤ (n-1)+(k-1) ≤ 98. But what about agents starting from different nodes and taking different paths? They may converge and cause congestion. Could there be need to delay more than k-1? Suppose we have two groups of agents coming from different directions merging onto a single edge. If they start at different times, you might need to delay some more to avoid congestion with the other group. But you could also offset start times. In worst case, you could have all k agents needing to use the same edge, but from different origins they may arrive at the edge at different times naturally due to different distances. However you can still schedule them by waiting at the source or along the path. The maximum extra delay for any agent beyond its earliest arrival time might be up to something like (k-1) + something due to different distances. But consider: agent A from far away might arrive at the bottleneck edge later than agent B from close by. If we want to avoid overlap, we might need to delay B to let A go first? But B could go earlier if it arrives earlier; we could schedule B early, then A later; that would not require B to be delayed beyond its earliest arrival; it might even need to wait if we want to avoid A overlapping? Actually if B arrives earlier, it can cross the edge earlier without conflict. If A arrives later, it can cross later; as long as we schedule them at distinct times, we need to ensure that if both want to cross at the same minute, we delay one. The maximum delay needed for any agent to resolve conflicts is at most (k-1) because there are only k agents; we can assign each a distinct time slot for crossing each bottleneck. But if there are multiple bottlenecks, an agent may be delayed at each bottleneck? However if we assign a global "start time offset" for each agent, and they follow a fixed route, then the spacing may be preserved along the path: if you start agents with offsets such that they are spaced by at least the edge traversal time (1 minute), they won't catch up because all have same travel times on edges. With different path lengths, though, offsets may cause them to arrive at bottlenecks at different relative times. But we can still design schedules. It seems plausible that an optimal schedule can be achieved with total makespan bounded by something like (max distance) + (k-1) * (some factor). Actually we can consider a time horizon T that is the maximum arrival time. Since we can always let all agents go as fast as possible (no waiting) and get max arrival time = max distance (if no waiting). That yields some cost. To improve, we might add waiting, but waiting only increases arrival times, so the max arrival time would not exceed max distance + something like k-1? Actually if we add waiting, max arrival time increases. So the optimal might have max arrival time <= max distance + (k-1) * L? Let's think: Could we ever want to delay an agent more than k-1 minutes? Suppose d is huge, c is tiny. Then we want to minimize congestion heavily. On a single edge, we would spread agents out completely, so the last agent's delay is k-1. But what if the path length L is greater than 1? The agent's total travel time = L + (delay at start) = L + (i-1). So max arrival = L + (k-1). So that's still <= (n-1)+(k-1).
But consider two edges in series: if we have a chain, and we want to avoid congestion on both edges, spacing at start by 1 ensures no overlap on either edge. So max arrival = L + (k-1). So it's fine.
What if we have a network where agents take different paths that converge and diverge, causing potential conflicts on multiple edges at different times? Still, with k agents, you could schedule them each to have a unique "time slot" across the whole schedule such that they never meet? Not necessarily; they might need to use the same edge at different times but also other edges. However we can assign each agent a "start time" offset and possibly different routes to avoid congestion. The worst-case max delay might be larger because an agent may need to wait at multiple points. But we could model it as a flow over time with bounded time horizon, and we can set T high enough to capture optimal solution. Since n,m,k ≤ 50, we can set T to some safe bound. The max arrival time cannot exceed something like (k * (n-1) * something) because we can always route each agent via simple path of length at most n-1, and we could delay them arbitrarily, but we wouldn't delay beyond some bound because cost would increase. We need to find an upper bound on T that is guaranteed to contain an optimal solution. Let's try to derive a bound.
We can treat the problem as a minimum cost flow over time with convex costs. Since costs are convex and increasing with time (waiting cost c per unit time), the optimal solution will have finite horizon. We can consider that any waiting beyond a certain point is wasteful because the cost of waiting (c per unit per agent) would exceed any possible saving from congestion. The maximum possible saving from congestion for an agent is bounded. For each edge, the maximum saving from not being on a congested edge is at most d*(k^2) (if it avoids being in a group of size k). But if an agent waits a lot, it might avoid many congested edges? However each edge contributes at most dk^2, but there are at most m edges (50). So total possible saving from congestion across all edges is at most m * d * k^2. That's at most 50502500 = 6,250,000? Actually k^2=2500, d=50 -> 125000 per edge, times 50 = 6,250,000. That's huge but finite. However waiting cost per minute per agent is c ≤ 50. If an agent waits T minutes, the added waiting cost is cT. For the agent to be beneficial, T must be less than (max_saving)/c ≈ (6,250,000)/50=125,000. That's huge, but maybe unrealistic because an agent cannot influence all edges; it only traverses a few edges (at most n-1). So saving per agent is at most (number of edges on its path) * d * (some reduction). Actually the agent's contribution to congestion cost on an edge is d*((x)^2 - (x-1)^2) = d*(2x-1) if it's one of x persons. So if the agent avoids being on that edge when there are many others, it saves at most d*(2k-1) per edge, but at most maybe 2dk. With at most n-1 edges, saving per agent per minute of delay? Actually waiting can shift its usage of edges to different times, potentially reducing congestion on those edges. The maximum total saving from having an agent wait could be bounded by sum over edges on its path of d(2k-1) ≈ 2dk * (n-1). For max values: d=50,k=50,n=50 -> 2505049 = 245,000. So waiting cost per minute is c=1 to 50, so maximum beneficial wait might be up to ~245,000/1 = 245,000. That's still huge. But is that realistic? The agent cannot save d*(2k-1) on an edge if by waiting it reduces the count on that edge from k to k-1; that saving is d*(k^2 - (k-1)^2)=d*(2k-1). That's at most 2dk. For each edge on its path, if it avoids being part of a big group, it could save up to that much. But if it waits, it might only shift its usage to a time when the count is smaller, but that may cause it to be part of a group later, maybe not. The maximum possible saving for a single agent across all edges is when it is the "extra" person causing each edge to have x people rather than x-1, and x is as large as possible (k). But if it waits to go alone, then on each edge it will be alone, saving d(x^2 - 1^2) for each edge? Actually if it originally was in a group of size x (including itself), by shifting to a different time where it is alone, the cost contribution becomes d1^2 instead of being part of a group where it contributed marginal cost d(x^2 - (x-1)^2) = d*(2x-1). However the total cost on that edge changes from d((x-1)^2 + 1^2) = d*(x^2 -2x+2). The saving is d*(2x-2) = 2d*(x-1). So it's about 2d*(x-1). If x = k, saving ~ 2d(k-1). So per edge max saving ~ 2dk? Actually 2d(n-1) with n-1=49, d=50, k=50 => 250T for the entire delay. If it delays by T, it saves at most that amount. So T must be ≤ saving / c ≈ 245,000 / 1 = 245,000. That's still large. However, we also need to consider that the agent's path length is at most 49, so earliest arrival time without waiting is at most 49. If we delay it by 245,000, its arrival time becomes huge, costing c * 245,000 = 245,000. The saving is also 245,000, so break-even. But could there be a scenario where it's optimal to delay that much? Possibly if d is huge relative to c, and k is large, and there are many edges where it would otherwise be part of a huge group. However, is it possible for a single agent to be part of a group of size k on many edges? If all k agents share the exact same path from their sources to 1, then each edge on that common path would have all k agents if they all go at the same time. By delaying one agent, you could reduce group sizes on all those edges. But if you delay only one agent, the other k-1 agents may still form large groups. The saving by delaying one agent on an edge used by all k agents is: originally cost d(k-1)^2. The delayed agent later goes alone, cost d(k-1)^2 + d(k^2 -2k +2). Saving = d*(2k-2). So per edge saving = 2d(k-1). With L common edges (<=49), total saving = 2d(k-1)L. Meanwhile, the delayed agent incurs extra waiting cost c * (extra time). To achieve that, the delayed agent must be scheduled after the group of k-1. The group of k-1 would take some time to clear the path. If we schedule them all at time 0 with no waiting, they will traverse each edge sequentially; the time they occupy edges: they will be on edge 1 at minute t1, edge 2 at t1+1, etc. The delayed agent could start after they have passed each edge? Actually to avoid overlapping, the delayed agent must not be on an edge at the same time as the group on that edge. If the group starts at time 0, they will occupy each edge for one minute (assuming they all go together). For edge 1, they are on it during minute 0-1. For edge 2, during minute 1-2, etc. So if the delayed agent starts after a delay of 1 minute, it might catch up on later edges? Let's think: Suppose the path has two edges: A->B->1. Group of k-1 start at time 0: at time 0 they move A->B, arriving B at time 1. At time 1 they move B->1, arriving at time 2. If delayed agent starts at time 1 from A: at time 1 moves A->B, arriving B at time 2. But at time 1, group is on B->1? Actually at time 1, group is at B and will move B->1 during minute 1-2. At time 1, delayed agent is moving A->B, so no conflict on same edge. At time 2, delayed agent arrives at B, while group has already arrived at 1 at time 2. So no conflict. So with delay of 1, the delayed agent avoids overlapping on both edges. That's because the group is exactly one step ahead. So the delayed agent only needs to be delayed by 1 minute to avoid conflict on all edges if they all move together. So extra waiting cost is c(batch size)^2 if we batch them (but batches are simultaneous processing? Actually they can cross simultaneously, but the cost is convex in batch size. So we can process multiple jobs in the same time slot (minute) but incur high cost. This is like we can choose to process jobs at any time >= release time, and we can process any number in a time slot, with cost = d*(number)^2 for that slot. The objective is to minimize sum of processing costs plus sum of completion times (since waiting cost is c per unit time per job from start? Actually waiting cost is c per minute for each agent from time 0 until completion (arrival at 1). But when focusing on a single edge, the waiting cost before that edge is part of the overall waiting. However we need to consider the whole network.
Given the small parameters, perhaps we can formulate as a min-cost flow on a time-expanded network with a reasonable time horizon T. Since n <= 50, k <= 50, we could set T to something like 200 or maybe 500, which would give at most 50200 = 10,000 nodes, which is manageable for min-cost flow with convex costs? But we also need to handle quadratic costs on edges per time, which we could convert to piecewise linear by splitting edges into multiple parallel arcs with increasing costs. However, the time-expanded network would have edges for waiting (cost c per time unit) and edges for movement (with congestion cost that depends on flow on that edge at that time). But the congestion cost is not linear; it's quadratic in the number of agents using the same edge-direction at the same time. However in a flow formulation, if we consider each edge at each time as a resource with capacity (unlimited) but cost that is a function of flow amount, we can model it with convex cost on arcs. Min-cost flow with convex costs can be solved by transforming each convex-cost arc into multiple arcs with linear costs (by piecewise linear approximation using integer capacities). Since the flow on each arc (movement edge at time t) is at most k (<=50), we can exactly represent the convex cost d * x^2 for integer x by having a set of unit-capacity arcs with costs d(i^2 - (i-1)^2) = d*(2i-1) for the i-th unit. That is standard: for an arc with convex cost f(x) = dx^2, we can have k parallel arcs each of capacity 1, with cost = marginal cost of adding another unit: f(i)-f(i-1) = 2i-1 times d? Actually f(x)=dx^2, so marginal cost for the i-th unit (i from 1 to k) is d*(i^2 - (i-1)^2) = d*(2i-1). So we can have arcs with that cost, and the min-cost flow will take them in order due to increasing costs, effectively achieving convex cost.
Thus we can build a time-expanded network from time 0 to T, with nodes (v,t) for v=1..n, t=0..T. We also need a sink for arrivals. Each agent starts at (a_i, 0) with supply 1. They need to end at node 1 at some time t (arrival). Actually after they reach node 1, they leave the system; we can model that by edges from (1,t) to a super sink with cost 0, capacity 1, and maybe also we need to account for arrival time cost c*t. Since we already account for waiting cost c per minute per agent for each time unit they are in the system, we can equivalently add cost c to each waiting edge and each movement edge? But careful: The waiting cost c per minute is incurred for each minute the agent is in the system before arriving. If we add cost c to each "time step" an agent spends (either waiting or moving), then total cost would be c * (total time spent) which is c * arrival_time, because each minute they are either moving or waiting. So we can add cost c to each arc that goes from time t to t+1 (i.e., each waiting edge and each movement edge). Because each minute spent adds c to the total cost.
Thus in the time-expanded network:
-
For each node v and time t from 0 to T-1, we have a waiting edge from (v,t) to (v, t+1) with cost c (since the agent waits one minute at v). Its capacity is infinite? Actually we can set capacity high (k). But we also have movement edges:
-
For each street (u,v) (bidirectional) and each time t from 0 to T-1, we have two directed edges: from (u,t) to (v,t+1) and from (v,t) to (u,t+1). The cost for using such an edge should be c (for the minute spent) plus the congestion cost contribution d * (something). However, the congestion cost is not per agent but depends on the total number using that edge-direction at that time. Since we are modeling each movement as flow on an arc, and the convex cost function will account for the squared term. But note: the congestion cost is d * (x)^2, where x is the number of agents walking that edge-direction at that time. This cost is incurred once per minute, not per agent. If we put a convex cost on the arc, the total cost contributed by flow x on that arc would be dx^2, which matches exactly. However we also have the per-minute cost c per agent: we need to add c to each agent's movement cost. So the total cost for arc with flow x should be c * x + d * x^2. Because each of the x agents incurs c for that minute, and collectively they incur dx^2. So the arc cost function is f(x) = c * x + d * x^2.
Similarly, for waiting arcs: from (v,t) to (v,t+1), if x agents wait at v at time t, then they each incur c for waiting, but there is no congestion cost for waiting. So cost should be c * x, linear. So waiting arcs have linear cost c per unit flow.
Thus we can model each movement arc (directed) with convex cost f(x)=cx + dx^2. Since it's convex (quadratic), we can split into unit-capacity arcs with marginal costs: for the i-th unit, marginal cost = f(i) - f(i-1) = c + d*(2i-1). Because f(i) = ci + di^2, f(i-1)= c*(i-1)+ d*(i-1)^2, difference = c + d*(i^2 - (i-1)^2) = c + d*(2i-1).
Thus we can replace each movement arc (for a given direction and time) with k parallel arcs, each capacity 1, with cost = c + d*(2i-1) for the i-th arc (i from 1 to k). Since the flow on that arc is integer and at most k, the total cost will be f(x). For waiting arcs, cost is linear c per unit, so we can have a single arc with capacity k and cost c (or also split into unit arcs with cost c each, but linear is fine; we can just set cost = c and capacity large; min-cost flow with linear costs can be handled directly). However we must be careful: In min-cost flow, we typically have linear costs per unit flow. But we can handle convex costs by splitting into multiple arcs with increasing linear costs. That's standard.
Now we need to also enforce that each agent eventually reaches node 1 by time T. We can create a super sink SINK. For each time t from 0 to T, we add an edge from (1,t) to SINK with cost 0 and capacity 1 per agent? Actually we need to allow multiple agents to arrive at the same time at node 1, but there is no limit; they just exit. So we can add an edge from (1,t) to SINK with infinite capacity, cost 0. However we also need to account for the arrival time cost? But we already accounted for c per minute spent, including the last minute's movement or waiting that brings them to node 1. When they move to (1,t+1) via an edge, that minute's cost includes c. When they arrive at (1,t+1), they are at node 1 at the beginning of minute t+1. Should we add an extra cost for reaching? The problem says "As soon as a person reaches the crossroad number 1, he or she immediately comes to the leader's house and attends the meeting." The discontent increases by c*x when a person reaches crossroad 1, where x is the number of minutes it took. So if they arrive at the beginning of minute t (i.e., after t minutes have passed), then x = t. But in our model, if an agent takes an action at time t (starting at t, moving/waiting, and then at time t+1 they are at node 1), they have spent t+1 minutes? Let's define time steps clearly.
We need to be consistent: At time 0, each person is at their starting crossroad. Then in each minute, they either wait or move. After performing an action for a minute, at the beginning of the next minute they are at a new crossroad (or same if waited). When they reach crossroad 1, they immediately attend. So if an agent moves to crossroad 1 during minute t (starting at crossroad u at beginning of minute t, moving along edge u-1, arriving at crossroad 1 at beginning of minute t+1), then the time taken is t+1 minutes? Let's test: Suppose an agent starts at node 1 at time 0: already at crossroad 1, so they attend immediately? In problem, they start at their home crossroad, which for members is a_i, and a_i ≥ 2, so not at 1. But for completeness: If an agent were at 1 at time 0, they would attend immediately with time 0? The problem says "As soon as a person reaches the crossroad number 1, he or she immediately comes to the leader's house and attends the meeting." So if at time 0 they are at 1, they attend immediately, time 0, discontent added c*0=0. In our discrete model, we consider that at time 0, they are at their starting node. So if they start at 1, we need to handle that. But all a_i ≥ 2, so no one starts at 1.
Now consider an agent that moves along a path: At time 0 at node u (≠1). They decide to move along edge to v. During minute 0, they travel, at beginning of minute 1 they are at v. If v=1, they have reached at time 1 (after 1 minute). So time taken = 1. That matches: they started at 0, after 1 minute they are at 1. If they wait first: at time 0, wait at u for one minute; at beginning of minute 1 still at u; then move to 1 during minute 1, arrive at beginning of minute 2; time taken = 2. So arrival time = the time index at which they first are at node 1 (the beginning of that minute). So if they are at (1, t) at the beginning of minute t, they have arrived.
Thus in our time-expanded network, we can define state (v, t) representing that the agent is at crossroad v at the beginning of minute t (before any action). Starting states: (a_i, 0). Actions: From (v,t), they can either:
- Wait: leads to (v, t+1). This consumes minute t, costs c (for that minute) and no congestion.
- Move along edge (v,w): leads to (w, t+1). This consumes minute t, costs c (per minute) plus congestion cost d*(number of agents using that edge-direction at that minute).
Thus total cost incurred at minute t is: for each agent, c added; for each edge-direction with x agents, add d*x^2.
Therefore, if we model flow through the network, each unit of flow corresponds to an agent. The total cost from movement arcs should reflect both per-agent c and congestion dx^2. As we described, movement arc from (v,t) to (w,t+1) with flow x incurs total cost = cx + dx^2. Waiting arc from (v,t) to (v,t+1) with flow x incurs cost = cx (since no congestion). That matches.
Now we need to connect to sink: when an agent reaches node 1 at time t, they should leave the system and not incur further costs. So we can add arcs from (1,t) to sink with cost 0, capacity infinite. However, we must ensure that agents cannot stay at node 1 beyond arrival? Actually once at node 1, they attend and stop. So we should not allow waiting at node 1 after arrival, because they would incur extra c cost incorrectly. But they could potentially wait at node 1 before leaving? But they would attend immediately, so they should not wait. So we should not have waiting arcs from (1,t) to (1,t+1) because they would not choose to wait (since they can leave with zero cost). In a min-cost flow, they will take the zero-cost sink edge as soon as possible because waiting costs c. So we can include waiting arcs at node 1 with cost c, but they wouldn't be used in optimal solution because leaving costs 0. To be safe, we can omit waiting arcs at node 1, but it's easier to include them but the min-cost flow will avoid them.
Now we need to set T. What is a sufficient time horizon? Agents must all eventually reach sink. Since waiting cost is positive, in an optimal solution no agent will have arrival time beyond some bound because we can always force them to go along a shortest path with no waiting, which takes at most D_max ≤ n-1 (since graph is connected). But waiting might be beneficial, increasing arrival time. However, waiting too much would add cost c per minute. The maximum additional waiting time an agent might incur is bounded by something. We need to find a T such that any optimal solution can be represented with all arrivals by T.
Given the earlier reasoning, it seems plausible that T = (n-1) + (k-1) is sufficient, maybe plus some slack. But is it always? Consider that agents might take different paths of different lengths. Suppose we have two groups: one from far away (distance L_max) and one from near (distance L_min). To reduce congestion on shared edges, we might delay the near group to let the far group go first, causing near group to wait up to (L_max - L_min) + (something). L_max - L_min ≤ n-1. So near group could be delayed by up to (n-1) to let far group go first, plus maybe additional to avoid conflicts with other near group agents? Actually if there are many near agents, they might be delayed further. But the maximum delay for any agent relative to its earliest arrival is at most (k-1) * (something?) Could be more if they need to wait at multiple points? But since we can schedule start delays, the total delay for an agent is just the difference between its actual start time (or its arrival times) and its earliest possible if it never waited. Since we control waiting at nodes, we can think of each agent having a "departure time" from its source (maybe waiting there) and then moving without further waiting. Is it always optimal to have waiting only at the source? Not necessarily; sometimes it might be beneficial to wait at intermediate nodes to avoid congestion later. For example, if two paths converge, you might want to have an agent from one path wait at the merging node to let agents from the other path pass. But that waiting could be replaced by waiting at the source instead, because the relative timing remains the same? Possibly not exactly, because the time to reach the merge point differs. If an agent waits at the source, its arrival at the merge point is delayed by the same amount. If it waits at the merge point, the effect is the same: it arrives later at the merge point. However, waiting at the source also delays its arrival at earlier edges, possibly causing less congestion there too. So waiting at source is at least as flexible as waiting later. So one might suspect that there exists an optimal solution where all waiting occurs at the sources. Is that true? Consider: agent A from source s has a path to 1. If we want A to arrive at a certain later