比赛 HAOI2009 模拟试题1 评测结果 AAAAAAAAAA
题目名称 上学路线 最终得分 100
用户昵称 ybh 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2011-03-25 21:40:09
显示代码纯文本
program routez;
type
	tymap=record
		next,t,c:longint;
	end;
	tylimit=record
		next,lim:longint;
	end;

var
	map:array[0..500,0..500] of tymap;
	ge,gelimit,ans,q,vd,d:array[0..500] of longint;
	arrive,b:array[0..500] of boolean;  	
	n,m,i,j,v,min,mini,rp,rq,rt,rc,maxflow,geans,h,t:longint;
	cost,cost2:array[0..500] of longint;
	limit:array[0..500,0..500] of tylimit;
	flow:array[0..500,0..500] of longint;
	fan:array[0..500,0..500] of longint;
	bian,bian2:array[0..500,0..500] of longint;	

function minmin(a,b:longint):longint;
begin
  if a>b
    then minmin:=b
    else minmin:=a;
end;

function dfs(u,flow2:longint):longint;
var
	v,tmp,dfs2:longint;
begin
	if u=n then 
	begin
		dfs:=flow2;
		exit;
	end;

	dfs2:=0;
	for v:=1 to ge[u] do
		if	limit[u,v].lim>0 then
		begin	
			if (flow[u,v]<limit[u,v].lim) and (d[u]=d[limit[u,v].next]+1) then
			begin
				tmp:=dfs(limit[u,v].next,minmin(flow2-dfs2,limit[u,v].lim-flow[u,v]));
				inc(flow[u,v],tmp);
				dfs2:=dfs2+tmp;
				if dfs2=flow2 then 
				begin
					dfs:=flow2;
					exit;
				end;
			end;
		end
		else
		begin
			if (flow[limit[u,v].next,fan[u,v]]>0) and  (d[u]=d[limit[u,v].next]+1) then
			begin
				tmp:=dfs(limit[u,v].next,minmin(flow2-dfs2,flow[limit[u,v].next,fan[u,v]]));
				dec(flow[limit[u,v].next,fan[u,v]],tmp);
				dfs2:=dfs2+tmp;
				if dfs2=flow2 then 
				begin
					dfs:=flow2;
					exit;
				end;
			end;
		end;				
	if d[1]>=n then 
	begin
		dfs:=dfs2;
		exit;
	end;
	dec(vd[d[u]]);
	if vd[d[u]]=0
		then d[1]:=n;
	inc(d[u]);
	inc(vd[d[u]]);
	dfs:=dfs2;
end;

begin
	assign(input,'routez.in');
	reset(input);
	assign(output,'routez.out');
	rewrite(output);
	
	readln(n,m);
	fillchar(ge,sizeof(ge),0);
	fillchar(gelimit,sizeof(gelimit),0);
	for i:=1 to m do
	begin	
		readln(rp,rq,rt,rc);
		inc(ge[rp]);
		map[rp,ge[rp]].next:=rq;
		map[rp,ge[rp]].t:=rt;		
		map[rp,ge[rp]].c:=rc;
		bian[rp,ge[rp]]:=i;
		inc(ge[rq]);
		map[rq,ge[rq]].next:=rp;
		map[rq,ge[rq]].t:=rt;
		map[rq,ge[rq]].c:=rc;	
		bian[rq,ge[rq]]:=i;
	end;
	
	fillchar(arrive,sizeof(arrive),false);
	fillchar(cost,sizeof(cost),0);
	v:=1;
	arrive[1]:=true;
	repeat  
		for i:=1 to ge[v] do  
		begin  
			if ((cost[v]+map[v,i].t<cost[map[v,i].next]) or (cost[map[v,i].next]=0)) and (arrive[map[v,i].next]=false) then  
			begin  
				cost[map[v,i].next]:=cost[v]+map[v,i].t;  
			end  
		end;  
		min:=maxlongint;  
		for i:=1 to n do  
		if (arrive[i]=false) and (min>cost[i]) and (cost[i]>0) then  
		begin  
			min:=cost[i];  
			mini:=i  
		end;  
		if min<maxlongint then  
		begin  
			arrive[mini]:=true;  
			v:=mini;  
		end  
	until min=maxlongint;  
	writeln(cost[n]);
	
	fillchar(arrive,sizeof(arrive),false);
	fillchar(cost2,sizeof(cost2),0);
	v:=n;
	arrive[n]:=true;
	repeat  
		for i:=1 to ge[v] do  
		begin  
			if ((cost2[v]+map[v,i].t<cost2[map[v,i].next]) or (cost2[map[v,i].next]=0)) and (arrive[map[v,i].next]=false) then  
			begin  
				cost2[map[v,i].next]:=cost2[v]+map[v,i].t;  
			end  
		end;  
		min:=maxlongint;  
		for i:=1 to n do  
		if (arrive[i]=false) and (min>cost2[i]) and (cost2[i]>0) then  
		begin  
			min:=cost2[i];  
			mini:=i  
		end;  
		if min<maxlongint then  
		begin  
			arrive[mini]:=true;  
			v:=mini;  
		end  
	until min=maxlongint;  		
		
	fillchar(limit,sizeof(limit),0);
	for i:=1 to n do
	begin
		for j:=1 to ge[i] do
		begin
			if cost[i]+map[i,j].t+cost2[map[i,j].next]=cost[n] then
			begin
				inc(gelimit[i]);
				limit[i,gelimit[i]].next:=map[i,j].next;
				limit[i,gelimit[i]].lim:=map[i,j].c;
				bian2[i,gelimit[i]]:=bian[i,j];
				inc(gelimit[map[i,j].next]);
				limit[map[i,j].next,gelimit[map[i,j].next]].next:=i;
				limit[map[i,j].next,gelimit[map[i,j].next]].lim:=0;
				fan[map[i,j].next,gelimit[map[i,j].next]]:=gelimit[i];
			end;
		end;
	end;	
	
	fillchar(vd,sizeof(vd),0);
	fillchar(d,sizeof(d),0);
	fillchar(flow,sizeof(flow),0);
	vd[0]:=n;
	maxflow:=0;
	while d[1]<n do
	    inc(maxflow,dfs(1,maxlongint));

	fillchar(b,sizeof(b),false);	
	h:=0;
	t:=1;
	q[1]:=1;
	b[1]:=true;
	while h<=t do
	begin
		inc(h);
		i:=q[h];
		for j:=1 to gelimit[i] do
		begin
			if (b[limit[i,j].next]=false) and (limit[i,j].lim>0) and (flow[i,j]<limit[i,j].lim) then
			begin
				inc(t);
				q[t]:=limit[i,j].next;
				b[q[t]]:=true;
			end;
		end;
	end;
	
	geans:=0;
	for i:=1 to n do
		for j:=1 to gelimit[i] do
		begin
			if (limit[i,j].lim>0) and (b[i]=true) and (b[limit[i,j].next]=false) then
			begin
				inc(geans);
				ans[geans]:=bian2[i,j];
			end;
		end;
			
	writeln(geans,' ',maxflow);
	for i:=1 to geans do
		writeln(ans[i]);
	close(input);
	close(output)
end.