COGS 497——奶牛派对
我们发现每头牛需要走的路程即为它到x的最短路+x到它的最短路。
转化:
于是这道题变成了一道典型的单源最短路问题,只需求出每个点到x的最短路dl,以及从x到此点的最短路d2,然后去找max(dl+d2)即可。
效率分析:
使用dijsktra算法,时间复杂度为O(n^2)。
【我的程序】
type aa=array[..,..] of longint;
var
n,m,x,a,b,t,i,j,k,min,max:longint;
map1,map2:aa;
ans:array[..] of longint;
f:array[..] of boolean;
d:array[..] of longint;
procedure dijkstra(map:aa);
begin
for i:= to n do
begin d[i]:=map[x,i]; f[i]:=false; end;
f[x]:=true;
for i:= to n do
begin
min:=maxlongint; k:=;
for j:= to n do
if (not f[j]) and (d[j]<min) then
begin min:=d[j]; k:=j; end;
if (k=) or (min=maxlongint) then exit;
f[k]:=true;
for j:= to n do
if (not f[j]) and (d[k]+map[k,j]<d[j])
then d[j]:=d[k]+map[k,j];
end;
for i:= to n do ans[i]:=ans[i]+d[i];
end;
begin
assign(input,'party.in');
reset(input);
assign(output,'party.out');
rewrite(output);
readln(n,m,x);
for i:= to n do
for j:= to n do
if i=j then begin map1[i,j]:=; map2[i,j]:=; end
else begin map1[i,j]:=maxlongint div ; map2[i,j]:=maxlongint div ; end;
for i:= to m do
begin
readln(a,b,t);
map1[a,b]:=t;
map2[b,a]:=t;//if (map[a,b]>t) or (map[a,b]=) then map[a,b]:=t;
end;
dijkstra(map1);
dijkstra(map2);
for i:= to n do if ans[i]>max then max:=ans[i];
writeln(max);
end.
【老师给的标程】
const
maxn=;
maxm=;
var
n,m,X,i,j,sum,ans:longint;
path:array[..,..maxn,..maxn]of Longint;
dis:array[..,..maxn]of Longint;
v:array[..maxn]of boolean;
procedure Init;
var
A,b,c:Longint;
begin
readln(n,m,X);
for i := to n do
for j:= to n do
begin
path[,i,j]:=maxm;
path[,i,j]:=maxm;
end;
for i := to m do
begin
readln(A,b,c);
If path[,A,b] > c then
begin
path[,A,b] := c;
path[,b,A] := c;
end;
end;
end;
procedure dij(k:longint);
var
min,minj,i,j,t:Longint;
begin
fillchar(v,sizeof(v),false);
for i := To n do
If path[k,X,i] <> maxm then dis[k,i] := path[k,X,i]
else dis[k,i] := maxm;
v[x] := true;
dis[k,x] := ;
for i := n- downto do
begin
min:=maxm;
for j := to n do
if (dis[k,j] < min)And(not v[j]) then
begin
min := dis[k,j];
minj := j;
end;
if min<>maxm then
begin
v[minj] := true;
j:=minj;
for t := to n do
If (dis[k,j]+path[k,j,t] < dis[k,t])And(not v[t]) Then
dis[k,t] := dis[k,j]+path[k,j,t];
end;
end;
end;
procedure main;
begin
dij();
dij();
Ans:=;
for i := to n do
begin
sum := dis[,i]+dis[,i];
If (sum>ans)and(i<>X) then ans:=sum;
end;
end;
procedure Ouit;
begin
Writeln(ans);
end;
begin
Init;
main;
Ouit;
end.
【考试时错误的做法】还是没有真正理解dijkstra
var
i,j,n,m,x,k1,k2,w,min,k,max:longint;
a:array[..,..] of longint;
d,ans:array[..] of longint;
f:array[..] of boolean;
procedure dijkstra1;
begin
for i:= to n do
begin d[i]:=a[x,i]; f[i]:=false; end;
f[x]:=true;
for i:= to n do
begin
min:=maxlongint; k:=;
for j:= to n do
if (not f[j]) and (d[j]<min) then
begin
min:=d[j]; k:=j;
end;
if (k=)or(min=maxint) then exit;
f[k]:=true;
for j:= to n do
if (not f[j]) and (d[k]+a[k,j]<d[j]) then d[j]:=d[k]+a[k,j];
end;
end;
procedure dijkstra2;
begin
for i:= to n do
begin d[i]:=a[i,x]; f[i]:=false; end;
f[x]:=true;
for i:= to n do
begin
min:=maxlongint; k:=;
for j:= to n do
if (not f[j]) and (d[j]<min) then
begin
min:=d[j]; k:=j;
end;
if (k=)or(min=maxint) then exit;
f[k]:=true;
for j:= to n do
if (not f[j]) and (d[k]+a[k,j]<d[j]) then d[j]:=d[k]+a[k,j];
end;
end;
begin
readln(n,m,x);
for i:= to n do
for j:= to n do
if i=j then a[i,j]:= else a[i,j]:=maxlongint div ;
for i:= to m do
begin
readln(k1,k2,w);
a[k1,k2]:=w;
end;
dijkstra1;
for i:= to n do if i<>x then begin ans[i]:=ans[i]+d[i]; end;
dijkstra2; max:=-maxint;
for i:= to n do if i<>x then
begin
ans[i]:=ans[i]+d[i];
if ans[i]>max then max:=ans[i];
end;
writeln(max);
end.
COGS 497——奶牛派对的更多相关文章
- BZOJ 1631==USACO 2007== POJ 3268 Cow Party奶牛派对
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19226 Accepted: 8775 Description One ...
- BZOJ1631: [Usaco2007 Feb]Cow Party
1631: [Usaco2007 Feb]Cow Party Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 459 Solved: 338[Submit ...
- BZOJ 1631: [Usaco2007 Feb]Cow Party( 最短路 )
这道题和蔡大神出的今年STOI初中组的第二题几乎一模一样... 先跑一遍最短路 , 再把所有边反向 , 再跑一遍 , 所有点两次相加的最大值即为answer --------------------- ...
- BZOJ 1631: [Usaco2007 Feb]Cow Party
题目 1631: [Usaco2007 Feb]Cow Party Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 491 Solved: 362[Sub ...
- 【BZOJ】1631: [Usaco2007 Feb]Cow Party(dijkstra)
http://www.lydsy.com/JudgeOnline/problem.php?id=1631 看到m<=100000果断用dij(可是好像dij比spfa还慢了在这里?)//upd: ...
- [Usaco2007 Feb]Cow Party
题目描述 农场有N(1≤N≤1000)个牛棚,每个牛棚都有1只奶牛要参加在X牛棚举行的奶牛派对.共有M(1≤M≤100000)条单向路连接着牛棚,第i条踣需要Ti的时间来通过.牛们都很懒,所以不管是前 ...
- 【COGS & USACO】896. 圈奶牛(凸包)
http://cojs.tk/cogs/problem/problem.php?pid=896 我的计算几何入门题... 看了看白书的计算几何部分,,恩好嘛.. 乃们都用向量!!!! 干嘛非要将2个点 ...
- COGS——T 803. [USACO Hol10] 政党 || 1776: [Usaco2010 Hol]cowpol 奶牛政坛
http://www.lydsy.com/JudgeOnline/problem.php?id=1776||http://cogs.pro/cogs/problem/problem.php?pid=8 ...
- cogs 896. 圈奶牛
★★☆ 输入文件:fc.in 输出文件:fc.out 简单对比 时间限制:1 s 内存限制:128 MB 描述 农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏.他建造的围栏必 ...
随机推荐
- jquery图片滚动normalizy.css
article,aside,details,figcaption,figure,footer,header,hgroup,main,nav,section,summary{display:block; ...
- TP5部署服务器问题总结
及最近部署TP5遇到了很多坑,各种环境下都会出现一些问题,下面是我记录的排坑之路 先说最简单的lnmp一键安装包,我用的是1.5稳定版 安装命令:wget http://soft.vpser.net/ ...
- hadoop2.5.0 HA高可用配置
hadoop2.5.0 HA配置 1.修改hadoop中的配置文件 进入/usr/local/src/hadoop-2.5.0-cdh5.3.6/etc/hadoop目录,修改hadoop-env.s ...
- Python3 函数参数
# # 常规参数 # def jiafa(a,b): # print(a+b) # # jiafa(5,6) # # # def xinxi(name,location,sex='male'): # ...
- TCD产品技术参考资料
1.Willis环 https://en.wikipedia.org/wiki/Circle_of_Willis 2.TCD仿真软件 http://www.transcranial.com/index ...
- RelativeSource设定绑定方向
<Window x:Class="Yingbao.Chapter2.RelativeEx.AppWin" xmlns="http://schemas.microso ...
- exchange 2007迁移到2010
标签:exchange 2007 2010 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://zpf666.blog.51cto.c ...
- Mybatis快速入门指南
简介 当下越来越多的企业项目架构中,在持久层部分,抛弃了Hibernate框架,而选用Mybatis框架取而代之,旨在更加深入细致的控制和数据库的交互. MyBatis 本是apache的一个开源项目 ...
- 『Golang』在Golang中使用json
由于要开发一个小型的web应用,而web应用大部分都会使用json作为数据传输的格式,所以有了这篇文章. 包引用 import ( "encoding/json" "gi ...
- jqgrid-parmNames和jsonReader的使用,以及json的返回格式(转)
prmNames : { page:"page", // 表示请求页码的参数名称 rows:"rows", // 表示请求行数的参数名称 sort: ...