题目链接:
POJ 2060 Taxi Cab Scheme
分析:
最小路径覆盖=顶点数-最大匹配数。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAX_N=510;int T,n,total;
int st[MAX_N],end[MAX_N],vis[MAX_N],match[MAX_N],head[MAX_N];
int a[MAX_N],b[MAX_N],c[MAX_N],d[MAX_N];struct Edge{int to,next;Edge () {}Edge(int to,int next) : to(to),next(next) {}
}edge[MAX_N*MAX_N];inline void AddEdge(int from,int to)
{edge[total].to=to;edge[total].next=head[from];head[from]=total++;
}inline bool dfs(int u)
{for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;if(!vis[v]){vis[v]=1;if(match[v]==-1 || dfs(match[v])){match[v]=u;return true;}}}return false;
}inline void solve()
{memset(match,-1,sizeof(match));int res=0;for(int i=1;i<=n;i++){memset(vis,0,sizeof(vis));if(dfs(i)){res++;}}printf("%d\n",n-res);
}int main()
{scanf("%d",&T);while(T--){scanf("%d",&n);for(int i=1;i<=n;i++){char s[10];scanf("%s%d%d%d%d",s,&a[i],&b[i],&c[i],&d[i]);st[i]=(s[1]-'0')*60;st[i]+=(s[0]-'0')*600;st[i]+=s[4]-'0';st[i]+=(s[3]-'0')*10;end[i]=st[i]+abs(a[i]-c[i])+abs(b[i]-d[i]);}memset(head,-1,sizeof(head));total=0;for(int i=1;i<=n;i++){for(int j=i+1;j<=n;j++){if(end[i]+abs(c[i]-a[j])+abs(d[i]-b[j])<st[j]){AddEdge(i,j);}}}solve();}return 0;
}