【BZOJ】【今日刷的网络流合集】【P2768】【P1934】【P3130】【P1070】【#2】

传送门:

http://www.lydsy.com/JudgeOnline/problem.php?id=2768

http://www.lydsy.com/JudgeOnline/problem.php?id=1934

http://www.lydsy.com/JudgeOnline/problem.php?id=3130

http://www.lydsy.com/JudgeOnline/problem.php?id=1070

最近不愿意写题解……看看代码吧

2768  1934 一模一样

Code:

/*
	ID:zky
	OJ:BZOJ
	Index:2768
	Language:C++
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int w[301][301];
int a[301];
int n,m;
int vis[301];
int Link[302];
bool find(int x){
	for(int i=1;i<=n;i++){
		if(w[x][i]&&!vis[i]){
			vis[i]=1;
			if(!Link[i]||find(Link[i])){
				Link[i]=x;
				return true;
			}
		}
	}
	return false;
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=m;i++){
		int Alice,Bob;
		cin>>Alice>>Bob;
		if(a[Alice]^a[Bob])w[Alice][Bob]=w[Bob][Alice]=1;
	}
	int ans=0;
	for(int i=1;i<=n;i++){
		memset(vis,0,sizeof(vis));
		if(find(i))ans++;
	}
	cout<<ans/2<<endl;
	return 0;
}
 

/*
	ID:zky
	OJ:BZOJ
	Index:3130
	Language:C++
*/
#include<vector>
#include<queue> 
#include<iomanip>
#include<cstdio>
#include<climits>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#define read(x) scanf("%d",&x)
using namespace std;
int n,m,p;
const int INF=INT_MAX;
struct edge{
	int u,v;
	double cap,flow;
	edge(int _u,int _v,double _cap):
		u(_u),v(_v),cap(_cap){flow=0;}
};
vector<edge>edges;
vector<vector<int> >G;
void add(int u,int v,double cap){
	edges.push_back(edge(u,v,cap));
	G[u].push_back(edges.size()-1);
}
int d[101];
int vis[101];
int cur[101];
double l=0,r=0,mid=INT_MAX,maxx;
bool bfs(){
	memset(vis,0,sizeof(vis));
	queue<int>q;
	q.push(1);
	d[1]=0;
	vis[1]=1;
	while(!q.empty()){
		int u=q.front();q.pop();
		for(int i=0;i<G[u].size();i++){
			edge &e=edges[G[u][i]];
			if(min(mid,e.cap)>e.flow&&!vis[e.v]){
				d[e.v]=d[u]+1;
				vis[e.v]=1;	
				q.push(e.v);
			}
		}
	}
	return vis[n];
}
const double eps=1e-6;
double dfs(int x,double a){
	if(x==n||fabs(a)<eps)return a;
	double flow=0,f;
	for(int &i=cur[x];i<G[x].size();i++){
		edge &e=edges[G[x][i]];
		if(d[e.v]==d[x]+1&&(f=dfs(e.v,min(a,min(mid,e.cap)-e.flow)))>0){
			e.flow+=f;
			edges[G[x][i]^1].flow-=f;
			flow+=f;
			a-=f;
			if(a==0)break;
		}
	}
	return flow;
}
double Dinic(){
	double flow=0;
	while(bfs()){
		double x=0;
		memset(cur,0,sizeof(cur));
		while(x=dfs(1,INF)){
			flow+=x;
			memset(cur,0,sizeof(cur));
		}
	}
	return flow;
}

bool ok(){
	for(int i=0;i<edges.size();i++)
	edges[i].flow=0;
	if(fabs(Dinic()-maxx)<eps)return true;
	return false;
}
int main(){
	read(n);read(m);read(p);
	G.resize(n+10);
	while(m--){
		int u,v,cap;
		read(u);read(v);read(cap);
		add(u,v,cap);
		add(v,u,0);
		r=max(r,(double)cap);
	}
	r++;
	maxx=Dinic();
	cout<<fixed<<setprecision(0)<<maxx<<endl;
	while(fabs(l-r)>eps){
		mid=(l+r)/2.0;
		if(ok())
			r=mid;
		else
		 	l=mid;
	}

	cout<<fixed<<setprecision(4)<<(double)p*l<<endl;
	return 0;
}
/*
	ID:zky
	OJ:BZOJ
	Index:1070
	Language:C++
*/
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<climits>
#include<iomanip>
#include<iostream>
#include<algorithm>
#define clear(x) memset(x,0,sizeof(x))
#define inf(x) memset(x,0x7f,sizeof(x))
#define Size G[u].size()
#define read3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define read4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)
using namespace std;
int s,t;
int n,m,k;
const int INF=INT_MAX;
struct edge{
	int u,v,cap,flow,cost;
};
vector<edge>edges;
vector<vector<int> >G;
int d[1001];
int cur[1001];
int vis[1001];
int prev[1001];
void add(int u,int v,int cap,int cost){
	edges.push_back((edge){u,v,cap,0,cost});
	G[u].push_back(edges.size()-1);
}
int a[1001];
bool spfa(int  &flow,int &cost){
	queue<int>q;
	q.push(s);
	memset(d,0x7f,sizeof(d));
	memset(vis,0,sizeof(vis));
	int B=d[0];
	d[s]=0;
	vis[s]=1;
	a[s]=INF;
	while(!q.empty()){
		int u=q.front();q.pop();vis[u]=0;
		for(int i=0;i<G[u].size();i++){
			edge &e=edges[G[u][i]];
			if(e.cap-e.flow>0&&d[e.v]>d[u]+e.cost){
				d[e.v]=d[u]+e.cost;
				prev[e.v]=G[u][i];
				a[e.v]=min(a[u],e.cap-e.flow);
				if(!vis[e.v]){
					vis[e.v]=1;
					q.push(e.v);
				}
			}
		}
	}
	if(d[t]==B)return false;
	int u=t;
	flow+=a[t];
	cost+=a[t]*d[t];
	while(u!=s){
		edges[prev[u]].flow+=a[t];
		edges[prev[u]^1].flow-=a[t];
		u=edges[prev[u]].u;
	}
	return true;
}
void costflow(){
	int flow=0,cost=0;
	while(spfa(flow,cost));
	cout<<fixed<<setprecision(2)<<(double)cost/n<<endl;
}
int map[61][61];
int main(){
	cin>>m>>n;
	G.resize(n*m+n+100);
	for(int i=1;i<=n;i++)
	for(int j=1;j<=m;j++){
		cin>>map[i][j];
	}
	s=0;t=n+n*m+1;
	for(int i=1;i<=n;i++){
		add(s,i,1,0);
		add(i,s,0,0);	
	}
	int T=n;
	for(int i=1;i<=m;i++)
	for(int j=1;j<=n;j++){
		add(++T,t,1,0);
		add(t,T,0,0);
		for(int k=1;k<=n;k++){
			add(k,T,1,map[k][i]*j);
			add(T,k,0,-map[k][i]*j);
		}
	}
	costflow();
	return 0;
}



你可能感兴趣的:(网络流,bzoj)