奶牛派对

我们发现每头牛需要走的路程即为它到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——奶牛派对的更多相关文章

  1. BZOJ 1631==USACO 2007== POJ 3268 Cow Party奶牛派对

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19226   Accepted: 8775 Description One ...

  2. BZOJ1631: [Usaco2007 Feb]Cow Party

    1631: [Usaco2007 Feb]Cow Party Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 459  Solved: 338[Submit ...

  3. BZOJ 1631: [Usaco2007 Feb]Cow Party( 最短路 )

    这道题和蔡大神出的今年STOI初中组的第二题几乎一模一样... 先跑一遍最短路 , 再把所有边反向 , 再跑一遍 , 所有点两次相加的最大值即为answer --------------------- ...

  4. BZOJ 1631: [Usaco2007 Feb]Cow Party

    题目 1631: [Usaco2007 Feb]Cow Party Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 491  Solved: 362[Sub ...

  5. 【BZOJ】1631: [Usaco2007 Feb]Cow Party(dijkstra)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1631 看到m<=100000果断用dij(可是好像dij比spfa还慢了在这里?)//upd: ...

  6. [Usaco2007 Feb]Cow Party

    题目描述 农场有N(1≤N≤1000)个牛棚,每个牛棚都有1只奶牛要参加在X牛棚举行的奶牛派对.共有M(1≤M≤100000)条单向路连接着牛棚,第i条踣需要Ti的时间来通过.牛们都很懒,所以不管是前 ...

  7. 【COGS & USACO】896. 圈奶牛(凸包)

    http://cojs.tk/cogs/problem/problem.php?pid=896 我的计算几何入门题... 看了看白书的计算几何部分,,恩好嘛.. 乃们都用向量!!!! 干嘛非要将2个点 ...

  8. 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 ...

  9. cogs 896. 圈奶牛

    ★★☆   输入文件:fc.in   输出文件:fc.out   简单对比 时间限制:1 s   内存限制:128 MB 描述 农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏.他建造的围栏必 ...

随机推荐

  1. CentOS 手动配置本地yum源(参考CentOS7 制作 CentOS6本地yum源)

    将原有/etc/yum.repos.d/目录下的文件名全部改为(*.bak),如(红色标记) [root@localhost ~]# cd /etc/yum.repos.d/ [root@localh ...

  2. Python学习笔记:第2天while循环 运算符 格式化输出 编码

    目录 1. while循环 continue.break和else语句 2. 格式化输出 3. 运算符 3.1 算数运算 3.2 比较运算符 3.3 赋值运算符 3.4 逻辑运算符 3.5 成员运算符 ...

  3. Typora -- 书写即美学

    #Typora -- 书写即美学 ##基本快捷键--需要在所见即所想界面进行输入 加粗 Ctrl + B 加粗 斜体 Ctrl + I 斜体 下划线 Ctrl + U 下划线 删除线 Ctrl + S ...

  4. (数据科学学习手札13)K-medoids聚类算法原理简介&Python与R的实现

    前几篇我们较为详细地介绍了K-means聚类法的实现方法和具体实战,这种方法虽然快速高效,是大规模数据聚类分析中首选的方法,但是它也有一些短板,比如在数据集中有脏数据时,由于其对每一个类的准则函数为平 ...

  5. .net core 新建一个web api 的步骤 初级

    1.使用VS2017 选择 .net core web应用程序. 2.选择web api(空). 3.如果需要用iis express调试,则需要修改 program.cs. 4.在Controlle ...

  6. 复制MySQL数据库A到另外一个MySQL数据库B(仅仅针对innodb数据库引擎)

    方案一:(不用太大的变化my.ini文件) copy 原数据库A中的   数据库(database)  ib_logfile1  ib_logfile0   ibdata1: 关闭目的数据库B: 备份 ...

  7. 判断电脑CPU硬件支不支持64位

    你可以在注册表中查看: HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment\PROCESSO ...

  8. 【数据结构】 Queue 的简单实现

    [数据结构] Queue 的简单实现 public class XQueue<T> { /// <summary> /// 第一个元素 /// </summary> ...

  9. Linux上jdk的安装(CentOS6.5)

    centos openjdk 安装 http://www.cnblogs.com/ilahsa/archive/2012/12/11/2813059.html 知CentOS6.5桌面版默认安装的是J ...

  10. CWindowWnd类源码分析

    CWindowWnd代码在UIBase.h和UIBase.cpp文件里.主要实现的是一个基本窗口的创建与消息处理. 相关代码: 头文件: class UILIB_API CWindowWnd { pu ...