BZOJ 4853 [Jsoi2016]飞机调度

题解:

我严重怀疑语文水平(自己的和出题人的)

把航线按照拓扑关系建立DAG

然后最小路径覆盖

为什么两条首尾相接航线之间不用维护????

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=2009;
const int oo=100000000;

int n,m;
int w[maxn];
int d[maxn][maxn];//d[x][y]从x到y,w[x]w[y]不算的最短路 
int g[maxn][maxn];

int rs[maxn],rt[maxn],rb[maxn];

struct Edge{
    int from,to,cap,flow;
};
vector<int>G[maxn];
vector<Edge>edges;
void Addedge(int x,int y,int z){
    Edge e;
    e.from=x;e.to=y;e.cap=z;e.flow=0;
    edges.push_back(e);
    e.from=y;e.to=x;e.cap=0;e.flow=0;
    edges.push_back(e);
    int c=edges.size();
    G[x].push_back(c-2);
    G[y].push_back(c-1);
}

int s,t;
int vis[maxn];
int dd[maxn];
queue<int>q;
int Bfs(){
    memset(vis,0,sizeof(vis));
    vis[s]=1;dd[s]=0;q.push(s);
    while(!q.empty()){
        int x=q.front();q.pop();
        for(int i=0;i<G[x].size();++i){
            Edge e=edges[G[x][i]];
            if((e.cap>e.flow)&&(!vis[e.to])){
                vis[e.to]=1;
                dd[e.to]=dd[x]+1;
                q.push(e.to);
            }
        }
    }
    return vis[t];
}

int cur[maxn];
int Dfs(int x,int a){
    if((x==t)||(a==0))return a;
    
    int nowflow=0,f=0;
    for(int i=cur[x];i<G[x].size();++i){
        cur[x]=i;
        Edge e=edges[G[x][i]];
        if((dd[x]+1==dd[e.to])&&((f=Dfs(e.to,min(a,e.cap-e.flow)))>0)){
            nowflow+=f;
            a-=f;
            edges[G[x][i]].flow+=f;
            edges[G[x][i]^1].flow-=f;
            if(a==0)break;
        }
    }
    return nowflow;
}

int Maxflow(){
    int flow=0;
    while(Bfs()){
        memset(cur,0,sizeof(cur));
        flow+=Dfs(s,oo);
    }
    return flow;
}

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;++i)scanf("%d",&w[i]);
    for(int i=1;i<=n;++i){
        for(int j=1;j<=n;++j){
            scanf("%d",&d[i][j]);
            g[i][j]=d[i][j];
        }
    }
    
    for(int k=1;k<=n;++k){
        for(int i=1;i<=n;++i){
            for(int j=1;j<=n;++j){
                if(d[i][k]+w[k]+d[k][j]<d[i][j]){
                    d[i][j]=d[i][k]+w[k]+d[k][j];
                }
            }
        }
    }

    for(int i=1;i<=m;++i){
        scanf("%d%d%d",&rs[i],&rt[i],&rb[i]);
    }
    
    s=m+m+1;t=s+1;
    for(int i=1;i<=m;++i)Addedge(s,i,1);
    for(int i=1;i<=m;++i)Addedge(i+m,t,1);
    for(int i=1;i<=m;++i){
        for(int j=1;j<=m;++j){
            if(i==j)continue;
//            cout<<i<<' '<<j<<' '<<w[rs[j]]<<' '<<d[rt[i]][rs[j]]+w[rs[j]]<<endl;
            if(rb[i]+g[rs[i]][rt[i]]+w[rt[i]]+((rt[i]!=rs[j])?d[rt[i]][rs[j]]+w[rs[j]]:0)<=rb[j]){
                Addedge(i,m+j,1);
            }
        }
    }
    cout<<m-Maxflow()<<endl;
    return 0;
}

 

posted @ 2018-03-25 23:28  ws_zzy  阅读(379)  评论(0编辑  收藏  举报