奶牛派对

我们发现每头牛需要走的路程即为它到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. Java OOP——第四章 异常

    1. 接口:接口就是给出一些没有内容的方法,封装到一起,到某个类要使用的时候,在根据具体情况把这些方法写出来. 接口是更加抽象的抽象的类, 抽象类里的方法可以有方法体, 接口里的所有方法都没有方法体. ...

  2. redis相关目录

    redis的docker化安装 redis的主从配置

  3. 微信小程序scroll-viwe遇到的问题

    1.当使用scroll-view的时候里面不可以使用某些标签 2.当使用scroll-view的时候会出现,子元素中滑动的时候会出现滚动的情况,我遇到的是因为view设置了高度和行高,一旦设置了这个, ...

  4. clear()、sync()、ignore()

    #include <iostream> using namespace std; int main() { int a; cin>>a; cout<<cin.rds ...

  5. TCD产品技术参考资料

    1.Willis环 https://en.wikipedia.org/wiki/Circle_of_Willis 2.TCD仿真软件 http://www.transcranial.com/index ...

  6. 面试官常问的10个Linux问题

    1.如何暂停一个正在运行的进程,把其放在后台(不运行)? 为了停止正在运行的进程,让其再后台运行,我们可以使用组合键Ctrl+Z. 2.什么是安装Linux所需的最小分区数量,以及如何查看系统启动信息 ...

  7. Vue-router使用

    Vue路由:--------------------------------------------------------1 .Vue-rouer入门2 .子路由3 .路由传参4 .多路由区域操作5 ...

  8. 通过数据库恢复SharePoint网站

           SharePoint网站一般包含很多个数据库,最主要的有3个,分别是SharePoint_Admin_Content(管理中心数据库),SharePoint_Config(配置数据库)和 ...

  9. SGU刷题之路,开始了

    0. 关于SGU的简介 SGU的网址是:acm.sgu.ru 向不了解的同学介绍一下SGU这个题库: 1. 题目难度很高,题目大多很经典. 2. 其数据范围很小,时间和空间要求也都很小,同时很精确.甚 ...

  10. 九度OJ--Q1473

    import java.util.ArrayList;import java.util.Scanner; /* * 题目描述: * 大家都知道,数据在计算机里中存储是以二进制的形式存储的. * 有一天 ...