【bzoj1507】 JSOI2008—Blue Mary的旅行

來源:互聯網
上載者:User

標籤:int   push   head   out   bre   while   div   oid   ace   

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;}

 

【bzoj1507】 JSOI2008—Blue Mary的旅行

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.