poj 2060(最小路径覆盖)

本文讨论了城市出租车调度问题,通过最小路径覆盖算法优化出租车资源分配,确保高效完成预定行程。采用网格模型简化城市布局,计算不同行程间的最短路径,实现合理调度,减少所需车辆数量。
Taxi Cab Scheme
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 4207 Accepted: 1750

Description

Running a taxi station is not all that simple. Apart from the obvious demand for a centralised coordination of the cabs in order to pick up the customers calling to get a cab as soon as possible,there is also a need to schedule all the taxi rides which have been booked in advance.Given a list of all booked taxi rides for the next day, you want to minimise the number of cabs needed to carry out all of the rides. 
For the sake of simplicity, we model a city as a rectangular grid. An address in the city is denoted by two integers: the street and avenue number. The time needed to get from the address a, b to c, d by taxi is |a - c| + |b - d| minutes. A cab may carry out a booked ride if it is its first ride of the day, or if it can get to the source address of the new ride from its latest,at least one minute before the new ride's scheduled departure. Note that some rides may end after midnight.

Input

On the first line of the input is a single positive integer N, telling the number of test scenarios to follow. Each scenario begins with a line containing an integer M, 0 < M < 500, being the number of booked taxi rides. The following M lines contain the rides. Each ride is described by a departure time on the format hh:mm (ranging from 00:00 to 23:59), two integers a b that are the coordinates of the source address and two integers c d that are the coordinates of the destination address. All coordinates are at least 0 and strictly smaller than 200. The booked rides in each scenario are sorted in order of increasing departure time.

Output

For each scenario, output one line containing the minimum number of cabs required to carry out all the booked taxi rides.

Sample Input

2
2
08:00 10 11 9 16
08:07 9 16 10 11
2
08:00 10 11 9 16
08:06 9 16 10 11

Sample Output

1
2

Source

分析:跟上题类似,此题也是最小路径覆盖,将每个客人的需求分为开始和结束两点,增加源和汇,将源与每个要求的结束点连上容量为1 的边,每个开始点与汇连上容量为1 的边,然后枚举结束点i,和开始点j,如果i能够赶到j的话,就连上一条边,然后答案为m-最大流。。。这题用二分图应该会快些吧~~~
代码:
#include<cstdio>
using namespace std;
const int mm=555555;
const int mn=1111;
const int oo=1000000000;
int node,src,dest,edge;
int ver[mm],flow[mm],next[mm];
int head[mn],work[mn],dis[mn],q[mn];
int sx[mn],sy[mn],ex[mn],ey[mn],st[mn],et[mn];
inline int min(int a,int b)
{
    return a<b?a:b;
}
inline void prepare(int _node,int _src,int _dest)
{
    node=_node,src=_src,dest=_dest;
    for(int i=0;i<node;++i)head[i]=-1;
    edge=0;
}
inline void addedge(int u,int v,int c)
{
    ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;
    ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}
bool Dinic_bfs()
{
    int i,u,v,l,r=0;
    for(i=0;i<node;++i)dis[i]=-1;
    dis[q[r++]=src]=0;
    for(l=0;l<r;++l)
        for(i=head[u=q[l]];i>=0;i=next[i])
            if(flow[i]&&dis[v=ver[i]]<0)
            {
                dis[q[r++]=v]=dis[u]+1;
                if(v==dest)return 1;
            }
    return 0;
}
int Dinic_dfs(int u,int exp)
{
    if(u==dest)return exp;
    for(int &i=work[u],v,tmp;i>=0;i=next[i])
        if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
        {
            flow[i]-=tmp;
            flow[i^1]+=tmp;
            return tmp;
        }
    return 0;
}
int Dinic_flow()
{
    int i,ret=0,delta;
    while(Dinic_bfs())
    {
        for(i=0;i<node;++i)work[i]=head[i];
        while(delta=Dinic_dfs(src,oo))ret+=delta;
    }
    return ret;
}
inline int get(int a)
{
    return a<0?-a:a;
}
int main()
{
    int m,s,i,j,n,t;
    char c;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        prepare(n+n+2,0,n+n+1);
        for(i=1;i<=n;++i)
        {
            scanf("%d%c%d%d%d%d%d",&m,&c,&s,&sx[i],&sy[i],&ex[i],&ey[i]);
            et[i]=(st[i]=m*60+s)+get(sx[i]-ex[i])+get(sy[i]-ey[i]);
        }
        for(i=1;i<=n;++i)
        {
            addedge(src,i,1),addedge(i+n,dest,1);
            for(j=i+1;j<=n;++j)
                if(et[i]+get(ex[i]-sx[j])+get(ey[i]-sy[j])<st[j])addedge(i,n+j,1);
        }
        printf("%d\n",n-Dinic_flow());
    }
    return 0;
}


### 最小路径覆盖问题及其解决方案 最小路径覆盖问题是图论中一个经典的问题,尤其在有向无环图(DAG)中有着广泛的应用。该问题的目标是找到图中尽可能少的路径,使得这些路径能够覆盖所有顶点,并且每个顶点恰好出现在一条路径中。 #### 问题定义 对于给定的有向图 $ G = (V, E) $,设 $ P $ 是 $ G $ 的一个简单路径集合。如果 $ V $ 中的每个顶点恰好在 $ P $ 的一条路径上,则称 $ P $ 是 $ G $ 的一个路径覆盖。目标是找到路径数最少的路径覆盖,即最小路径覆盖。 #### 解决方案:网络流模型 解决最小路径覆盖问题的经典方法是将其转化为最大匹配问题,进而通过网络流算法求解。 ##### DAG中的最小路径覆盖 对于有向无环图(DAG),可以通过构造二分图并求其最大匹配来解决最小路径覆盖问题。具体步骤如下: 1. **构建二分图**: - 将原图中的每个顶点 $ v $ 拆分为两个部分:$ v_{in} $ 和 $ v_{out} $。 - 左侧集合为 $ \{v_{in} | v \in V\} $,右侧集合为 $ \{v_{out} | v \in V\} $。 - 对于每条边 $ (u, v) \in E $,在二分图中添加一条从 $ u_{in} $ 到 $ v_{out} $ 的边。 2. **求最大匹配**: - 在构造的二分图中求最大匹配。最大匹配的大小记为 $ M $。 3. **计算最小路径覆盖**: - 最小路径覆盖的大小等于顶点数减去最大匹配的大小,即 $ |V| - M $ [^4]。 ##### 示例代码 以下是一个基于匈牙利算法实现的最大匹配求解代码片段,用于计算 DAG 的最小路径覆盖: ```cpp #include <cstdio> #include <cstring> using namespace std; #define N 150 int lover[N]; // 记录匹配关系 bool vis[N]; // 标记访问状态 int e[N][N]; // 邻接矩阵表示二分图 bool find(int x, int n) { for (int i = 1; i <= n; i++) { if (e[x][i] && !vis[i]) { vis[i] = true; if (!lover[i] || find(lover[i], n)) { lover[i] = x; return true; } } } return false; } int max_matching(int n) { int ans = 0; memset(lover, 0, sizeof(lover)); for (int i = 1; i <= n; i++) { memset(vis, 0, sizeof(vis)); if (find(i, n)) ans++; } return ans; } int main() { int T; scanf("%d", &T); while (T--) { int n, m; scanf("%d %d", &n, &m); memset(e, 0, sizeof(e)); for (int i = 1; i <= m; i++) { int a, b; scanf("%d %d", &a, &b); e[a][b] = 1; // 构造二分图 } int match = max_matching(n); printf("最小路径覆盖数: %d\n", n - match); } return 0; } ``` #### 可相交的最小路径覆盖 在某些情况下,路径可以相交。例如,在 POJ 2594 Treasure Exploration 问题中,允许路径在顶点上重叠。这种情况下,可以通过先对图进行传递闭包处理,再使用上述方法求解最小路径覆盖 [^3]。 #### 应用场景 - **任务调度**:将任务之间的依赖关系建模为有向图,寻找最少的任务序列以覆盖所有任务。 - **软件工程**:在模块调用图中,确定最少的测试路径覆盖所有模块。 - **交通规划**:设计最少的路线以覆盖所有地点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值