zoukankan      html  css  js  c++  java
  • 【bzoj1507】 JSOI2008—Blue Mary的旅行

    http://www.lydsy.com/JudgeOnline/problem.php?id=1570 (题目链接)

    题意

      给出$m$个航班,每天只能做一次飞机,有$T$人从起点到终点,问最晚到达的人最早什么时候到。

    Solution

      枚举答案分层建图最大流判断即可。之前的流量不要清空。

    细节

      ?

    代码

    // bzoj1570
    #include<algorithm>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #include<queue>
    #define LL long long
    #define inf (1ll<<30)
    #define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout)
    using namespace std;
    
    const int maxn=1000010;
    int head[maxn],n,m,T,et,ans,cnt=1;
    struct data {int u,v,w;}t[maxn];
    struct edge {int to,next,w;}e[maxn];
    
    void link(int u,int v,int w) {
    	e[++cnt]=(edge){v,head[u],w};head[u]=cnt;
    	e[++cnt]=(edge){u,head[v],0};head[v]=cnt;
    }
    
    namespace Dinic {
    	int d[maxn],s,t;
    	bool bfs(int s,int t) {
    		memset(d,-1,sizeof(d));
    		queue<int> q;q.push(s);d[s]=0;
    		while (!q.empty()) {
    			int x=q.front();q.pop();
    			for (int i=head[x];i;i=e[i].next)
    				if (e[i].w && d[e[i].to]<0) d[e[i].to]=d[x]+1,q.push(e[i].to);
    		}
    		return d[t]>0;
    	}
    	int dfs(int x,int f) {
    		if (x==t || f==0) return f;
    		int w,used=0;
    		for (int i=head[x];i;i=e[i].next) if (e[i].w && d[e[i].to]==d[x]+1) {
    				w=dfs(e[i].to,min(e[i].w,f-used));
    				used+=w;e[i].w-=w;e[i^1].w+=w;
    				if (used==f) return used;
    			}
    		if (!used) d[x]=-1;
    		return used;
    	}
    	int main(int x,int y) {
    		s=x,t=y;int flow=0;
    		while (bfs(x,y)) flow+=dfs(x,inf);
    		return flow;
    	}
    }
    
    int main() {
    	scanf("%d%d%d",&n,&m,&T);
    	for (int i=1;i<=m;i++) scanf("%d%d%d",&t[i].u,&t[i].v,&t[i].w);
    	link(0,1,T);et=1000000;
    	for (int i=0;1;i++) {
    		for (int j=1;j<=n;j++) link(i*n+j,(i+1)*n+j,inf);
    		for (int j=1;j<=m;j++) link(i*n+t[j].u,(i+1)*n+t[j].v,t[j].w);
    		link((i+1)*n,et,inf);
    		ans+=Dinic::main(0,et);
    		if (ans==T) {printf("%d",i);break;}
    	}
    	return 0;
    }
    
  • 相关阅读:
    PyCharm小技巧
    How to install torcs package in Debian
    QT4.8.6静态编译
    Debian初识(选择最佳镜像发布站点加入source.list文件)
    Dev-C++ 小问题锦集
    ubuntu 12.04lts 安装mysql ,并通过QT连接
    win7下安装ubuntu14.04lts 双系统
    cmake打印变量值
    驾车常识:小轿车灯光
    汽车点火开关的功能介绍
  • 原文地址:https://www.cnblogs.com/MashiroSky/p/6616682.html
Copyright © 2011-2022 走看看