tyvj P1031:热浪

   
 背景 Background 
  USACO OCT09 9TH
   
   
 描述 Description  
  德克萨斯纯朴的民眾们这个夏天正在遭受巨大的热浪!!!他们的德克萨斯长角牛吃起来不错,可是他们并不是很擅长生產富含奶油的乳製品。Farmer John此时以先天下之忧而忧,后天下之乐而乐的精神,身先士卒地承担起向德克萨斯运送大量的营养冰凉的牛奶的重任,以减轻德克萨斯人忍受酷暑的痛苦。

FJ已经研究过可以把牛奶从威斯康星运送到德克萨斯州的路线。这些路线包括起始点和终点先一共经过T (1 <= T <= 2,500)个城镇,方便地标号為1到T。除了起点和终点外地每个城镇由两条双向道路连向至少两个其它地城镇。每条道路有一个通过费用(包括油费,过路费等等)。考虑这个有7个城镇的地图。城镇5是奶源,城镇4是终点(括号内的数字是道路的通过费用)。

                [1]----1---[3]-
               /         /
            [3]---6---[4]---3--[3]--4
           /         /     /|
          5     --[3]--  --[2]- |
           /     /    /     |
            [5]---7---[2]--2---[3]---
              |     /
               [1]------

经过路线5-6-3-4总共需要花费3 (5->6) + 4 (6->3) + 3 (3->4) = 10的费用。

给定一个地图,包含C (1 <= C <= 6,200)条直接连接2个城镇的道路。每条道路由道路的起点Rs,终点Re (1 <= Rs <= T; 1 <= Re <= T),和花费(1 <= Ci <= 1,000)组成。求从起始的城镇Ts (1 <= Ts <= T)到终点的城镇Te(1 <= Te <= T)最小的总费用。
   
   
 输入格式 Input Format 
  * 第一行: 4个由空格隔开的整数: T, C, Ts, Te

* 第2到第C+1行: 第i+1行描述第i条道路。有3个由空格隔开的整数: Rs, Re和Ci

   
   
 输出格式 Output Format 
  * 第一行: 一个单独的整数表示Ts到Te的最短路的长度。(不是费用麼?怎麼突然变直白了
——译者注)数据保证至少存在一条道路。

   
   
 样例输入 Sample Input  
 
   
   
 样例输出 Sample Output  
 
   
   
 时间限制 Time Limitation 
  各个测试点1s
   
   
 注释 Hint 
  5->6->1->4 (3 + 1 + 3)

 

 

单源点最短路问题,理论上dijkstra应该可以,数据规模不大,可以直接用邻接矩阵存图。

我一开始写复杂了,写了个前向星+spfa。。。

代码如下

  前向星+spfa

 

const
  maxn=2500;
  maxm=6200*2;
var
  i,j,n,m,s,t:longint;
  a,b,w:array[0..maxm] of longint;
  f,d,dist:array[0..10000] of longint;
  head,tail:longint;
  h:array[1..maxn] of boolean;
procedure qsort(l,r:longint);
var
  i,j,x,t1,t2,t3:longint;
begin
  i:=l;j:=r;
  x:=a[(i+j) div 2];
  repeat
    while a[i]<x do inc(i);
    while a[j]>x do dec(j);
    if i<=j then
    begin
      t1:=a[i];t2:=b[i];t3:=w[i];
      a[i]:=a[j];b[i]:=b[j];w[i]:=w[j];
      a[j]:=t1;b[j]:=t2;w[j]:=t3;
      inc(i);dec(j);
    end;
  until i>j;
  if i<r then qsort(i,r);
  if j>l then qsort(l,j);
end;
procedure spfa;
var
  now,i,k:longint;
begin
  fillchar(h,sizeof(h),false);
  fillchar(dist,sizeof(dist),$7f);
  head:=0;tail:=0;dist[s]:=0;
  inc(tail);
  d[tail]:=s;
  h[s]:=true;
  while head<>tail do
  begin
    inc(head);now:=d[head];h[d[head]]:=false;
    k:=f[d[head]];
    i:=k;
    while a[i]=a[k] do
    begin
      if dist[b[i]]>dist[now]+w[i] then
      begin
        dist[b[i]]:=dist[now]+w[i];
        if h[b[i]]=false then
        begin
          inc(tail);
          d[tail]:=b[i];
          h[b[i]]:=true;
        end;
      end;
      inc(i);
    end;
  end;
end;
begin
  readln(n,m,s,t);
  for i:=1 to m do
  begin
    read(a[i],b[i],w[i]);
    a[i+m]:=b[i];
    b[i+m]:=a[i];
    w[i+m]:=w[i];
  end;
  m:=m*2;
  qsort(1,m);
  i:=1;f[a[i]]:=1;
  repeat
    while a[i+1]=a[i] do inc(i);
    inc(i);
    f[a[i]]:=i;
  until i>=m;
  spfa;
  writeln(dist[t]);
end.

直接用邻接矩阵:

 

const
  maxn=2500;
  maxm=6200*2;
var
  i,j,n,m,s,t,x,y,z:longint;
  d,dist:array[0..10000] of longint;
  head,tail:longint;
  a:array[1..maxn,1..maxn] of longint;
  h:array[1..maxn] of boolean;
procedure spfa;
var
  now,i,k:longint;
begin
  fillchar(h,sizeof(h),false);
  fillchar(dist,sizeof(dist),$7f);
  head:=0;tail:=0;dist[s]:=0;
  inc(tail);
  d[tail]:=s;
  h[s]:=true;
  while head<>tail do
  begin
    inc(head);now:=d[head];h[d[head]]:=false;
    for i:=1 to n do
    begin
      if (a[now,i]>0) and (dist[i]>dist[now]+a[now,i]) then
      begin
        dist[i]:=dist[now]+a[now,i];
        if h[i]=false then
        begin
          inc(tail);
          d[tail]:=i;
          h[i]:=true;
        end;
      end;
    end;
  end;
end;
begin
  readln(n,m,s,t);
  for i:=1 to m do
  begin
    readln(x,y,z);
    a[x,y]:=z;
    a[y,x]:=z;
  end;
  m:=m*2;
  spfa;
  writeln(dist[t]);
end.

 

 

    这道题给我了一个很惨痛的教训。。spfa队列一定要开大点。。我一开始比着maxn=2500的数据开的队列,过了80分,数据很弱,然后一直不知道错在哪里。。感谢hjj大牛。。以后队列一定要开大点。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值