HDU 1350 最小不相交路径覆盖 匈牙利算法

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define INF 0x3f3f3f3f
using namespace std;
const int maxe=300000;
const int maxv=550;
int cnt;
int head[maxv],link[maxv];
bool visit[maxv];
struct Edge {
    int to,next;
} edge[maxe];
struct Node {
    int time_start,time_cost,time,x_start,x_end,y_start,y_end;
} node[maxv];
void init(){
    cnt=0;
    memset(head,-1,sizeof head);
    memset(link,-1,sizeof link);
}
void addedge(int from,int to){
    edge[cnt].to=to;
    edge[cnt].next=head[from];
    head[from]=cnt++;
}
bool ok(Node a,Node b){
    int dis=abs(a.x_end-b.x_start)+abs(a.y_end-b.y_start);
    int t=a.time_start+a.time_cost+dis+1;
    return t<=b.time_start;
}
bool dfs(int u){//匈牙利算法
    for(int i=head[u];i!=-1;i=edge[i].next){
        int v=edge[i].to;
        if(visit[v])
            continue;
        visit[v]=true;
        if(link[v]==-1||dfs(link[v])){
            link[v]=u;
            return true;
        }
    }
    return false;
}
int main(void) {
#ifndef ONLINE_JUDGE
    freopen("E:\\input.txt","r",stdin);
#endif // ONLINE_JUDGE
    int T,h,m,n;
    cin>>T;
    while(T--) {
        cin>>n;
        for(int i=1; i<=n; i++) {
            scanf("%d:%d%d%d%d%d",&h,&m,&node[i].x_start,&node[i].y_start,&node[i].x_end,&node[i].y_end);
            node[i].time_start=h*60+m;
            node[i].time_cost=abs(node[i].x_start - node[i].x_end) + abs(node[i].y_start - node[i].y_end);
        }
        init();
        for(int i=1;i<n;i++)
            for(int j=i+1;j<=n;j++)
                if(ok(node[i],node[j]))
                    addedge(i,j);
        int ans=0;
        for(int i=1;i<=n;i++){
            memset(visit,0,sizeof visit);
            ans+=dfs(i);
        }
        printf("%d\n",n-ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/shadandeajian/article/details/81369203