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 描述 农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏.他建造的围栏必 ...
随机推荐
- 配置Github秘钥
Git安装完成后,需要手动配置ssh密钥 配置github的ssh密钥: (1)打开Git Bash查看电脑上是否已经存在SSH密钥: 输入 cd ~/.ssh 若如上图显示无法找到该文件则要创建新的 ...
- Django---URL、Views
1.Django URL(路由系统) URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL模式以及要为该URL模式调用的视图函数之间的映射表:你就是以这种方式告诉Djang ...
- Apache Maven(七):settings.xml
settings.xml 文件中包含settings标签,这个标签可以配置如何去执行Maven.其中包括本地存储库位置,备用远程存储库服务器和身份验证信息等值. 有如下两个位置可能存放这setting ...
- python 方法解析顺序 mro
一.概要: mor(Method Resolution Order),即方法解析顺序,是python中用于处理二义性问题的算法 二义性: 1.两个基类,A和B都定义了f()方法,c继承A和B那么C调用 ...
- PHPPCRE正则解析
一.前言 前面的博客里,有对字符集的解析.这里就不是字符集的事儿了,在PHP中很多函数的处理默认是unicode中的UTF-8编码格式.那么废话不多说,直接开始正题. 二.PHP函数mb_split解 ...
- php+sqlserver处理读取decimal 类型数据,不满1的数字会去掉0的问题
php+sqlserver处理读取decimal 类型数据,如果数据不满1,会去掉0的问题.比如读取的数据是 0.05,会显示 .05 function convert_number($number) ...
- zabbix配置报警媒介-用户-动作-邮件脚本触发mailx邮件报警
2018-09-16更新,新版本zabbix不需要使用脚本发送邮件,在zabbix web界面直接配置就可以 配置邮件参数,测试发送邮件 确认安装相关服务,centos7默认安装 [root@VM_1 ...
- Ganglia3.1.7安装与配置(收录)
一.所需要软件 二.安装过程 1.Ganglia运行平台的安装 2.Ganglia依赖库的安装 3.RRDTool的安装 4.Ganglia的安装 (包括使用yum方式 ...
- 实用脚本 1 -- 安装Ctags
Ctags是vim下方便代码阅读的工具,一般VIM中已经默认安装了Ctags,它可以帮助程序员很容易地浏览源代码. 1.如果系统中没有此工具用如下方法安装: 到ctags官网下载源码,解压后 ...
- java二分法来求一个数组中一个值的key
package TestArray; import java.util.Arrays; /** * 二分法查找 */ public class Test { public static void ma ...