[Usaco-3.2.6] Sweet Butter香甜的黄油

usaco-3.2.6 Sweet Butter香甜的黄油

时间限制: 1 Sec 内存限制: 128 MB

题目描述

农夫John发现做出全威斯康辛州最甜的黄油的方法:糖。把糖放在一片牧场上,他知道N(1<=N<=500)只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油。当然,他将付出额外的费用在奶牛上。
农夫John很狡猾。像以前的Pavlov,他知道他可以训练这些奶牛,让它们在听到铃声时去一个特定的牧场。他打算将糖放在那里然后下午发出铃声,以至他可以在晚上挤奶。
农夫John知道每只奶牛都在各自喜欢的牧场(一个牧场不一定只有一头牛)。给出各头牛在的牧场和牧场间的路线,找出使所有牛到达的路程和最短的牧场(他将把糖放在那)

输入

第一行: 三个数:奶牛数N,牧场数(2<=P<=800),牧场间道路数C(1<=C<=1450)
第二行到第N+1行: 1到N头奶牛所在的牧场号
第N+2行到第N+C+1行: 每行有三个数:相连的牧场A、B,两牧场间距离D(1<=D<=255),当然,连接是双向的

输出

一行 输出奶牛必须行走的最小的距离和

样例输入

3 4 5
2
3
4
1 2 1
1 3 5
2 3 7
2 4 3
3 4 5

样例输出

8

题解

  • 因为是无向图,所以枚举放的点作为起点SPFA算出分别到达其他点的距离即可

  • 有空补上 Dijkstra+heap 的写法

SPFA

测试点#1.in 结果:AC 内存使用量: 256kB 时间使用量: 0ms
测试点#10.in 结果:AC 内存使用量: 256kB 时间使用量: 88ms
测试点#2.in 结果:AC 内存使用量: 256kB 时间使用量: 0ms
测试点#3.in 结果:AC 内存使用量: 256kB 时间使用量: 1ms
测试点#4.in 结果:AC 内存使用量: 256kB 时间使用量: 0ms
测试点#5.in 结果:AC 内存使用量: 256kB 时间使用量: 0ms
测试点#6.in 结果:AC 内存使用量: 256kB 时间使用量: 5ms
测试点#7.in 结果:AC 内存使用量: 256kB 时间使用量: 23ms
测试点#8.in 结果:AC 内存使用量: 256kB 时间使用量: 53ms
测试点#9.in 结果:AC 内存使用量: 256kB 时间使用量: 89ms

var
 w:array[0..10000,1..3]of longint;
 e,p,dist:array[0..1000]of longint;
 t:array[0..10000]of longint;
 i,j,k,l:longint;
 a,b,c,x,y,tt:longint;
 n,m,start,finish:longint;
 head,tail,sum,ans:longint;
function min(a,b:longint):longint;
begin
 if a>b
 then exit(b)
 else exit(a);
end;

begin
 ans:=maxlongint;
 readln(e[0],n,m); k:=n+1;
 for i:=1 to e[0] do
  begin
   readln(a);
   inc(e[a]);
  end;
 for i:=1 to m do
  begin
   readln(a,b,c);
   w[k,1]:=b;
   w[k,2]:=c;
   if w[a,3]=0
   then w[a,3]:=k
   else w[w[a,1],3]:=k;
   w[a,1]:=k;
   inc(k);
   w[k,1]:=a;
   w[k,2]:=c;
   if w[b,3]=0
   then w[b,3]:=k
   else w[w[b,1],3]:=k;
   w[b,1]:=k;
   inc(k);
  end;
 for l:=1 to n do
 begin
 sum:=0; start:=l;
 for i:=1 to n do
  dist[i]:=100000;
 dist[start]:=0;
 head:=1; tail:=2; t[1]:=start; p[start]:=1;
 while head<tail do
  begin
   x:=head; y:=tail;
   for i:=head to tail-1 do
    begin
     tt:=w[t[i],3];
     while tt<>0 do
      begin
       if dist[t[i]]+w[tt,2]<dist[w[tt,1]]
       then
        begin
         dist[w[tt,1]]:=dist[t[i]]+w[tt,2];
         if p[w[tt,1]]=0
         then begin p[w[tt,1]]:=1; t[y]:=w[tt,1]; inc(y); end;
        end;
       tt:=w[tt,3];
      end;
     inc(x);
     p[t[i]]:=0;
    end;
   head:=x; tail:=y;
  end;
 for k:=1 to n do
  inc(sum,dist[k]*e[k]);
 ans:=min(sum,ans);
 end;
 writeln(ans);
end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值