码迷,mamicode.com
首页 > 其他好文 > 详细

CF 1307 G Cow and Exercise

时间:2021-02-22 12:52:10      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:long   epo   clock   fir   cow   false   ast   cto   now()   

CF 1307 G Cow and Exercise

一个结论:
取出前\(k\)小的互不相交的路径。

假设它们的长度和为\(len_k\)

则答案为\(\min\{(len_k+x)/k\}\)

proof:

假设只给\(k\)条增加,那么一定是尽可能均匀。

如果不是最优的会被覆盖。

/*
{
######################
#       Author       #
#        Gary        #
#        2021        #
######################
*/
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define check_min(a,b) a=min(a,b)
#define check_max(a,b) a=max(a,b)
using namespace std;
//inline int read(){
//    int x=0;
//    char ch=getchar();
//    while(ch<‘0‘||ch>‘9‘){
//        ch=getchar();
//    }
//    while(ch>=‘0‘&&ch<=‘9‘){
//        x=(x<<1)+(x<<3)+(ch^48);
//        ch=getchar();
//    }
//    return x;
//}
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
/*}
*/

const int GRAPH_SIZE= 55;
int s=0,t=GRAPH_SIZE-1;
struct EDGE{
	int u,v;
	double c,cos;
};
vector<EDGE> e;
vector<int> each[GRAPH_SIZE];
double maxflow,mincost;
double flow[GRAPH_SIZE];
double dis[GRAPH_SIZE];
int inq[GRAPH_SIZE],las[GRAPH_SIZE];
int n,m;
vector<pair<double,double> > v;
bool spfa(){
	memset(inq,0,sizeof(inq));
	rb(i,1,n){
		dis[i]=1e9;
	}
	flow[s]=1e9;
	dis[s]=0;
	queue<int> q;
	q.push(s);
	inq[s]=1;
	while(!q.empty()){
		int now=q.front();
		q.pop();
		inq[now]=0;
		for(auto it:each[now]){
			int to;
			double f;
			double c;
			to=e[it].v;
			f=e[it].c;
			c=e[it].cos;
			if(f<=0) continue;
			if(dis[to]>dis[now]+c){
				dis[to]=dis[now]+c;
				las[to]=it;
				flow[to]=min(flow[now],f);
				if(!inq[to]) q.push(to);
				inq[to]=1;
			}
		}
	}
	return dis[t]<1e9;
}
void KM(){
	while(spfa()){
		maxflow+=flow[t];
		mincost+=dis[t]*flow[t];
		int now=t;
		while(now!=s){
			e[las[now]].c-=flow[t];
			e[las[now]^1].c+=flow[t];
			now=e[las[now]].u;
		}
		v.PB(II(maxflow,mincost));
	}
}
void make_edge(int U,int V,int C,int COS){
	EDGE tmp;
	tmp.u=U;
	tmp.v=V;
	tmp.c=C;
	tmp.cos=COS;
	e.PB(tmp);
	each[U].PB(e.size()-1);
	swap(tmp.u,tmp.v);
	tmp.c=0;
	tmp.cos=-COS;
	e.PB(tmp);
	each[V].PB(e.size()-1);
}

int main(){
	s=1;
	scanf("%d%d",&n,&m);
	t=n;
	rb(i,1,m){
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		make_edge(u,v,1,w);
	}
	KM();
	int q;
	scanf("%d",&q);
	while(q--){
		double x;
		scanf("%lf",&x);
		double rest=1e9;
		for(auto it:v){
			check_min(rest,(it.SEC+x)/it.FIR);
		}
		printf("%.10f\n",rest);
	}
	return 0;
}

CF 1307 G Cow and Exercise

标签:long   epo   clock   fir   cow   false   ast   cto   now()   

原文地址:https://www.cnblogs.com/gary-2005/p/14427463.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!