代码改变世界

NOIP2009 3.最优贸易

2010-10-02 17:30  snowkylin  阅读(837)  评论(0编辑  收藏  举报

内容 :

     C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市。任意两个城市之间最多只有一条道路直接相连。这 m 条道路中有一部分为单向通行的道路,一部分为双向通行的道路,双向通行的道路在统计条数时也计为 1 条。 
     C 国幅员辽阔,各地的资源分布情况各不相同,这就导致了同一种商品在不同城市的价格不一定相同。但是,同一种商品在同一个城市的买入价和卖出价始终是相同的。 
     商人阿龙来到 C 国旅游。当他得知同一种商品在不同城市的价格可能会不同这一信息之后,便决定在旅游的同时,利用商品在不同城市中的差价赚回一点旅费。设 C 国 n 个城市的标号从 1~ n,阿龙决定从 1 号城市出发,并最终在 n 号城市结束自己的旅行。在旅游的过程中,任何城市可以重复经过多次,但不要求经过所有 n 个城市。阿龙通过这样的贸易方式赚取旅费:他会选择一个经过的城市买入他最喜欢的商品——水晶球,并在之后经过的另一个城市卖出这个水晶球,用赚取的差价当做旅费。由于阿龙主要是来 C 国旅游,他决定这个贸易只进行最多一次,当然,在赚不到差价的情况下他就无需进行贸易。 
     假设 C 国有 5 个大城市,城市的编号和道路连接情况如下图,单向箭头表示这条道路为单向通行,双向箭头表示这条道路为双向通行。 

【NOIP2009】提高组复赛试题 - StrayerScott - 迷失者     假设 1~n 号城市的水晶球价格分别为 4,3,5,6,1。 
     阿龙可以选择如下一条线路:1->2->3->5,并在 2 号城市以 3 的价格买入水晶球,在 3号城市以 5的价格卖出水晶球,赚取的旅费数为 2。 
     阿龙也可以选择如下一条线路 1->4->5->4->5,并在第 1 次到达 5 号城市时以 1 的价格买入水晶球,在第 2 次到达 4 号城市时以 6 的价格卖出水晶球,赚取的旅费数为 5。 

     现在给出 n个城市的水晶球价格,m条道路的信息(每条道路所连接的两个城市的编号以及该条道路的通行情况) 。请你告诉阿龙,他最多能赚取多少旅费。

输入说明 :

第一行包含 2 个正整数 n 和 m,中间用一个空格隔开,分别表示城市的数目和道路的数目。 
第二行 n 个正整数,每两个整数之间用一个空格隔开,按标号顺序分别表示这 n 个城市的商品价格。 
接下来 m行, 每行有 3 个正整数, x, y, z, 每两个整数之间用一个空格隔开。 如果 z=1,表示这条道路是城市 x到城市 y之间的单向道路;如果 z=2,表示这条道路为城市 x 和城市y之间的双向道路。

输出说明 :

共1 行, 包含 1 个整数, 表示最多能赚取的旅费。 如果没有进行贸易,则输出 0。

范例输入 : 

5 5
4 3 5 6 1
1 2 1
1 4 1
2 3 2
3 5 1
4 5 2 

范例输出 :

5

提示 :

输入数据保证 1 号城市可以到达 n号城市。 
对于 10%的数据,1≤n≤6。 
对于 30%的数据,1≤n≤100。 
对于 50%的数据,不存在一条旅游路线,可以从一个城市出发,再回到这个城市。 
对于 100%的数据,1≤n≤100000,1≤m≤500000,1≤x,y≤n,1≤z≤2,1≤各城市水晶球价格≤100。

 

program Project1;
const maxn=100000;
type nodep=^node;
     node=record
                data:longint;
                next:nodep;
                end;
     graph=array[1..maxn]of nodep;
var buymin,sellmax:array[1..maxn]of longint;
    c:array[1..maxn]of longint;
    g1,g2:graph;
    queue:array[0..maxn]of longint;
    inqueue:array[1..maxn]of boolean;
    rear,front,res,n,m,i,x,y,z:longint;

procedure insert(var head:nodep;n:longint);
var p:nodep;
begin
     new(p);
     p^.data:=n;
     p^.next:=head;
     head:=p;
end;

function pick_out_front:longint;
begin
     front:=(front+1)mod maxn;
     pick_out_front:=queue[front];
     inqueue[pick_out_front]:=false;
end;

procedure join_in_rear(n:longint);
begin
     rear:=(rear+1)mod maxn;
     queue[rear]:=n;
     inqueue[n]:=true;
end;

function max(a,b:longint):longint;
begin
     if a>b then exit(a) else exit(b);
end;

function min(a,b:longint):longint;
begin
     if a<b then exit(a) else exit(b);
end;

procedure spfa_buymin;
var p:nodep;
    k,i:longint;
begin
     for i:=1 to n do buymin[i]:=maxlongint;
     fillchar(queue,sizeof(queue),0);
     fillchar(inqueue,sizeof(inqueue),0);
     queue[1]:=1;
     rear:=1;
     front:=0;
     while rear<>front do
     begin
          k:=pick_out_front;
          p:=g1[k];
          while p<>nil do
          begin
               if min(buymin[k],c[p^.data])<buymin[p^.data] then
               begin
                    buymin[p^.data]:=min(buymin[k],c[p^.data]);
                    if not(inqueue[p^.data]) then join_in_rear(p^.data);
               end;
               p:=p^.next;
          end;
     end;
end;

procedure spfa_sellmax;
var p:nodep;
    k,i:longint;
begin
     for i:=1 to n do sellmax[i]:=0;
     fillchar(queue,sizeof(queue),0);
     fillchar(inqueue,sizeof(inqueue),0);
     queue[1]:=n;
     rear:=1;
     front:=0;
     while rear<>front do
     begin
          k:=pick_out_front;
          p:=g2[k];
          while p<>nil do
          begin
               if max(sellmax[k],c[p^.data])>sellmax[p^.data] then
               begin
                    sellmax[p^.data]:=max(sellmax[k],c[p^.data]);
                    if not(inqueue[p^.data]) then join_in_rear(p^.data);
               end;
               p:=p^.next;
          end;
     end;
end;

begin
     readln(n,m);
     for i:=1 to n do read(c[i]);
     for i:=1 to m do
     begin
          readln(x,y,z);
          if z=1 then begin insert(g1[x],y);insert(g2[y],x); end
          else
          begin
               insert(g1[x],y);insert(g1[y],x);
               insert(g2[y],x);insert(g2[x],y);
          end;
     end;
     spfa_buymin;
     spfa_sellmax;
     res:=0;
     for i:=1 to n do if sellmax[i]-buymin[i]>res then res:=sellmax[i]-buymin[i];
     writeln(res);
end. 

注意:

1、注意这里:

if min(buymin[k],c[p^.data])<buymin[p^.data] then
               begin
                    buymin[p^.data]:=min(buymin[k],c[p^.data]);
                    if not(inqueue[p^.data]) then join_in_rear(p^.data);
               end; 

 

buymin[u]取buymin[k],c[u]和它本身的最小值。

(一开始buymin都是无限大,spfa就是有更新就进队列,也就是说,把自己从无限大更新到c[n]也要进队列)

2、队列是从队尾(rear)进数据,从队头(front)出队列。

进数据:inc(rear);queue[rear]:=n;

出数据:inc(front);