[Luogu1343]地震逃生 最大流
题目链接:https://www.luogu.org/problem/show?pid=1343
dinic跑最大流。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int INF=1<<30; 6 int inline readint(){ 7 // int Num;char ch; 8 // while((ch=getchar())<'0'||ch>'9');Num=ch-'0'; 9 // while((ch=getchar())>='0'&&ch<='9') Num=Num*10+ch-'0'; 10 // return Num; 11 int Num; 12 scanf("%d",&Num); 13 return Num; 14 } 15 int N,M,X; 16 int to[4010],ne[4010],c[4010],fir[210],cnt=0; 17 void Add(int x,int y,int z){ 18 to[cnt]=y,ne[cnt]=fir[x],c[cnt]=z,fir[x]=cnt++; 19 to[cnt]=x,ne[cnt]=fir[y],c[cnt]=0,fir[y]=cnt++; 20 } 21 int S,T; 22 int bfn[210],ti=0; 23 int cur[210],q[210],dep[210]; 24 bool Bfs(){ 25 int head=1,tail=1; 26 q[1]=S; 27 bfn[S]=++ti; 28 dep[S]=0; 29 int u; 30 while(head<=tail){ 31 u=q[head++]; 32 for(int i=fir[u];i!=-1;i=ne[i]){ 33 int v=to[i]; 34 if(bfn[v]!=bfn[u]&&c[i]){ 35 bfn[v]=bfn[u]; 36 dep[v]=dep[u]+1; 37 q[++tail]=v; 38 } 39 } 40 } 41 return bfn[T]==bfn[S]; 42 } 43 int Dfs(int u,int mxf){ 44 if(!mxf||u==T) return mxf; 45 int flow=0,f; 46 for(int &i=cur[u];i!=-1;i=ne[i]){ 47 int v=to[i]; 48 if(dep[v]==dep[u]+1&&(f=Dfs(v,min(mxf,c[i])))>0){ 49 c[i]-=f; 50 c[i^1]+=f; 51 flow+=f; 52 mxf-=f; 53 if(!mxf) break; 54 } 55 } 56 return flow; 57 } 58 int Dinic(){ 59 int ret=0; 60 while(Bfs()){ 61 memcpy(cur,fir,sizeof(fir)); 62 ret+=Dfs(S,INF); 63 } 64 return ret; 65 } 66 int main(){ 67 N=readint(); 68 M=readint(); 69 X=readint(); 70 S=1; 71 T=N; 72 memset(fir,-1,sizeof(fir)); 73 for(int i=1;i<=M;i++){ 74 int a=readint(), 75 b=readint(), 76 c=readint(); 77 Add(a,b,c); 78 } 79 int mx=Dinic(); 80 if(!mx){ 81 puts("Orz Ni Jinan Saint Cow!"); 82 return 0; 83 } 84 printf("%d %d\n",mx,X/mx+(X%mx?1:0)); 85 return 0; 86 }