[bzoj4853][Jsoi2016]飞机调度【最短路】【网络流】

【题目链接】
  https://www.lydsy.com/JudgeOnline/problem.php?id=4853
【题解】
  用Floyd求出飞机从一个点到另一个地点的最短耗时。
  将需要的线路看做点,若一条线路能到其他的线路,则向该线路连边。
  问题转化成了最少路径覆盖问题。
  

/* --------------
    user Vanisher
    problem bzoj-4853 
----------------*/
# include <bits/stdc++.h>
# define    ll      long long
# define    inf     0x3f3f3f3f
# define    NN      510
# define    M       1000010
# define    N       2010
using namespace std;
int read(){
    int tmp=0, fh=1; char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') fh=-1; ch=getchar();}
    while (ch>='0'&&ch<='9'){tmp=tmp*10+ch-'0'; ch=getchar();}
    return tmp*fh;
}
int dis[NN][NN],n,m,w[NN],x[NN],y[NN],t[NN],in[NN],out[NN],S,T,id,mp[NN][NN];
struct node{
    int data,next,vote,re,l;
}e[M];
int dist[N],q[N],place,head[N],now[N];
void build(int u, int v, int l){
    e[++place].data=v; e[place].next=head[u]; head[u]=place; e[place].l=l; e[place].re=place+1;
    e[++place].data=u; e[place].next=head[v]; head[v]=place; e[place].l=0; e[place].re=place-1;
}
void bfs(int S, int T){
    memset(dist,inf,sizeof(dist));
    dist[S]=0; int pl=1,pr=1; q[1]=0;
    while (pl<=pr){
        int x=q[pl++];
        for (int start=head[x]; start!=0; start=e[start].next)
            if (e[start].l>0&&dist[e[start].data]==inf){
                dist[e[start].data]=dist[x]+1;
                q[++pr]=e[start].data;
            }
    } 
}
int dfs(int x, int T, int flow){
    if (x==T) return flow; int sum=0;
    for (int start=now[x]; start!=0; start=e[start].next){
        if (e[start].l>0&&dist[e[start].data]==dist[x]+1){
            int l=dfs(e[start].data,T,min(e[start].l,flow));
            sum+=l; flow-=l;
            e[start].l-=l; e[e[start].re].l+=l;
            if (flow==0){
                now[x]=start;
                return sum;
            }
        }
    }
    now[x]=0; return sum;
}
int dinic(){
    int sum=0;
    for (bfs(S,T); dist[T]!=inf; bfs(S,T)){
        memcpy(now,head,sizeof(now));
        sum+=dfs(S,T,inf);
    }
    return sum;
} 
int main(){
    n=read(), m=read();
    for (int i=1; i<=n; i++)
        w[i]=read();
    for (int i=1; i<=n; i++)
        for (int j=1; j<=n; j++){
            dis[i][j]=mp[i][j]=read();
            if (i!=j) dis[i][j]+=w[j];
        }
    for (int k=1; k<=n; k++)
        for (int i=1; i<=n; i++)
            for (int j=1; j<=n; j++)
                if (i!=j&&i!=k&&j!=k)
                    dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
    S=0, T=1, id=1;
    for (int i=1; i<=m; i++){
        x[i]=read(), y[i]=read(), t[i]=read();
        in[i]=++id, out[i]=++id;
        build(S,in[i],1); build(out[i],1,T);
    }
    for (int i=1; i<=m; i++)    
        for (int j=1; j<=m; j++){
            if (i==j) continue;
            if (t[i]+dis[y[i]][x[j]]+w[y[i]]+mp[x[i]][y[i]]<=t[j]) build(in[i],out[j],1);
        }
    printf("%d\n",m-dinic());   
    return 0;
}
posted @ 2018-04-14 19:42  Vanisher  阅读(143)  评论(0编辑  收藏  举报