Luogu P1343 Earthquake Escape (the largest network board)

Topic link

Title description

When the Wenchuan earthquake occurred, Sichuan ** Middle School was in class. As soon as the earthquake occurred, the teachers immediately led x students to escape. The whole school can be abstracted as a directed graph with n points and m edges. Point 1 is the classroom, and point n is the safety zone. Each side can only accommodate a certain number of students. If the building is exceeded, it will collapse. Due to the large number of people, the principal decided to let the students escape in several batches, only the first batch After all the students have escaped, the second batch of students can escape from point 1. Now please help the principal to calculate the maximum number of students that can be transported in each batch, and x students can be transported in several batches.

Input format

The first line of 3 integers n, m, x (x<2^31, n<=200, m<=2000); the following m lines, each line of three integers a, b, c (a, b, c description An edge, respectively represents an edge from point a to point b, and can accommodate c students)

Output format

Two integers respectively indicate the maximum number of students that can be shipped in each batch, and the number of batches of x students can be shipped. If you cannot reach the destination (point n), output "Orz Ni Jinan Saint Cow!"

enter

6 7 7
1 2 1
1 4 2
2 3 1
4 5 1
4 3 1
3 6 2
5 6 1

Output

3 3

Code

#include<map>
#include<set>
#include<stack>
#include<queue>
#include<string>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int maxn=1e6+5;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int minn=0xc0c0c0c0;
bool flag,vis[maxn];
int a,b,c,n,m,x,s,t,ans,Min,pre[maxn],edge[500][500];
bool find()
{
    
    
	queue<int> que;
	memset(pre,-1,sizeof pre);
	memset(vis,false,sizeof vis);
	vis[s]=1;
	que.push(s);
	while(!que.empty())
	{
    
    
		int u=que.front();
		que.pop();
		for(int i=1;i<=n;i++)
		{
    
    
			if(!vis[i]&&edge[u][i]>0)
			{
    
    
				pre[i]=u;//记录路径 
				vis[i]=true;//防环 
				que.push(i);
				if(i==t)
					return true;
			}
		}
	}
	return false;
}
void dfs()
{
    
    
	if(!find())
		return ;//已无增广路,即ans已是最大流 
	Min=inf;
	for(int i=t;i!=s;i=pre[i])
		Min=min(Min,edge[pre[i]][i]);//寻找最短的残差路
	for(int i=t;i!=s;i=pre[i])
	{
    
    
		edge[pre[i]][i]-=Min;
		edge[i][pre[i]]+=Min;
	}//更新 
	ans+=Min;
	dfs();
}
int main()
{
    
    
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	flag=true;
	cin>>n>>m>>x;
	s=n;
	t=1;
	for(int i=1;i<=m;i++)
	{
    
    
		cin>>a>>b>>c;
		edge[b][a]+=c;
	}
	dfs();
	if(ans==0)
		cout<<"Orz Ni Jinan Saint Cow!"<<endl;
	else
		cout<<ans<<" "<<x/ans+(x%ans==0?0:1)<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/WTMDNM_/article/details/108778057