记录编号 61027 评测结果 AAAAAAAAAA
题目名称 [SCOI 2007] 修车 最终得分 100
用户昵称 Gravatarcstdio 是否通过 通过
代码语言 C++ 运行时间 0.477 s
提交时间 2013-06-02 20:59:26 内存使用 4.24 MiB
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<vector>
#include<queue>
using namespace std;
const int INF=0x7fffffff;
const int SIZEN=10000;
int numc,nume;//车主和工人的数量
int S,T;
class EDGE{
public:
	int from,to,cap,flow,cost;
};
vector<EDGE> edges;
vector<int> c[SIZEN];//邻接表
int ansc=0;
void addedge(int from,int to,int cap,int cost){
	edges.push_back((EDGE){from,to,cap,0,cost});
	edges.push_back((EDGE){to,from,0,0,-cost});
	int tot=edges.size()-2;
	c[from].push_back(tot);
	c[to].push_back(tot+1);
}
bool SPFA(int s,int t,int n){//0~n
	queue<int> Q;
	int f[SIZEN]={0};//距离
	bool inq[SIZEN]={0};//是否在队列中
	int cf[SIZEN]={0};//每个点的最大可改进量
	int father[SIZEN]={0};
	int i;
	for(i=0;i<=n;i++) f[i]=INF,inq[i]=false,cf[i]=INF,father[i]=-1;
	f[s]=0,inq[s]=true,Q.push(s);
	int x;
	#define now edges[c[x][i]]
	while(!Q.empty()){
		x=Q.front();Q.pop();
		inq[x]=false;
		for(i=0;i<c[x].size();i++){
			if(now.cap>now.flow&&f[now.to]>f[x]+now.cost){
				f[now.to]=f[x]+now.cost;
				father[now.to]=c[x][i];
				cf[now.to]=min(cf[now.to],now.cap-now.flow);
				if(!inq[now.to]){
					inq[now.to]=true;
					Q.push(now.to);
				}
			}
		}
	}
	if(f[t]==INF) return false;
	ansc+=cf[t]*f[t];
	x=t;
	while(x!=s){
		edges[father[x]].flow+=cf[t];
		edges[father[x]^1].flow-=cf[t];
		x=edges[father[x]].from;
	}
	return true;
}
void MCMF(void){
	while(SPFA(S,T,T));
	double ans=(double)ansc/numc;
	cout<<setiosflags(ios::fixed)<<setprecision(2)<<ans<<endl;
}
int tim[1000][1000];
void init(void){
	scanf("%d%d",&nume,&numc);
	S=0,T=numc+numc*nume+1;
	int i,j;
	for(i=1;i<=numc;i++){
		for(j=1;j<=nume;j++) scanf("%d",&tim[i][j]);//第j个工人修好第i辆车的用时
	}
	int k;
	for(i=1;i<=numc;i++) addedge(S,i,1,0);
	for(i=numc+numc*nume;i>=numc+1;i--) addedge(i,T,1,0);
	for(i=1;i<=numc;i++){
		for(j=1;j<=nume;j++){
			for(k=1;k<=numc;k++){
				addedge(i,j*numc+k,1,(numc-k+1)*tim[i][j]);
			}
		}
	}
}
int main(){
	freopen("scoi2007_repair.in","r",stdin);
	freopen("scoi2007_repair.out","w",stdout);
	init();
	MCMF();
	return 0;
}