●洛谷 1343 地震逃生
●题链:
https://www.luogu.org/problem/lists?name=1343
●题解:
最大流裸题,dinic实现。
加了个当前弧优化。
代码:
#include<queue> #include<cstdio> #include<cstring> #include<iostream> #define MAXN 500 #define INF 0x3f3f3f3f using namespace std; struct Edge{ int to[MAXN*10],cap[MAXN*10],nxt[MAXN*10],head[MAXN],ent; void Init(){ ent=2; memset(head,0,sizeof(head)); } void Adde(int u,int v,int w){ to[ent]=v; cap[ent]=w; nxt[ent]=head[u]; head[u]=ent++; } int Next(bool type,int i){ return type?head[i]:nxt[i]; } }E; int N,M,X; int d[MAXN],cur[MAXN]; bool bfs(){ memset(d,0,sizeof(d)); queue<int>q; q.push(1); d[1]=1; while(!q.empty()){ int u=q.front(); q.pop(); for(int i=E.Next(1,u);i;i=E.Next(0,i)){ int v=E.to[i]; if(d[v]||!E.cap[i]) continue; d[v]=d[u]+1; q.push(v); } } return d[N]; } int dfs(int u,int reflow){ if(!reflow||u==N) return reflow; int flowout=0,f; for(int &i=cur[u];i;i=E.Next(0,i)){ int v=E.to[i]; if(d[v]!=d[u]+1) continue; f=dfs(v,min(reflow,E.cap[i])); reflow-=f; E.cap[i]-=f; flowout+=f; E.cap[i]+=f; if(!reflow) break; } if(!flowout) d[u]=0; return flowout; } int Dinic(){ int flow=0; while(bfs()){ memcpy(cur,E.head,sizeof(E.head)); flow+=dfs(1,INF); } return flow; } int main() { scanf("%d%d%d",&N,&M,&X); E.Init(); for(int i=1,a,b,c;i<=M;i++){ scanf("%d%d%d",&a,&b,&c); E.Adde(a,b,c); E.Adde(b,a,0); } int ans=Dinic(); ans?printf("%d %d",ans,(X-1)/ans+1): printf("Orz Ni Jinan Saint Cow!"); return 0; }
Do not go gentle into that good night.
Rage, rage against the dying of the light.
————Dylan Thomas